forked from richzeng/asterisk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
winmouse.py
234 lines (208 loc) · 9.26 KB
/
winmouse.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
import cv2
from cv2 import cv
import numpy
from time import time
import sys
if sys.platform != 'win32':
print "Error: Non-windows OS can not run this."
exit()
import win32api, win32con
def changemousepos(x,y):
win32api.SetCursorPos((x,y))
def clickdown(x, y):
changemousepos(x,y)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
def clickup(x, y):
changemousepos(x,y)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
def connectedcomps(img, cutoff):
rows, columns = cv.GetSize(img)
rowcutoff, colcutoff = int(rows*cutoff), int(columns*cutoff)
imgc = cv.CreateImage(cv.GetSize(img), 8, 1)
cv.Copy(img, imgc)
def getconnectedcomp(x, y):
imgarr = numpy.asarray(cv.GetMat(imgc))
if(imgarr[y][x] == 255):
return cv.FloodFill(imgc, (x, y), 255, 0, 0)
else: return (0, 0, 0)
complist = list(getconnectedcomp(x, y) for x in range(rowcutoff, rows-rowcutoff, 20) for y in range(colcutoff,columns-colcutoff,20))
complist = set(sorted(filter(lambda comp: comp[0] > 700, complist), key = lambda comp: comp[0]))
for comp in complist:
x, y, width, height = comp[-1]
x1, x2, y1, y2 = x, x + width, y, y + height
cv.Rectangle(img, (x1, y1), (x2, y2), (255, 0, 0))
return complist
#Get rectangle enclosing list of Connected Components
def getCompRect(complist):
minx, miny, maxx, maxy = 100000, 100000, 0, 0
for comp in complist:
x, y, width, height = comp[-1]
if(x < minx): minx = x
if(y < miny): miny = y
if(x + width > maxx): maxx = x
if(y + height > maxy): maxy = y
return (minx, miny, maxx, maxy)
def getSearchSpace(pastCompRect, prcnt, screenSize):
rows, columns = screenSize
minx, miny, maxx, maxy = pastCompRect
minx = max(0, int(minx - prcnt*(maxx - minx)))
maxx = min(rows, int(maxx + prcnt*(maxx - minx)))
miny = max(0, int(miny - prcnt*(maxy - miny)))
maxy = min(columns, int(maxy + prcnt*(maxy - miny)))
return minx, maxx, miny, maxy
def connectedcomps2(img, cutoff, pastcomps):
rows, columns = cv.GetSize(img)
rowscutoff, colcutoff = int(rows*cutoff), int(columns*cutoff)
if(pastcomps):
pastCompRect = getCompRect(pastcomps)
minx, maxx, miny, maxy = getSearchSpace(pastCompRect, 0.20, (rows, columns))
else:
minx, maxx, miny, maxy = rowscutoff, rows-rowscutoff, colcutoff, columns-colcutoff
imgc = cv.CreateImage(cv.GetSize(img), 8, 1)
cv.Copy(img, imgc)
def getconnectedcomp(x, y):
if(cv.Get2D(imgc, y, x)[0] == 255):
return cv.FloodFill(imgc, (x, y), 125, 0, 0)
else: return (0, 0, 0)
complist = list(getconnectedcomp(x, y) for x in range(minx, maxx, 20) for y in range(miny, maxy,20))
complist = sorted(filter(lambda comp: comp[0] > 0, complist), key = lambda comp: comp[0])
for comp in complist:
x, y, width, height = comp[-1]
x1, x2, y1, y2 = x, x + width, y, y + height
cv.Rectangle(img, (x1, y1), (x2, y2), (255, 0, 0))
return complist
class BlobTracker:
def __init__(self):
cv.NamedWindow("Image",1)
cv.NamedWindow("Threshold",1)
self.capture = cv.CaptureFromCAM(0)
def run(self):
while cv.WaitKey(10) != 27:
img = cv.QueryFrame(self.capture)
cv.Flip(img,img,1)
cv.CvtColor(img, img, cv.CV_BGR2HSV)
#cv.Dilate(img, img, None, 3)
#cv.Erode(img, img, None, 1)
#cv.Smooth(img, img, cv.CV_BLUR, 3)
h = cv.CreateImage(cv.GetSize(img), 8, 1)
s = cv.CreateImage(cv.GetSize(img), 8, 1)
v = cv.CreateImage(cv.GetSize(img), 8, 1)
cv.Split(img, h, s, v, None)
threshold_h = cv.CreateImage(cv.GetSize(img), 8, 1)
cv.Threshold(h, threshold_h, 15, 255, cv.CV_THRESH_BINARY)
cv.Merge(threshold_h, s, v, None, img)
cv.CvtColor(img, img, cv.CV_HSV2BGR)
#cv.ShowImage("Hue", threshold_h)
#cv.ShowImage("Image", img)
self.destroy()
def run2(self):
pastcomps = []
mouse_down = False
while cv.WaitKey(10) != 27:
img = cv.QueryFrame(self.capture)
cv.Flip(img,img,1)
cv.ShowImage("Image", img)
r = cv.CreateImage(cv.GetSize(img), 8, 1)
g = cv.CreateImage(cv.GetSize(img), 8, 1)
b = cv.CreateImage(cv.GetSize(img), 8, 1)
cv.Split(img, r, g, b, None)
cv.Smooth(g, g, cv.CV_BLUR, 4)
threshold_g = cv.CreateImage(cv.GetSize(img), 8, 1)
cv.InRangeS(g, 65, 160, threshold_g)
cv.CvtColor(img, img, cv.CV_BGR2HSV)
h = cv.CreateImage(cv.GetSize(img), 8, 1)
s = cv.CreateImage(cv.GetSize(img), 8, 1)
v = cv.CreateImage(cv.GetSize(img), 8, 1)
cv.Split(img, h, s, v, None)
cv.Erode(h, h, None, 1)
threshold_h = cv.CreateImage(cv.GetSize(img), 8, 1)
threshold_s = cv.CreateImage(cv.GetSize(img), 8, 1)
threshold_total = cv.CreateImage(cv.GetSize(img), 8, 1)
cv.InRangeS(h, 55, 95, threshold_h)
cv.InRangeS(s, 50, 255, threshold_s)
cv.And(threshold_h, threshold_s, threshold_total)
cv.And(threshold_total, threshold_g, threshold_total)
#cv.ShowImage("Filtered", threshold_total)
cv.Smooth(threshold_total, threshold_total, cv.CV_BLUR, 11)
cv.Threshold(threshold_total, threshold_total, 100, 255, cv.CV_THRESH_BINARY)
t = time()
comps = connectedcomps(threshold_total, 0.10)
'''
if(len(comps)<=1):
comps = list(comps)
if(len(comps)==1): x1, y1, width1, height1 = comps[0][-1]
#x2, y2, width2, height2 = comps[1][-1]
#x1, x2 = int(x1*(1360/700.)), int(x2*(1360/700.))
#y1, y2 = int(y1*(768/520.)), int(y1*(768/520.))
#x1, y1 = x1 + width1//2, y1 + height1//2
#x2, y2 = x2 + width2//2, y2 + height2//2
#print((x1-x2)**2 + (y1-y2)**2, x1, y1, x2, y2)
if(len(comps)==0):
if(not mouse_down):
mouse_down = True
clickdown(x1, y1)
elif(mouse_down):
changemousepos(x1, y1)
elif(mouse_down):
mouse_down = False
clickup(x1, y1)
else:
changemousepos(x1, y1)
'''
if(len(comps)==2):
comps = list(comps)
x1, y1, width1, height1 = comps[0][-1]
x2, y2, width2, height2 = comps[1][-1]
x1, y1 = x1 + width1//2, y1 + height1//2
x2, y2 = x2 + width2//2, y2 + height2//2
x1, x2 = int(x1*(1360/650.)), int(x2*(1360/650.))
y1, y2 = int(y1*(768/480.)), int(y2*(768/480.))
#print((x1-x2)**2 + (y1-y2)**2, x1, y1, x2, y2)
if((x1-x2)**2 + (y1-y2)**2 < 12000):
if(not mouse_down):
mouse_down = True
clickdown((x1+x2)//2, (y1 + y2)//2)
elif(mouse_down):
changemousepos((x1+x2)//2, (y1+y2)//2)
elif(mouse_down):
mouse_down = False
clickup((x1+x2)//2, (y1 + y2)//2)
else:
changemousepos((x1+x2)//2, (y1+y2)//2)
#pastcomps = connectedcomps2(threshold_total, 0.20, pastcomps)
#print(time() - t)
cv.ShowImage("Threshold", threshold_total)
r = cv.GetSubRect(threshold_total, (30, 30, img.width-30, img.height-30))
do_edges = False
if do_edges:
try:
mem = cv.CreateMemStorage()
contours = cv.FindContours(r,mem,cv.CV_RETR_LIST)
if contours:
moments = cv.Moments(contours, 0)
area = cv.GetCentralMoment(moments, 0, 0)
if area > 10:
x = cv.GetSpatialMoment(moments, 1, 0)/area
y = cv.GetSpatialMoment(moments, 0, 1)/area
print('x:{0} y{1} area:{2}'.format(x,y,area))
except:
raise
do_box = False
if do_box:
cv.Canny(threshold_total, threshold_total, 25, 75)
storage = cv.CreateMemStorage(0)
obj = cv.FindContours(threshold_total, storage, cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_SIMPLE)
aa = cv.ApproxPoly(obj, storage, cv.CV_POLY_APPROX_DP)
print [a for a in aa]
box = cv.BoundingRect(obj)
#print((box[0]+(box[2]/2), box[1]+(box[3]/2)))
cv.Rectangle(img, (box[0], box[1]), (box[0] + box[2], box[1] + box[3]),(255,0,0),1,8,0)
#cv.ShowImage("Postprocessed", r)
#cv.ShowImage("Test", threshold_g)
self.destroy()
def destroy(self):
cv.DestroyAllWindows()
del self.capture
if __name__=="__main__":
color_tracker = BlobTracker()
color_tracker.run2()