| import os |
| import random |
| import matplotlib.pyplot as plt |
| import numpy as np |
| import tensorflow as tf |
| from keras_preprocessing import image |
|
|
|
|
| |
| |
| vgg16_model = tf.keras.models.load_model('../Rock_Paper_Scissors_VGG16/best_weights.hdf5') |
| img_width, img_height = 224, 224 |
| |
| class_labels = ['paper', 'rock', 'scissors'] |
| folder_path = '../rps/test' |
|
|
|
|
| |
| 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) |
|
|
|
|
| |
| 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 |
|
|
| return img_array |
|
|
|
|
| |
| 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 |
|
|
|
|
| |
| image1 = load_and_preprocess_image(selectRandomPicture(folder_path)) |
| image2 = load_and_preprocess_image(selectRandomPicture(folder_path)) |
|
|
| |
| 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]] |
|
|
| |
| plt.figure(figsize=(8, 5)) |
| plt.subplot(1, 2, 1) |
| plt.imshow(image1) |
| plt.title(class_labels[predicted_classes[0]]) |
| plt.axis('off') |
| |
| 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)}') |
|
|