c++
commited on
Update RPS_Part2_VGG16.py
Browse files- RPS_Part2_VGG16.py +58 -61
RPS_Part2_VGG16.py
CHANGED
|
@@ -1,61 +1,58 @@
|
|
| 1 |
-
import sys
|
| 2 |
-
import os
|
| 3 |
-
import numpy as np
|
| 4 |
-
import tensorflow as tf
|
| 5 |
-
from keras_preprocessing import image
|
| 6 |
-
from matplotlib import pyplot as plt
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
#
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
#
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
plt.
|
| 38 |
-
plt.
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
# it sends the images and loaded model to prediction function
|
| 60 |
-
img = image.load_img(filepath, target_size=(img_width, img_height))
|
| 61 |
-
predict_image(img, model)
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
import os
|
| 3 |
+
import numpy as np
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
from keras_preprocessing import image
|
| 6 |
+
from matplotlib import pyplot as plt
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
# Loading the pre-trained model/best saved weight and perform Prediction
|
| 10 |
+
# model = tf.keras.models.load_model('../Rock_Paper_Scissors_VGG16/RPS_Model.hdf5')
|
| 11 |
+
model = tf.keras.models.load_model('../Rock_Paper_Scissors_VGG16/best_weights.hdf5')
|
| 12 |
+
img_width, img_height = 224, 224
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
# Predict function
|
| 16 |
+
def predict_image(image_input, model):
|
| 17 |
+
if image_input is None or image_input == '':
|
| 18 |
+
print("Invalid type")
|
| 19 |
+
return None
|
| 20 |
+
# putting the images in an array
|
| 21 |
+
img_array = image.img_to_array(image_input)
|
| 22 |
+
processed_img = tf.reshape(img_array, shape=[1, img_width, img_height, 3])
|
| 23 |
+
|
| 24 |
+
# It uses the model to predict the class probabilities for the processed image.
|
| 25 |
+
predict_proba = np.max(model.predict(processed_img)[0])
|
| 26 |
+
# It identifies the predicted class index and its corresponding label.
|
| 27 |
+
predict_class = np.argmax(model.predict(processed_img))
|
| 28 |
+
|
| 29 |
+
# Map predicted class index to label
|
| 30 |
+
class_labels = ['Paper', 'Rock', 'Scissors']
|
| 31 |
+
predict_label = class_labels[predict_class]
|
| 32 |
+
|
| 33 |
+
# It plots the input image with its predicted class label and displays the image without axis ticks.
|
| 34 |
+
plt.figure(figsize=(4, 4))
|
| 35 |
+
plt.imshow(img)
|
| 36 |
+
plt.axis('off')
|
| 37 |
+
plt.title(f'Predicted Class: {predict_label}')
|
| 38 |
+
plt.show()
|
| 39 |
+
|
| 40 |
+
# Print prediction result and probability
|
| 41 |
+
print("\nImage prediction result:", predict_label)
|
| 42 |
+
print("Probability:", round(predict_proba * 100, 2), "%")
|
| 43 |
+
print('\n')
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
# asking the user for their desired folder location
|
| 47 |
+
if __name__ == "__main__":
|
| 48 |
+
image_path = ''
|
| 49 |
+
if len(sys.argv) != 2:
|
| 50 |
+
image_path = input("Enter the path to the image file: ")
|
| 51 |
+
if input() == '':
|
| 52 |
+
image_path = '../rps/test'
|
| 53 |
+
# it collects 21 random images from the folder
|
| 54 |
+
for filename in os.listdir(image_path)[0:20]:
|
| 55 |
+
filepath = os.path.join(image_path, filename)
|
| 56 |
+
# it sends the images and loaded model to prediction function
|
| 57 |
+
img = image.load_img(filepath, target_size=(img_width, img_height))
|
| 58 |
+
predict_image(img, model)
|
|
|
|
|
|
|
|
|