-
Notifications
You must be signed in to change notification settings - Fork 0
/
markerMouse.py
122 lines (86 loc) · 3.04 KB
/
markerMouse.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
import cv2 as cv
import datetime
import numpy as np
import time
import mouse as m
import time
import HandTrackingModule as htm
import pyautogui as pg
import pydirectinput as pn
import win32api, win32con
wCam,hCam = 1280,720
plocx,plocy=0,0
clocx,clocy=0,0
cap = cv.VideoCapture(1)
cap.set(3, wCam)
cap.set(4, hCam)
tipIds = [4,8,12,16,20]
last_click = datetime.datetime.now()
detector = htm.HandDetector(maxHands=1,detectionCon=0.75)
while True:
success,img = cap.read()
img = cv.flip(img,1)
hsv = cv.cvtColor(img,cv.COLOR_BGR2HSV)
lower_blue = np.array([89,40,59])
upper_blue = np.array([179,255,255])
mask_blue = cv.inRange(hsv, lower_blue, upper_blue)
lower_green = np.array([34,59,71])
upper_green = np.array([84,236,255])
mask_green = cv.inRange(hsv, lower_green, upper_green)
lower_red = np.array([0,139,184])
upper_red = np.array([179,255,255])
mask_red = cv.inRange(hsv, lower_red, upper_red)
contoursBlue, _ = cv.findContours(mask_blue, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
contoursGreen, _ = cv.findContours(mask_green, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
contoursRed, _ = cv.findContours(mask_red, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
for c in contoursRed:
if cv.contourArea(c)<=50:
continue
x, y, _, _ = cv.boundingRect(c)
clocx = plocx + (x-plocx)/6
clocy = plocy + (y-plocy)/6
win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, int(clocx-plocx), int(clocy-plocy), 0, 0)
#m.move((clocx-plocx),(clocy-plocy),False)
#pg.moveTo(wScreen-clocx,clocy)
#pg.moveRel()
plocx,plocy = clocx,clocy
cv.drawContours(img, contoursRed, -1, (0, 255, 0), 3)
#for c in contoursBlue:
# if cv.contourArea(c)<=75:
# #m.release(button='left')
# continue
# now = datetime.datetime.now()
# diff = now - last_click
# if diff.total_seconds() > 0.5:
# last_click = datetime.datetime.now()
# cv.drawContours(img, contoursBlue, -1, (0, 255, 0), 3)
# m.click(button='left')
img = detector.findHands(img)
lmList = detector.findPosition(img)
if len(lmList)!=0:
#x1,y1=lmList[8][1:]
fingers = []
#THUMB
if lmList[tipIds[0]][1] > lmList[tipIds[0]-1][1]:
fingers.append(1)
else:
fingers.append(0)
#FINGERS
for id in range(1,5):
if lmList[tipIds[id]][2] < lmList[tipIds[id]-1][2]:
fingers.append(1)
else:
fingers.append(0)
if fingers[1]==1:
pn.keyDown('w')
else:
pn.keyUp('w')
if fingers[0]==1:
m.press()
else:
m.release()
cv.imshow("Image",img)
key = cv.waitKey(1)
if key==27:
break;
cap.release()