-
Notifications
You must be signed in to change notification settings - Fork 76
/
play.py
99 lines (77 loc) · 2.55 KB
/
play.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
from keras.models import load_model
import cv2
import numpy as np
from random import choice
REV_CLASS_MAP = {
0: "rock",
1: "paper",
2: "scissors",
3: "none"
}
def mapper(val):
return REV_CLASS_MAP[val]
def calculate_winner(move1, move2):
if move1 == move2:
return "Tie"
if move1 == "rock":
if move2 == "scissors":
return "User"
if move2 == "paper":
return "Computer"
if move1 == "paper":
if move2 == "rock":
return "User"
if move2 == "scissors":
return "Computer"
if move1 == "scissors":
if move2 == "paper":
return "User"
if move2 == "rock":
return "Computer"
model = load_model("rock-paper-scissors-model.h5")
cap = cv2.VideoCapture(0)
prev_move = None
while True:
ret, frame = cap.read()
if not ret:
continue
# rectangle for user to play
cv2.rectangle(frame, (100, 100), (500, 500), (255, 255, 255), 2)
# rectangle for computer to play
cv2.rectangle(frame, (800, 100), (1200, 500), (255, 255, 255), 2)
# extract the region of image within the user rectangle
roi = frame[100:500, 100:500]
img = cv2.cvtColor(roi, cv2.COLOR_BGR2RGB)
img = cv2.resize(img, (227, 227))
# predict the move made
pred = model.predict(np.array([img]))
move_code = np.argmax(pred[0])
user_move_name = mapper(move_code)
# predict the winner (human vs computer)
if prev_move != user_move_name:
if user_move_name != "none":
computer_move_name = choice(['rock', 'paper', 'scissors'])
winner = calculate_winner(user_move_name, computer_move_name)
else:
computer_move_name = "none"
winner = "Waiting..."
prev_move = user_move_name
# display the information
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(frame, "Your Move: " + user_move_name,
(50, 50), font, 1.2, (255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(frame, "Computer's Move: " + computer_move_name,
(750, 50), font, 1.2, (255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(frame, "Winner: " + winner,
(400, 600), font, 2, (0, 0, 255), 4, cv2.LINE_AA)
if computer_move_name != "none":
icon = cv2.imread(
"images/{}.png".format(computer_move_name))
icon = cv2.resize(icon, (400, 400))
frame[100:500, 800:1200] = icon
cv2.imshow("Rock Paper Scissors", frame)
k = cv2.waitKey(10)
if k == ord('q'):
break
cap.release()
cv2.destroyAllWindows()