-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCursorPositionCalibrationAndGestureControls.py
165 lines (113 loc) · 6.96 KB
/
CursorPositionCalibrationAndGestureControls.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from HelperFunctions import HandModule
import time
from subprocess import call
import numpy as np
import cv2 as openCV
import autopy
import pyautogui
flag = 0
widthScreen, heightScreen = 640, 480
frame = 100 # Frame Reduction
videoCapture = openCV.VideoCapture(0)
videoCapture.set(4, heightScreen)
videoCapture.set(3, widthScreen)
previousTimeValue = 0
previousXCoordinate, previousYCoordinate, currentXCoordinate, currentYCoordinate = 0, 0, 0, 0
screenWidth, screenHeight = autopy.screen.size()
handModuleObject = HandModule()
while True:
try:
_, currentFrame = videoCapture.read()
handsData, currentFrame = handModuleObject.identifyHands(currentFrame)
if handsData:
# Hand-1
hand1 = handsData[0]
lmList1 = hand1["lmList"]
handType1 = hand1["type"]
fingers1Shown = handModuleObject.identifyFingers(hand1); print(f"Fingers1: {fingers1Shown}")
if len(handsData) == 1 and flag == 1:
# 2. Get the tip of the index and middle fingers
if len(lmList1) != 0:
x1, y1 = lmList1[8][0], lmList1[8][1]
x2, y2 = lmList1[12][0], lmList1[12][1]
# 4. Only Index Finger : Moving Mode
if fingers1Shown[1] == 1 and fingers1Shown[2] == 0 and fingers1Shown[4] == 0:
# 5. Convert Coordinates
x3 = np.interp(x1, (frame, widthScreen - frame), (0, screenWidth))
y3 = np.interp(y1, (frame, heightScreen - frame), (0, screenHeight))
# 6. Smoothen Values
currentXCoordinate = previousXCoordinate + (x3 - previousXCoordinate) / 7
currentYCoordinate = previousYCoordinate + (y3 - previousYCoordinate) / 7
# 7. Move Mouse
autopy.mouse.move(screenWidth - currentXCoordinate, currentYCoordinate)
openCV.circle(currentFrame, (x1, y1), 8, (255,255,255), openCV.FILLED)
previousXCoordinate, previousYCoordinate = currentXCoordinate, currentYCoordinate
# 8. Both Index and middle fingers are up : Clicking Mode
if fingers1Shown[1] == 1 and fingers1Shown[2] == 1 and fingers1Shown[3] != 1 and fingers1Shown[4] != 1:
autopy.mouse.click()
# Right Click
if fingers1Shown[1] == 1 and fingers1Shown[2] == 1 and fingers1Shown[3] == 1 and fingers1Shown[4] != 1:
# autopy.mouse.toggle(down=False, button = autopy.mouse.Button.RIGHT)
pyautogui.click(button='right')
# Scroll
if fingers1Shown[1] == 1 and fingers1Shown[2] == 1 and fingers1Shown[3] == 1 and fingers1Shown[4] == 1:
try:
tempSuccess2, tempCurrentFrame = videoCapture.read()
tempHandsData, tempCurrentFrame = handModuleObject.identifyHands(tempCurrentFrame)
tempLmList2 = tempHandsData[0]["lmList"]
tempx1, tempy1 = tempLmList2[8][0], tempLmList2[8][1]
if tempy1 > y1 and fingers1Shown[1] == 1:
pyautogui.scroll(100)
print("UP")
elif tempy1 < y1 and fingers1Shown[1] == 1:
pyautogui.scroll(-100)
print("Down")
except Exception as ScrollError:
print(f"ScrollError: {ScrollError}")
# Zoom In
if fingers1Shown[0] == 1 and fingers1Shown[1] == 1 and fingers1Shown[2] == 1 and fingers1Shown[3] == 0 and fingers1Shown[4] == 1:
pyautogui.keyDown('ctrl')
pyautogui.scroll(1)
pyautogui.keyUp('ctrl')
# Zoom Out
if fingers1Shown[0] == 1 and fingers1Shown[1] == 1 and fingers1Shown[2] == 0 and fingers1Shown[3] == 0 and fingers1Shown[4] == 1:
pyautogui.keyDown('ctrl')
pyautogui.scroll(-1)
pyautogui.keyUp('ctrl')
elif len(handsData) == 2:
hand2 = handsData[1]
lmList2 = hand2["lmList"]
handType2 = hand2["type"]
fingers2Shown = handModuleObject.identifyFingers(hand2); print(f"Fingers2: {fingers2Shown}")
# Pause
if fingers1Shown[0] == 1 and fingers1Shown[1] == 1 and fingers1Shown[2] == 1 and fingers1Shown[3] == 1 and fingers1Shown[4] == 1 and fingers2Shown[0] == 1 and fingers2Shown[1] == 1 and fingers2Shown[2] == 1 and fingers2Shown[3] == 1 and fingers2Shown[4] == 1:
flag = 0
print("Pause")
# Play
elif fingers1Shown[0] == 0 and fingers1Shown[1] == 0 and fingers1Shown[2] == 0 and fingers1Shown[3] == 0 and fingers1Shown[4] == 0 and fingers2Shown[0] == 0 and fingers2Shown[1] == 0 and fingers2Shown[2] == 0 and fingers2Shown[3] == 0 and fingers2Shown[4] == 0:
flag = 1
print("Play")
if flag == 1:
# Volume Up
if fingers1Shown[0] == 1 and fingers1Shown[1] == 1 and fingers1Shown[2] == 1 and fingers1Shown[3] == 0 and fingers1Shown[4] == 1:
# pyautogui.press("volumeup")
print("Volume Up")
call(["amixer", "-D", "pulse", "sset", "Master", "2%+"])
# Volume Down
if fingers1Shown[0] == 1 and fingers1Shown[1] == 1 and fingers1Shown[2] == 0 and fingers1Shown[3] == 0 and fingers1Shown[4] == 1:
# pyautogui.press("volumedown")
print("Volume Down")
call(["amixer", "-D", "pulse", "sset", "Master", "2%-"])
if flag == 0:
openCV.putText(currentFrame, "Pause", (460, 50), openCV.FONT_HERSHEY_PLAIN, 3, (0, 0, 0), 3)
# 11. Frame Rate
currentTimeValue = time.time()
fps = 1 / (currentTimeValue - previousTimeValue)
previousTimeValue = currentTimeValue
openCV.putText(currentFrame, str(int(fps)), (20, 50), openCV.FONT_HERSHEY_PLAIN, 3, (0, 0, 0), 3)
# 12. Display
openCV.imshow("Image", currentFrame)
if openCV.waitKey(1) == ord('q'):
break
except Exception as MainError:
print(MainError)