-
Notifications
You must be signed in to change notification settings - Fork 0
/
play.py
60 lines (43 loc) · 1.28 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
from keras.models import load_model
import cv2
import numpy as np
from random import choice
import pyttsx3
REV_CLASS_MAP = {
0: "yes",
1: "no",
2: "alldone",
3: "please"
}
def mapper(val):
return REV_CLASS_MAP[val]
engine = pyttsx3.init()
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, (300, 300))
# predict the move made
pred = model.predict(np.array([img]))
move_code = np.argmax(pred[0])
user_move_name = mapper(move_code)
print(user_move_name)
if cv2.waitKey(1) & 0xFF == ord(' '):
engine.say(user_move_name)
engine.runAndWait()
cv2.imshow("Rock Paper Scissors", frame)
k = cv2.waitKey(10)
if k == ord('q'):
break
cap.release()
cv2.destroyAllWindows()