-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
88 lines (55 loc) · 1.96 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
78
79
80
81
82
83
84
85
86
87
88
import cv2
import mediapipe as mp
import pyautogui
import time
def count_fingers(lst):
cnt = 0
thresh = (lst.landmark[0].y*100 - lst.landmark[9].y*100)/2
if (lst.landmark[5].y*100 - lst.landmark[8].y*100) > thresh:
cnt += 1
if (lst.landmark[9].y*100 - lst.landmark[12].y*100) > thresh:
cnt += 1
if (lst.landmark[13].y*100 - lst.landmark[16].y*100) > thresh:
cnt += 1
if (lst.landmark[17].y*100 - lst.landmark[20].y*100) > thresh:
cnt += 1
if (lst.landmark[5].x*100 - lst.landmark[4].x*100) > 6:
cnt += 1
return cnt
cap = cv2.VideoCapture(0)
drawing = mp.solutions.drawing_utils
hands = mp.solutions.hands
hand_obj = hands.Hands(max_num_hands=1)
start_init = False
prev = -1
while True:
end_time = time.time()
_, frm = cap.read()
frm = cv2.flip(frm, 1)
res = hand_obj.process(cv2.cvtColor(frm, cv2.COLOR_BGR2RGB))
if res.multi_hand_landmarks:
hand_keyPoints = res.multi_hand_landmarks[0]
cnt = count_fingers(hand_keyPoints)
if not(prev==cnt):
if not(start_init):
start_time = time.time()
start_init = True
elif (end_time-start_time) > 0.2:
if (cnt == 1):
pyautogui.press("right")
elif (cnt == 2):
pyautogui.press("left")
elif (cnt == 3):
pyautogui.press("up")
elif (cnt == 4):
pyautogui.press("down")
elif (cnt == 5):
pyautogui.press("space")
prev = cnt
start_init = False
drawing.draw_landmarks(frm, hand_keyPoints, hands.HAND_CONNECTIONS)
cv2.imshow("window", frm)
if cv2.waitKey(1) == 27:
cv2.destroyAllWindows()
cap.release()
break