File size: 2,678 Bytes
f60ca25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import os
import random
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from keras_preprocessing import image


# Loading the pre-trained model/best saved weight and perform Prediction
# vgg16_model = tf.keras.models.load_model('../Rock_Paper_Scissors_VGG16/RPS_Model.hdf5')
vgg16_model = tf.keras.models.load_model('../Rock_Paper_Scissors_VGG16/best_weights.hdf5')
img_width, img_height = 224, 224
# Define labels and Image addresses
class_labels = ['paper', 'rock', 'scissors']
folder_path = '../rps/test'


# Get a random Image from Folder
def selectRandomPicture(folder_path):
    files = os.listdir(folder_path)
    image_files = [file for file in files if file.lower().endswith(('.png', '.jpg', '.jpeg'))]
    random_photo = random.choice(image_files)
    return os.path.join(folder_path, random_photo)


# Function to load and preprocess images
def load_and_preprocess_image(image_path):
    img = image.load_img(image_path, target_size=(img_width, img_height))
    img_array = image.img_to_array(img)
    img_array /= 255.0  # Normalize pixel values

    return img_array


# A simple condition/rules to decide who’s the winner
def whos_winner(first_image, second_image):
    winner = ''
    if first_image == second_image:
        winner = "Tie!!"

    elif (first_image == 'rock' and second_image == 'scissors' or
          first_image == 'scissors' and second_image == 'rock'):
        winner = "Rock wins"

    elif (first_image == 'rock' and second_image == 'paper' or
          first_image == 'paper' and second_image == 'rock'):
        winner = "Paper wins"

    elif (first_image == 'paper' and second_image == 'scissors' or
          first_image == 'scissors' and second_image == 'paper'):
        winner = "Scissors wins"

    return winner


# Read and preprocess the images and put 2 images in separate variables
image1 = load_and_preprocess_image(selectRandomPicture(folder_path))
image2 = load_and_preprocess_image(selectRandomPicture(folder_path))

# Predict the labels of the images
images = np.array([image1, image2])
predictions = vgg16_model.predict(images)
predicted_classes = np.argmax(predictions, axis=1)

firs_img = class_labels[predicted_classes[0]]
sec_img = class_labels[predicted_classes[1]]

# Plot the images
plt.figure(figsize=(8, 5))
plt.subplot(1, 2, 1)
plt.imshow(image1)
plt.title(class_labels[predicted_classes[0]])
plt.axis('off')
# Plot/Display the last result
plt.subplot(1, 2, 2)
plt.imshow(image2)
plt.title(class_labels[predicted_classes[1]])
plt.axis('off')
plt.tight_layout()
plt.suptitle(f'{whos_winner(firs_img, sec_img)}!')
plt.show()

print(f'The winner is:{whos_winner(firs_img, sec_img)}')