-
Notifications
You must be signed in to change notification settings - Fork 9
/
Dinosuar.py
70 lines (58 loc) · 2.26 KB
/
Dinosuar.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
# The code is inspired by https://github.com/uvipen/AirGesture
import tensorflow as tf
import cv2
import multiprocessing as _mp
from utils import load_graph, detect_hands, predict
from utils import RED, GREEN, YELLOW, BLUE, ORANGE
from pyKey import pressKey, releaseKey, press #inspired by https://github.com/andohuman/pyKey for controlling keyboard keys
width = 640
height = 480
threshold = 0.6
alpha = 0.3
pre_trained_model_path = "model/pretrained_model.pb"
def main():
graph, sess = load_graph(pre_trained_model_path)
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
mp = _mp.get_context("spawn")
v = mp.Value('i', 0)
lock = mp.Lock()
while True:
key = cv2.waitKey(10)
if key == ord("q"):
break
_, frame = cap.read()
frame = cv2.flip(frame, 1)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
boxes, scores, classes = detect_hands(frame, graph, sess)
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
results = predict(boxes, scores, classes, threshold, width, height)
if len(results) == 1:
x_min, x_max, y_min, y_max, category = results[0]
x = int((x_min + x_max) / 2)
y = int((y_min + y_max) / 2)
cv2.circle(frame, (x, y), 5, RED, -1)
if category == "Closed":
action = 0 # Do nothing
text = "Run"
elif category == "Open" and y < height/2:
action = 1 # Jump
pressKey('UP')
text = "Jump"
elif category == "Open" and y > height/2:
action = 2
pressKey('DOWN')
text = "Duck"
with lock:
v.value = action
cv2.putText(frame, "{}".format(text), (x_min, y_min - 5),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, GREEN, 2)
overlay = frame.copy()
cv2.rectangle(overlay, (0, 0), (width, int(height / 2)), ORANGE, -1)
cv2.addWeighted(overlay, alpha, frame, 1 - alpha, 0, frame)
cv2.imshow('Detection', frame)
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
main()