-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
65 lines (51 loc) · 1.71 KB
/
main.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
import cv2
from hands import Landmarker, Hand
from cursor import Cursor
import time
def main():
"""
Cursor will follow right hand pointer finger
"""
video_capture = cv2.VideoCapture(0)
if not video_capture.isOpened():
print("Error opening video stream or file")
exit(-1)
landmarker = Landmarker('hand_landmarker.task', num_hands=2)
cursor = Cursor()
left, right = Hand(), Hand()
break_timer = None
while True:
ret, frame = video_capture.read()
if not ret:
break
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
landmark_result = landmarker.get_landmarks(image)
left.get_landmarks(landmark_result, 'Left')
right.get_landmarks(landmark_result, 'Right')
if right.landmarks:
pos = right.get_centroid()
cursor.move_to(pos)
if left.landmarks:
left.do_gesture(cursor)
# Check if program should stop
if right.landmarks and left.landmarks:
if right.is_scrolling() and left.is_scrolling():
if break_timer is None:
break_timer = time.time()
else:
if time.time() - break_timer > 3: # More than 3 seconds
break
else:
break_timer = None
# image = draw_landmarks_on_image(image, landmark_result)
# image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
# cv2.imshow('Result', image)
#
# pressed_key = cv2.waitKey(1)
# if pressed_key & 0xFF == ord('q'):
# break
video_capture.release()
cv2.destroyAllWindows()
print("Finished")
if __name__ == '__main__':
main()