-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvirtual_mouse2.py
125 lines (91 loc) · 3.48 KB
/
virtual_mouse2.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 numpy as np
import pyautogui
import time
import pickle
with open("range.pickle","rb") as file:
a = pickle.load(file)
print(a)
(sx,sy)=pyautogui.size()
(camx, camy) = (640, 480)
#lower and upper bound for hsv range.
lowerBound = np.array([150,100,100])
upperBound = np.array([179,255,255])
cam = cv2.VideoCapture(0)
kernelOpen = np.ones((5,5))
kernelClose = np.ones((20,20))
pinchFlag = 0
while True:
ret, img = cam.read()
img = cv2.resize(img, (camx,camy))
img = cv2.flip(img,1)
imgHSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
#Apply mask for red color
mask = cv2.inRange(imgHSV, lowerBound, upperBound)
maskOpen = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernelOpen)
maskClose = cv2.morphologyEx(maskOpen, cv2.MORPH_CLOSE, kernelClose)
maskFinal = maskClose
# cv2.imshow("HSV_Masked", maskFinal)
(version, _, _) = cv2.__version__.split('.')
if version == '3':
_, contours, hierarchy = cv2.findContours(maskFinal.copy(),cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
elif version == '2':
contours, hierarchy = cv2.findContours(maskFinal.copy(),cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
if(len(contours)==1):
#one object mode
cnt = contours[0]
m = cv2.moments(cnt)
cx = int(m['m10']/m['m00'])
cy = int(m['m01']/m['m00'])
cv2.circle(img, (cx, cy), 10, (0, 0, 255), -1)
cv2.imshow("img", img)
print("In one object mode")
elif(len(contours)==2):
#two object mode
cnt1 = contours[0]
m = cv2.moments(cnt1)
cx1 = int(m['m10']/m['m00'])
cy1 = int(m['m01']/m['m00'])
cv2.circle(img, (cx1, cy1), 10, (0, 0, 255), -1)
cnt2 = contours[1]
m2 = cv2.moments(cnt2)
cx2 = int(m2['m10']/m2['m00'])
cy2 = int(m2['m01']/m2['m00'])
cv2.circle(img, (cx2, cy2), 10, (0, 0, 255), -1)
cv2.line(img, (cx1, cy1), (cx2, cy2), (0,0,255), 2)
cv2.imshow("img", img)
centerX = int((cx1+cx2)/2)
centerY = int((cy1+cy2)/2)
cv2.circle(img, (centerX, centerY), 3, (0, 0, 255), -1)
xi = sx - (centerX*(sx/camx))
yi = centerY*(sy/camy)
pyautogui.moveTo(xi, yi)
print("In two object mode")
# time.sleep(1)
elif(len(contours)==3):
#three object mode
cnt1 = contours[0]
m = cv2.moments(cnt1)
cx1 = int(m['m10']/m['m00'])
cy1 = int(m['m01']/m['m00'])
cv2.circle(img, (cx1, cy1), 3, (0, 0, 255), -1)
cnt2 = contours[1]
m2 = cv2.moments(cnt2)
cx2 = int(m2['m10']/m2['m00'])
cy2 = int(m2['m01']/m2['m00'])
cv2.circle(img, (cx2, cy2), 3, (0, 0, 255), -1)
cnt3 = contours[2]
m3 = cv2.moments(cnt3)
cx3 = int(m3['m10']/m3['m00'])
cy3 = int(m3['m01']/m3['m00'])
cv2.circle(img, (cx3, cy3), 3, (0, 0, 255), -1)
cv2.imshow("img", img)
print("In three object mode")
else:
print("Neither in one, two or three object mode!!")
cv2.drawContours(img, contours, -1, (0,255,0), 3)
cv2.imshow("img", img)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
cam.release()
cv2.destroyAllWindows()