-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass_rps.py
203 lines (148 loc) · 6.71 KB
/
class_rps.py
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import numpy as np
import random
import cv2
from keras.models import load_model
class RPS:
'''Represents a game of Rock, Paper, Scissors.
Attributes:
computer_wins (int): The number of times the computer has won.
user_wins (int): The number of times the user has won.
'''
def __init__(self):
'''Initializes a new instance of the RPS class with the computer_wins and user_wins attributes set to 0.'''
self.computer_wins = 0
self.user_wins = 0
def play(self):
'''The function runs a loop until either the computer or user wins three times,
calling other functions to get user input and computer choice, and determine the
winner of each round.
'''
while True:
if self.computer_wins == 3:
print(f"Game over! Computer won {self.computer_wins}:{self.user_wins}.")
break
elif self.user_wins == 3:
print(f"Game over! You won {self.user_wins}:{self.computer_wins}.")
break
else:
user = self.get_prediction()
comp = self.get_computer_choice()
self.get_winner(comp, user)
def get_computer_choice(self):
'''The function randomly selects and returns a move (Rock, Paper, or Scissors) for the computer.
Returns
-------
the computer's choice of either "Rock", "Paper", or "Scissors".
'''
moves = ["Rock", "Paper", "Scissors"]
computer_choice = random.choice(moves)
print(f"Computer chose: {computer_choice}")
return computer_choice
def countdown_animation(self, frame, seconds_left):
'''This function draws a countdown text on a given frame using OpenCV library in Python.
Parameters
----------
frame
The current frame of the video or image on which the countdown text will be drawn.
seconds_left
The number of seconds left in the countdown. This parameter is used to display the remaining time
on the frame.
'''
# Draw countdown text on the frame
font = cv2.FONT_HERSHEY_TRIPLEX
text = str(seconds_left)
textsize = cv2.getTextSize(text, font, 4, 5)[0]
text_x = int((frame.shape[1] - textsize[0]) / 2)
text_y = int((frame.shape[0] + textsize[1]) / 2)
cv2.putText(frame, text, (text_x, text_y), font, 4, (0, 0, 255), 5)
def get_prediction(self):
'''This function loads a pre-trained Keras model and uses it to predict the label of an image captured
from a webcam after a countdown animation.
Returns
-------
the label of the predicted object in the final frame captured by the camera.
'''
model = load_model('keras_model_mat_2_day_night.h5')
with open('labels.txt', 'r') as f:
# Read the lines of the file and strip the newline characters
lines = [line.strip() for line in f.readlines()]
# Create an empty dictionary to store the indices and labels
labels = {}
# Loop through the lines and split them into key-value pairs
for line in lines:
idx, label = line.split()
labels[int(idx)] = label
cap = cv2.VideoCapture(0)
data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
# Set countdown timer
countdown_time = 5 # seconds
start_time = cv2.getTickCount() / cv2.getTickFrequency()
# Start countdown
while True:
# Capture a frame from the camera
ret, frame = cap.read()
# Check if frame was successfully captured
if not ret:
print("Unable to capture frame")
break
# Calculate time elapsed
time_elapsed = (cv2.getTickCount() / cv2.getTickFrequency()) - start_time
# Check if countdown has finished
if time_elapsed >= countdown_time:
break
# Display countdown animation
self.countdown_animation(frame, countdown_time - int(time_elapsed))
# Display the frame
cv2.imshow("Countdown", frame)
# Wait for a key press
key = cv2.waitKey(1)
if key == ord('q'):
break
# Capture final frame
ret, frame = cap.read()
# Check if frame was successfully captured
if not ret:
print("Unable to capture frame")
# Resize and normalize image
resized_frame = cv2.resize(frame, (224, 224), interpolation=cv2.INTER_AREA)
image_np = np.array(resized_frame)
normalized_image = (image_np.astype(np.float32) / 127.0) - 1 # Normalize the image
data[0] = normalized_image
# Predict on final frame
prediction = model.predict(data)
index = np.argmax(prediction)
#confidence_score = prediction[0][index]
print(f"You chose: {labels[index]}")
cap.release()
cv2.destroyAllWindows()
if labels[index] in ["Rock", "Paper", "Scissors"]:
return labels[index]
# In case it predicts "Nothing"
else:
print("Please choose Rock, Paper or Scissors!")
self.get_prediction()
def get_winner(self, comp, user):
'''Explains the game rules and logic.'''
if comp == "Rock" and user == "Paper":
print("You won!")
self.user_wins += 1
elif comp == "Rock" and user == "Scissors":
print("You lost!")
self.computer_wins += 1
elif comp == "Paper" and user == "Rock":
print("You lost!")
self.computer_wins += 1
elif comp == "Paper" and user == "Scissors":
print("You won!")
self.user_wins += 1
elif comp == "Scissors" and user == "Rock":
print("You won!")
self.user_wins += 1
elif comp == "Scissors" and user == "Paper":
print("You lost!")
self.computer_wins += 1
else:
print("It's a tie!")
if __name__ == "__main__":
rps_game = RPS()
rps_game.play()