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

Update RPS_Part2_VGG16.py

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