c++ commited on
Commit
f60ca25
·
verified ·
1 Parent(s): 7c27607

Update RPS_Part3_VGG16.py

Browse files
Files changed (1) hide show
  1. RPS_Part3_VGG16.py +83 -87
RPS_Part3_VGG16.py CHANGED
@@ -1,87 +1,83 @@
1
- import os
2
- import random
3
- import matplotlib.pyplot as plt
4
- import numpy as np
5
- import tensorflow as tf
6
- from keras_preprocessing import image
7
-
8
- # Part 3
9
- # a) Read two images of hand signs provided as script arguments
10
- # b) Predict the labels of the two images
11
- # c) Output which image won the rock, paper, scissor game
12
-
13
- # Loading the pre-trained model/best saved weight and perform Prediction
14
- # vgg16_model = tf.keras.models.load_model('../Rock_Paper_Scissors_VGG16/RPS_Model.hdf5')
15
- vgg16_model = tf.keras.models.load_model('../Rock_Paper_Scissors_VGG16/best_weights.hdf5')
16
- img_width, img_height = 224, 224
17
- # Define labels and Image addresses
18
- class_labels = ['paper', 'rock', 'scissors']
19
- folder_path = '../rps/test'
20
-
21
-
22
- # Get a random Image from Folder
23
- def selectRandomPicture(folder_path):
24
- files = os.listdir(folder_path)
25
- image_files = [file for file in files if file.lower().endswith(('.png', '.jpg', '.jpeg'))]
26
- random_photo = random.choice(image_files)
27
- return os.path.join(folder_path, random_photo)
28
-
29
-
30
- # Function to load and preprocess images
31
- def load_and_preprocess_image(image_path):
32
- img = image.load_img(image_path, target_size=(img_width, img_height))
33
- img_array = image.img_to_array(img)
34
- img_array /= 255.0 # Normalize pixel values
35
-
36
- return img_array
37
-
38
-
39
- # A simple condition/rules to decide who’s the winner
40
- def whos_winner(first_image, second_image):
41
- winner = ''
42
- if first_image == second_image:
43
- winner = "Tie!!"
44
-
45
- elif (first_image == 'rock' and second_image == 'scissors' or
46
- first_image == 'scissors' and second_image == 'rock'):
47
- winner = "Rock wins"
48
-
49
- elif (first_image == 'rock' and second_image == 'paper' or
50
- first_image == 'paper' and second_image == 'rock'):
51
- winner = "Paper wins"
52
-
53
- elif (first_image == 'paper' and second_image == 'scissors' or
54
- first_image == 'scissors' and second_image == 'paper'):
55
- winner = "Scissors wins"
56
-
57
- return winner
58
-
59
-
60
- # Read and preprocess the images and put 2 images in separate variables
61
- image1 = load_and_preprocess_image(selectRandomPicture(folder_path))
62
- image2 = load_and_preprocess_image(selectRandomPicture(folder_path))
63
-
64
- # Predict the labels of the images
65
- images = np.array([image1, image2])
66
- predictions = vgg16_model.predict(images)
67
- predicted_classes = np.argmax(predictions, axis=1)
68
-
69
- firs_img = class_labels[predicted_classes[0]]
70
- sec_img = class_labels[predicted_classes[1]]
71
-
72
- # Plot the images
73
- plt.figure(figsize=(8, 5))
74
- plt.subplot(1, 2, 1)
75
- plt.imshow(image1)
76
- plt.title(class_labels[predicted_classes[0]])
77
- plt.axis('off')
78
- # Plot/Display the last result
79
- plt.subplot(1, 2, 2)
80
- plt.imshow(image2)
81
- plt.title(class_labels[predicted_classes[1]])
82
- plt.axis('off')
83
- plt.tight_layout()
84
- plt.suptitle(f'{whos_winner(firs_img, sec_img)}!')
85
- plt.show()
86
-
87
- print(f'The winner is:{whos_winner(firs_img, sec_img)}')
 
1
+ import os
2
+ import random
3
+ import matplotlib.pyplot as plt
4
+ import numpy as np
5
+ import tensorflow as tf
6
+ from keras_preprocessing import image
7
+
8
+
9
+ # Loading the pre-trained model/best saved weight and perform Prediction
10
+ # vgg16_model = tf.keras.models.load_model('../Rock_Paper_Scissors_VGG16/RPS_Model.hdf5')
11
+ vgg16_model = tf.keras.models.load_model('../Rock_Paper_Scissors_VGG16/best_weights.hdf5')
12
+ img_width, img_height = 224, 224
13
+ # Define labels and Image addresses
14
+ class_labels = ['paper', 'rock', 'scissors']
15
+ folder_path = '../rps/test'
16
+
17
+
18
+ # Get a random Image from Folder
19
+ def selectRandomPicture(folder_path):
20
+ files = os.listdir(folder_path)
21
+ image_files = [file for file in files if file.lower().endswith(('.png', '.jpg', '.jpeg'))]
22
+ random_photo = random.choice(image_files)
23
+ return os.path.join(folder_path, random_photo)
24
+
25
+
26
+ # Function to load and preprocess images
27
+ def load_and_preprocess_image(image_path):
28
+ img = image.load_img(image_path, target_size=(img_width, img_height))
29
+ img_array = image.img_to_array(img)
30
+ img_array /= 255.0 # Normalize pixel values
31
+
32
+ return img_array
33
+
34
+
35
+ # A simple condition/rules to decide who’s the winner
36
+ def whos_winner(first_image, second_image):
37
+ winner = ''
38
+ if first_image == second_image:
39
+ winner = "Tie!!"
40
+
41
+ elif (first_image == 'rock' and second_image == 'scissors' or
42
+ first_image == 'scissors' and second_image == 'rock'):
43
+ winner = "Rock wins"
44
+
45
+ elif (first_image == 'rock' and second_image == 'paper' or
46
+ first_image == 'paper' and second_image == 'rock'):
47
+ winner = "Paper wins"
48
+
49
+ elif (first_image == 'paper' and second_image == 'scissors' or
50
+ first_image == 'scissors' and second_image == 'paper'):
51
+ winner = "Scissors wins"
52
+
53
+ return winner
54
+
55
+
56
+ # Read and preprocess the images and put 2 images in separate variables
57
+ image1 = load_and_preprocess_image(selectRandomPicture(folder_path))
58
+ image2 = load_and_preprocess_image(selectRandomPicture(folder_path))
59
+
60
+ # Predict the labels of the images
61
+ images = np.array([image1, image2])
62
+ predictions = vgg16_model.predict(images)
63
+ predicted_classes = np.argmax(predictions, axis=1)
64
+
65
+ firs_img = class_labels[predicted_classes[0]]
66
+ sec_img = class_labels[predicted_classes[1]]
67
+
68
+ # Plot the images
69
+ plt.figure(figsize=(8, 5))
70
+ plt.subplot(1, 2, 1)
71
+ plt.imshow(image1)
72
+ plt.title(class_labels[predicted_classes[0]])
73
+ plt.axis('off')
74
+ # Plot/Display the last result
75
+ plt.subplot(1, 2, 2)
76
+ plt.imshow(image2)
77
+ plt.title(class_labels[predicted_classes[1]])
78
+ plt.axis('off')
79
+ plt.tight_layout()
80
+ plt.suptitle(f'{whos_winner(firs_img, sec_img)}!')
81
+ plt.show()
82
+
83
+ print(f'The winner is:{whos_winner(firs_img, sec_img)}')