-
Notifications
You must be signed in to change notification settings - Fork 0
/
GestureController.py
125 lines (101 loc) · 4.12 KB
/
GestureController.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
import cv2
import time
import handTrackingModule as htm
import pyautogui
from FingerMovementFunctions import FingerMovements
def main():
pyautogui.FAILSAFE = False
# camera frame resolution
wCam, hCam = 640, 480
# capture device
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
cap.set(3, wCam)
cap.set(4, hCam)
x, y = pyautogui.size()
pTime = index = swipe = fiveUp = volCtrl = cd = vlstp = length = pos_x = vl1 = 0
detector = htm.handDetector(min_detectioncon=0.75)
# Storing the fingertips
tipIds = [4, 8, 12, 16, 20]
# cool down (in frames)
cdTime = 40
while True:
success, img = cap.read()
detector.findHands(img)
lmList = detector.findPosition(img, draw=True)
if len(lmList) != 0:
fingers = []
# Thumb detection - to use the right hand keep the ">" for left hand use "<"
if lmList[tipIds[0]][1] > lmList[tipIds[0]-1][1]:
# if the finger is up, its value is 1, if not its 0
fingers.append(1)
else:
fingers.append(0)
# Other fingers
for id in range(1, 5):
if lmList[tipIds[id]][2] < lmList[tipIds[id]-2][2]:
fingers.append(1)
else:
fingers.append(0)
# Actions
mov = FingerMovements(img, fingers, lmList, cdTime, cd, x, y)
if cd == 0:
# the values in "fingers" correspond to [thumb, index, middle finger, ring finger, pinky]
# For every frame you keep your fingers in one of the positions below, that's a + 1 to It's variable
if fingers == [0, 1, 0, 0, 0]:
index += 1
elif fingers == [0, 1, 1, 0, 0]:
swipe += 1
elif fingers == [1, 1, 0, 0, 0]:
volCtrl += 1
elif fingers == [1, 1, 1, 1, 1]:
fiveUp += 1
# Fist is the standard position
elif fingers == [0, 0, 0, 0, 0]:
fiveUp = volCtrl = swipe = 0
if index >= 25:
# releasing the mouse
pyautogui.mouseUp(pyautogui.position())
index = 0
cd = cdTime
if index >= 22:
# sliding the mouse on the Youtube Music player
pos_x = mov.slide(lmList[8][1], index)
elif swipe == 20:
# gets the position of the index finger
pos_x = lmList[8][1]
elif 200 > swipe >= 30:
# if the index moved enough it skips or go back a song depending on the direction
if mov.swipe(pos_x, lmList[8][1]):
pos_x = lmList[8][1]
cd = cdTime
swipe = 0
elif swipe >= 200:
print("Failed to understand, please try again")
swipe = 0
elif volCtrl == 15:
# in case it's muted
pyautogui.press('volumeup')
# Just for you to see the volume going up and down
pyautogui.moveTo(130, 90)
elif volCtrl >= 17:
length, vl1, cd, vlstp, volCtrl = mov.volCtrl(pos_x, lmList[8][1], vlstp, volCtrl, length, vl1)
# Pause/Continue
elif fiveUp == 20:
cd, fiveUp = mov.pause()
else:
cd -= 1
cTime = time.time()
fps = 1/(cTime-pTime)
pTime = cTime
cv2.putText(img, f'FPS: {int(fps)}', (10, 25), cv2.FONT_HERSHEY_PLAIN,
1.2, (255, 0, 0), 2)
cv2.putText(img, f'Cooldown : {int(cd)}', (10, 55), cv2.FONT_HERSHEY_PLAIN,
1.2, (255, 0, 0), 2)
cv2.imshow("GestureCapture", img)
# press 'q' to end the program
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
main()