-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVideoControl.py
More file actions
97 lines (58 loc) · 2.41 KB
/
VideoControl.py
File metadata and controls
97 lines (58 loc) · 2.41 KB
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
89
90
91
92
93
94
95
96
97
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) # read frames from 0th webcam
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() # reading frames and storing them in frm
frm = cv2.flip(frm, 1) # flip camera
# passing frm in hand_obj but in RGB format
res = hand_obj.process(cv2.cvtColor(frm, cv2.COLOR_BGR2RGB))
# res can be null also incase no hands are detected
if res.multi_hand_landmarks: #means if hands are detected
hand_keyPoints = res.multi_hand_landmarks[0] # we will detect only 1 hand
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) # drawing the landmarks on detected hands
cv2.imshow("window", frm) # show frames to user
# if user presses the ESC key
if cv2.waitKey(1) == 27:
cv2.destroyAllWindows() # close camera window
cap.release() # release camera
break # break this while Loop