-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
77 lines (60 loc) · 1.63 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
66
67
68
69
70
71
72
73
74
75
76
77
# import the opencv library
import cv2
import HandTrackingModule
import pyautogui
import time
import threading
import keyboard
def go_right():
global key
while key:
pyautogui.press('d')
time.sleep(0.01)
def go_left():
global key
while key:
pyautogui.press('a')
time.sleep(0.01)
def drift():
global key
pyautogui.keyDown('s')
while key:
time.sleep(0.01)
pyautogui.keyUp('s')
# define a video capture object
vid = cv2.VideoCapture(0)
handDetector = HandTrackingModule.handDetector()
state = None
pre_state = state
key = False
while (True):
# Capture the video frame
# by frame
ret, frame = vid.read()
frame, state = handDetector.find_Hands(frame)
# State: 1: W, 2: D, 3: SD, 4: A, 5: SA
if state != pre_state:
key = False
if state == 2:
t = threading.Thread(target=drift)
elif state == 3:
t = threading.Thread(target=go_right)
elif state == 4:
t = threading.Thread(target=drift)
elif state == 5:
t = threading.Thread(target=go_left)
elif state == None:
key = False
if state != None:
time.sleep(0.2)
key = True
t.start()
pre_state = state
# Display the resulting frame
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# After the loop release the cap object
vid.release()
# Destroy all the windows
cv2.destroyAllWindows()