-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
77 lines (61 loc) · 2.39 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 cv2
from cvzone.HandTrackingModule import HandDetector
import time
import pyautogui
detector = HandDetector(detectionCon=0.8, maxHands=1)
time.sleep(2.0)
video = cv2.VideoCapture(0)
prev_key = "nothing"
while True:
ret, frame = video.read()
video.set(cv2.CAP_PROP_FRAME_WIDTH, 1000)
video.set(cv2.CAP_PROP_FRAME_HEIGHT, 2000)
hands, img = detector.findHands(frame)
cv2.rectangle(img, (0, 480), (300, 425), (50, 50, 255), -2)
cv2.rectangle(img, (640, 480), (400, 425), (50, 50, 255), -2)
if hands:
lmList = hands[0]
fingerUp = detector.fingersUp(lmList)
if fingerUp == [1, 1, 1, 1, 1]:
cv2.putText(frame, 'Right Direction', (20, 460),
cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 1,
cv2.LINE_AA)
cv2.putText(frame, 'Accelerator', (420, 460),
cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 1,
cv2.LINE_AA)
if(prev_key == "left"):
pyautogui.keyUp('left')
prev_key = "right"
pyautogui.keyDown('right')
prev_key = "right"
if fingerUp == [0, 0, 0, 0, 0]:
cv2.putText(frame, 'Left Direction', (20, 460),
cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 1,
cv2.LINE_AA)
cv2.putText(frame, 'Reverse', (420, 460),
cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 1,
cv2.LINE_AA)
if(prev_key == "right"):
pyautogui.keyUp('right')
prev_key = "left"
pyautogui.keyDown('left')
prev_key = "left"
if(0 < sum(fingerUp) < 5):
cv2.putText(frame, 'Nothing', (20, 460),
cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 1,
cv2.LINE_AA)
cv2.putText(frame, 'No action', (420, 460),
cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 1,
cv2.LINE_AA)
if(prev_key == "right"):
pyautogui.keyUp('right')
prev_key = "nothing"
if(prev_key == "left"):
pyautogui.keyUp('left')
prev_key = "nothing"
cv2.imshow("frame", frame)
k = cv2.waitKey(1)
if k == ord('q'):
break
video.release()
cv2.destroyAllWindows()