c++
commited on
Update RPS_Part3_VGG16.py
Browse files- 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 |
-
|
| 9 |
-
#
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
folder_path
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
winner = "
|
| 44 |
-
|
| 45 |
-
elif (first_image == 'rock' and second_image == '
|
| 46 |
-
first_image == '
|
| 47 |
-
winner = "
|
| 48 |
-
|
| 49 |
-
elif (first_image == '
|
| 50 |
-
first_image == '
|
| 51 |
-
winner = "
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
#
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
plt.
|
| 74 |
-
|
| 75 |
-
plt.
|
| 76 |
-
plt.
|
| 77 |
-
plt.
|
| 78 |
-
|
| 79 |
-
plt.
|
| 80 |
-
plt.
|
| 81 |
-
plt.
|
| 82 |
-
|
| 83 |
-
|
| 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)}')
|
|
|
|
|
|
|
|
|
|
|
|