-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
308 lines (261 loc) · 10.5 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import cv2
import keyboard_module as htm
import pyautogui
def keyboard():
cap = cv2.VideoCapture(0)
wCam, hCam = 640, 480
cap.set(3, wCam)
cap.set(4, hCam)
detector = htm.handDetector(detectionCon=0.7, maxHands=2)
r1_reset = True
r2_reset = True
r3_reset = True
r4_reset = True
l1_reset = True
l2_reset = True
l3_reset = True
l4_reset = True
shift_press = False
while True:
success, img = cap.read()
img_flipped = cv2.flip(img, 1)
img = detector.findHands(img_flipped)
lmList0, box0 = detector.findPosition(img, handNo=0, draw_circle=True, draw_box=False)
lmList1, box1 = detector.findPosition(img, handNo=1, draw_circle=True, draw_box=False)
fingers_r = []
fingers_l = []
def rl_detector(inp_list):
if inp_list[4][1] < inp_list[20][1]:
return 0
if inp_list[4][1] > inp_list[20][1]:
return 1
if len(lmList0) != 0:
area0 = (box0[2] - box0[0]) * (box0[3] - box0[1]) // 100
if area0 > 150:
fingers0 = detector.fingersUp(lmList0)
if rl_detector(lmList0) == 0:
fingers_r = fingers0
if rl_detector(lmList0) == 1:
fingers_l = fingers0
if len(lmList1) != 0:
area1 = (box1[2] - box1[0]) * (box1[3] - box1[1]) // 100
if area1 > 150:
fingers1 = detector.fingersUp(lmList1)
if rl_detector(lmList1) == 0:
fingers_r = fingers1
if rl_detector(lmList1) == 1:
fingers_l = fingers1
if len(fingers_r) != 0 and len(fingers_l) != 0:
if fingers_r[1:2] == [1]:
r1_reset = True
if fingers_r[2:3] == [1]:
r2_reset = True
if fingers_r[3:4] == [1]:
r3_reset = True
if fingers_r[4:5] == [1]:
r4_reset = True
if fingers_l[1:2] == [1]:
l1_reset = True
if fingers_l[2:3] == [1]:
l2_reset = True
if fingers_l[3:4] == [1]:
l3_reset = True
if fingers_l[4:5] == [1]:
l4_reset = True
if fingers_r[0:1] == [1] and fingers_l[0:1] == [1]:
if fingers_l[1:5] == [1, 1, 1, 0] and l4_reset:
if shift_press:
pyautogui.press("1")
else:
pyautogui.press("a")
l4_reset = False
if fingers_l[1:5] == [1, 1, 0, 1] and l3_reset:
if shift_press:
pyautogui.press("S")
else:
pyautogui.press("s")
l3_reset = False
if fingers_l[1:5] == [1, 0, 1, 1] and l2_reset:
if shift_press:
pyautogui.press("D")
else:
pyautogui.press("d")
l2_reset = False
if fingers_l[1:5] == [0, 1, 1, 1] and l1_reset:
if shift_press:
pyautogui.press("F")
else:
pyautogui.press("f")
l1_reset = False
if fingers_r[1:5] == [0, 1, 1, 1] and r1_reset:
if shift_press:
pyautogui.press("H")
else:
pyautogui.press("h")
r1_reset = False
if fingers_r[1:5] == [1, 0, 1, 1] and r2_reset:
if shift_press:
pyautogui.press("J")
else:
pyautogui.press("j")
r2_reset = False
if fingers_r[1:5] == [1, 1, 0, 1] and r3_reset:
if shift_press:
pyautogui.press("K")
else:
pyautogui.press("k")
r3_reset = False
if fingers_r[1:5] == [1, 1, 1, 0] and r4_reset:
if shift_press:
pyautogui.press("L")
else:
pyautogui.press("l")
r4_reset = False
if fingers_r[0:1] == [0] and fingers_l[0:1] == [1]:
if fingers_l[1:5] == [1, 1, 1, 0] and l4_reset:
if shift_press:
pyautogui.press("Q")
else:
pyautogui.press("q")
l4_reset = False
if fingers_l[1:5] == [1, 1, 0, 1] and l3_reset:
if shift_press:
pyautogui.press("W")
else:
pyautogui.press("w")
l3_reset = False
if fingers_l[1:5] == [1, 0, 1, 1] and l2_reset:
if shift_press:
pyautogui.press("E")
else:
pyautogui.press("e")
l2_reset = False
if fingers_l[1:5] == [0, 1, 1, 1] and l1_reset:
if shift_press:
pyautogui.press("R")
else:
pyautogui.press("r")
l1_reset = False
if fingers_r[1:5] == [0, 1, 1, 1] and r1_reset:
if shift_press:
pyautogui.press("Y")
else:
pyautogui.press("y")
r1_reset = False
if fingers_r[1:5] == [1, 0, 1, 1] and r2_reset:
if shift_press:
pyautogui.press("U")
else:
pyautogui.press("u")
r2_reset = False
if fingers_r[1:5] == [1, 1, 0, 1] and r3_reset:
if shift_press:
pyautogui.press("I")
else:
pyautogui.press("i")
r3_reset = False
if fingers_r[1:5] == [1, 1, 1, 0] and r4_reset:
if shift_press:
pyautogui.press("O")
else:
pyautogui.press("o")
r4_reset = False
if fingers_r[0:1] == [1] and fingers_l[0:1] == [0]:
if fingers_l[1:5] == [1, 1, 1, 0] and l4_reset:
if shift_press:
pyautogui.press("Z")
else:
pyautogui.press("z")
l4_reset = False
if fingers_l[1:5] == [1, 1, 0, 1] and l3_reset:
if shift_press:
pyautogui.press("X")
else:
pyautogui.press("x")
l3_reset = False
if fingers_l[1:5] == [1, 0, 1, 1] and l2_reset:
if shift_press:
pyautogui.press("C")
else:
pyautogui.press("c")
l2_reset = False
if fingers_l[1:5] == [0, 1, 1, 1] and l1_reset:
if shift_press:
pyautogui.press("V")
else:
pyautogui.press("v")
l1_reset = False
if fingers_r[1:5] == [0, 1, 1, 1] and r1_reset:
if shift_press:
pyautogui.press("B")
else:
pyautogui.press("b")
r1_reset = False
if fingers_r[1:5] == [1, 0, 1, 1] and r2_reset:
if shift_press:
pyautogui.press("N")
else:
pyautogui.press("n")
r2_reset = False
if fingers_r[1:5] == [1, 1, 0, 1] and r3_reset:
if shift_press:
pyautogui.press("M")
else:
pyautogui.press("m")
r3_reset = False
if fingers_r[1:5] == [1, 1, 1, 0] and r4_reset:
if shift_press:
pyautogui.press("P")
else:
pyautogui.press("p")
r4_reset = False
if fingers_r[0:1] == [0] and fingers_l[0:1] == [0]:
if fingers_l[1:5] == [1, 1, 1, 0] and l4_reset and not shift_press:
shift_press = True
l4_reset = False
if fingers_l[1:5] == [1, 1, 1, 0] and l4_reset and shift_press:
shift_press = False
l4_reset = False
if fingers_l[1:5] == [1, 1, 0, 1] and l3_reset:
if shift_press:
pyautogui.press("=")
else:
pyautogui.press("enter")
l3_reset = False
if fingers_l[1:5] == [1, 0, 1, 1]:
if shift_press:
pyautogui.press("delete")
else:
pyautogui.press("backspace")
l2_reset = False
if fingers_l[1:5] == [0, 1, 1, 1] and l1_reset:
if shift_press:
pyautogui.press("T")
else:
pyautogui.press("t")
l1_reset = False
if fingers_r[1:5] == [0, 1, 1, 1] and r1_reset:
if shift_press:
pyautogui.press("G")
else:
pyautogui.press("g")
r1_reset = False
if fingers_r[1:5] == [1, 0, 1, 1] and r2_reset:
if shift_press:
pyautogui.press(":")
else:
pyautogui.press(",")
r2_reset = False
if fingers_r[1:5] == [1, 1, 0, 1] and r3_reset:
if shift_press:
pyautogui.press('?')
else:
pyautogui.press(".")
r3_reset = False
if fingers_r[1:5] == [1, 1, 1, 0] and r4_reset:
if not shift_press:
pyautogui.press("space")
r4_reset = False
cv2.imshow("Image", img)
cv2.waitKey(1)
keyboard()