-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_model.py
76 lines (48 loc) · 1.83 KB
/
test_model.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
import cv2
from keras.models import load_model
import numpy as np
import time
class CameraInput:
def __init__(self):
self.model = load_model('keras_model.h5')
self.cap = cv2.VideoCapture(0)
self.data = np.ndarray(shape=(1, 224, 224, 3), dtype = np.float32)
self.choices = ['rock', 'paper', 'scissors', 'nothing']
def get_user_input(self,prediction):
"""
Gives the predicted output on the input camera images based on the list of probabilities
"""
user_move = np.argmax(prediction)
user_cat = self.choices[user_move]
return(user_cat)
def countdown(self):
"""
Displats the countdown to zero to indicate to get ready to play game
"""
print("User Move")
for i in range(3, 0 ,-1):
#time.sleep(1)
time.time()
print(i)
return(None)
def get_prediction(self):
"""
Open the camera and gets the prediction for the move made
"""
while True:
self.countdown()
ret,frame = self.cap.read()
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
self.data[0] = normalized_image
prediction = self.model.predict(self.data)
cv2.imshow('frame', frame)
choice = self.get_user_input(prediction[0])
if choice != 'nothing':
break
if cv2.waitKey(1) & 0XFF == ord('q'):
break
self.cap.release()
cv2.destroyAllWindows()
return(choice)