-
Notifications
You must be signed in to change notification settings - Fork 0
/
mouse_control.py
71 lines (64 loc) · 2.14 KB
/
mouse_control.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
import cv2
import numpy as np
from pynput.mouse import Button, Controller
import wx
cam= cv2.VideoCapture(0)
kernelOpen=np.ones((5,5))
kernelClose=np.ones((20,20))
pinchFlag=0
app=wx.App(False)
(sx,sy)=wx.GetDisplaySize()
(camx,camy)=(320,240)
mouse=Controller()
lowerBound=np.array([33,80,40])
upperBound=np.array([102,255,255])
while True:
ret, img=cam.read()
img=cv2.resize(img,(340,220))
imgHSV= cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
mask=cv2.inRange(imgHSV,lowerBound,upperBound)
maskOpen=cv2.morphologyEx(mask,cv2.MORPH_OPEN,kernelOpen)
maskClose=cv2.morphologyEx(maskOpen,cv2.MORPH_CLOSE,kernelClose)
maskFinal=maskClose
_,conts,h=cv2.findContours(maskFinal.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
if(len(conts)==2):
if(pinchFlag==1):
pinchFlag=0
mouse.release(Button.left)
x1,y1,w1,h1=cv2.boundingRect(conts[0])
x2,y2,w2,h2=cv2.boundingRect(conts[1])
cv2.rectangle(img,(x1,y1),(x1+w1,y1+h1),(255,0,0),2)
cv2.rectangle(img,(x2,y2),(x2+w2,y2+h2),(255,0,0),2)
cx1=int(x1+w1/2)
cy1=int(y1+h1/2)
cx2=int(x2+w2/2)
cy2=int(y2+h2/2)
cx=int((cx1+cx2)/2)
cy=int((cy1+cy2)/2)
cv2.line(img, (cx1,cy1),(cx2,cy2),(255,0,0),2)
cv2.circle(img, (cx,cy),2,(0,0,255),2)
print(sx-((cx*sx)/camx))
mouseLoc=(int(sx-((cx*sx)/camx)),int( cy*sy/camy))
mouse.move(int(sx-((cx*sx)/camx)),int( cy*sy/camy))
'''while mouse.position!=mouseLoc:
pass'''
elif(len(conts)==1):
x,y,w,h=cv2.boundingRect(conts[0])
if(pinchFlag==0):
pinchFlag=1
mouse.press(Button.left)
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
cx=int(x+w/2)
cy=int(y+h/2)
cv2.circle(img,(cx,cy),int((w+h)/4),(0,0,255),2)
print(sx-((cx*sx)/camx))
mouseLoc=(int(sx-(cx*sx/camx)), int(cy*sy/camy))
mouse.move(int(sx-((cx*sx)/camx)),int( cy*sy/camy))
''''while mouse.position!=mouseLoc:
pass'''
cv2.imshow("cam",img)
k=cv2.waitKey(30) & 0xff
if k==27:
break
camera.release()
cv2.destroyAllWindows()