-
Notifications
You must be signed in to change notification settings - Fork 0
/
app-mouse.py
48 lines (41 loc) · 1.53 KB
/
app-mouse.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
import cv2
from utils.initialize_models import models_init
from utils import hand_track_utils as htutils
from utils.utils import gestures_map_to_behaviors, draw_keypoints
from pynput.mouse import Controller, Button
from utils.hand_track_utils import *
mouse = Controller()
action_recog = []
def main():
capture = cv2.VideoCapture(0)
if capture.isOpened():
hasFrame, frame = capture.read()
else:
hasFrame = False
detector = models_init()
scale = 2.5
while hasFrame:
points, _ = detector(frame.copy())
gesture = None
if points is not None:
gesture = htutils.recog_gesture(points)
synthesized_img = draw_keypoints(frame, points)
synthesized_img = gestures_map_to_behaviors(frame, gesture)
action_recog.insert(0, [gesture, points])
if len(action_recog) == 5:
action_recog.pop(len(action_recog) - 1)
if len(action_recog) > 1:
mouse.move(action_recog[1][1][8][0] * scale - action_recog[0][1][8][0] * scale, action_recog[0][1][8][1] * scale - action_recog[1][1][8][1] * scale)
if gesture == "2":
mouse.click(Button.left, 1)
if synthesized_img is None:
break
cv2.imshow("handTrack", synthesized_img)
hasFrame, frame = capture.read()
key = cv2.waitKey(1)
if key == ord('q'):
break
capture.release()
cv2.destroyAllWindows()
if __name__=='__main__':
main()