-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVideoControl_withVoice.py
128 lines (78 loc) · 2.97 KB
/
VideoControl_withVoice.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import cv2
import mediapipe as mp
import pyautogui
import time
import pgzrun
# Set Dimensions of Camera Window
wcam , hcam = 640,480
sounds.welcome.play()
sounds.intro.play()
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)
cap.set(3,wcam)
cap.set(4,hcam)
pTime = 0 # present time
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)
# Displaying FPS on Screen
cTime = time.time()
fps = 1/(cTime-pTime)
pTime = cTime
cv2.putText(frm, f'FPS : {int(fps)}', (550,20),cv2.FONT_HERSHEY_PLAIN,1,(255,0,0),1)
cv2.putText(frm, 'CONTROLS', (5,25),cv2.FONT_HERSHEY_TRIPLEX,1,(255,0,0),1)
cv2.putText(frm, '1 : Forward', (5,50),cv2.FONT_HERSHEY_COMPLEX_SMALL,1,(0,255,0),1)
cv2.putText(frm, '2 : Forward', (5,75),cv2.FONT_HERSHEY_COMPLEX_SMALL,1,(0,255,0),1)
cv2.putText(frm, '3 : Forward', (5,100),cv2.FONT_HERSHEY_COMPLEX_SMALL,1,(0,255,0),1)
cv2.putText(frm, '4 : Forward', (5,125),cv2.FONT_HERSHEY_COMPLEX_SMALL,1,(0,255,0),1)
cv2.putText(frm, '5 : Forward', (5,150),cv2.FONT_HERSHEY_COMPLEX_SMALL,1,(0,255,0),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):
sounds.forward.play()
pyautogui.press("right")
elif (cnt == 2):
sounds.backward.play()
pyautogui.press("left")
elif (cnt == 3):
sounds.volup.play()
pyautogui.press("up")
elif (cnt == 4):
sounds.voldown.play()
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