-
Notifications
You must be signed in to change notification settings - Fork 1
/
alphaV.py
424 lines (376 loc) · 17.7 KB
/
alphaV.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
import numpy as np
import cv2
from common import draw_str, RectSelector
import video
import win32api,win32con
import pyautogui
screenWidth, screenHeight = pyautogui.size()
#required_area is the maximum area of hand and max_area is the current area of palm
required_area = 0
max_area = 0
#variables used for clicking
end =0
one=0
def rnd_warp(a):
h, w = a.shape[:2]
T = np.zeros((2, 3))
coef = 0.2
ang = (np.random.rand()-0.5)*coef
c, s = np.cos(ang), np.sin(ang)
T[:2, :2] = [[c,-s], [s, c]]
T[:2, :2] += (np.random.rand(2, 2) - 0.5)*coef
c = (w/2, h/2)
T[:,2] = c - np.dot(T[:2, :2], c)
return cv2.warpAffine(a, T, (w, h), borderMode = cv2.BORDER_REFLECT)
def divSpec(A, B):
Ar, Ai = A[...,0], A[...,1]
Br, Bi = B[...,0], B[...,1]
C = (Ar+1j*Ai)/(Br+1j*Bi)
C = np.dstack([np.real(C), np.imag(C)]).copy()
return C
eps = 1e-5
#Efficient tracking of object
class MOSSE:
def __init__(self, frame, rect):
x1, y1, x2, y2 = rect
w, h = map(cv2.getOptimalDFTSize, [x2-x1, y2-y1])
x1, y1 = (x1+x2-w)//2, (y1+y2-h)//2
self.pos = x, y = x1+0.5*(w-1), y1+0.5*(h-1)
self.size = w, h
img = cv2.getRectSubPix(frame, (w, h), (x, y))
self.win = cv2.createHanningWindow((w, h), cv2.CV_32F)
g = np.zeros((h, w), np.float32)
g[h//2, w//2] = 1
g = cv2.GaussianBlur(g, (-1, -1), 2.0)
g /= g.max()
self.G = cv2.dft(g, flags=cv2.DFT_COMPLEX_OUTPUT)
self.H1 = np.zeros_like(self.G)
self.H2 = np.zeros_like(self.G)
for i in xrange(128):
a = self.preprocess(rnd_warp(img))
A = cv2.dft(a, flags=cv2.DFT_COMPLEX_OUTPUT)
self.H1 += cv2.mulSpectrums(self.G, A, 0, conjB=True)
self.H2 += cv2.mulSpectrums( A, A, 0, conjB=True)
self.update_kernel()
self.update(frame)
def update(self, frame, rate = 0.125):
(x, y), (w, h) = self.pos, self.size
self.last_img = img = cv2.getRectSubPix(frame, (w, h), (x, y))
img = self.preprocess(img)
self.last_resp, (dx, dy), self.psr = self.correlate(img)
self.good = self.psr > 8.0
if not self.good:
return
self.pos = x+dx, y+dy
self.last_img = img = cv2.getRectSubPix(frame, (w, h), self.pos)
img = self.preprocess(img)
A = cv2.dft(img, flags=cv2.DFT_COMPLEX_OUTPUT)
H1 = cv2.mulSpectrums(self.G, A, 0, conjB=True)
H2 = cv2.mulSpectrums( A, A, 0, conjB=True)
self.H1 = self.H1 * (1.0-rate) + H1 * rate
self.H2 = self.H2 * (1.0-rate) + H2 * rate
self.update_kernel()
@property
def state_vis(self):
f = cv2.idft(self.H, flags=cv2.DFT_SCALE | cv2.DFT_REAL_OUTPUT )
h, w = f.shape
f = np.roll(f, -h//2, 0)
f = np.roll(f, -w//2, 1)
kernel = np.uint8( (f-f.min()) / f.ptp()*255 )
resp = self.last_resp
resp = np.uint8(np.clip(resp/resp.max(), 0, 1)*255)
vis = np.hstack([self.last_img, kernel, resp])
return vis
def draw_state(self, vis, i):
(x, y), (w, h) = self.pos, self.size
x1, y1, x2, y2 = int(x-0.5*w), int(y-0.5*h), int(x+0.5*w), int(y+0.5*h)
cv2.rectangle(vis, (x1, y1), (x2, y2), (0, 0, 255))
if self.good:
cv2.circle(vis, (int(x), int(y)), 2, (0, 0, 255), -1)
win32api.SetCursorPos((int(screenWidth-4*x),int(screenHeight-2*y)))
i=0
else:
cv2.line(vis, (x1, y1), (x2, y2), (0, 0, 255))
cv2.line(vis, (x2, y1), (x1, y2), (0, 0, 255))
while(i==0):
#win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,int(2*x),int(2*y),0,0)
#win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,int(2*x),int(2*y),0,0)
i=1
draw_str(vis, (x1, y2+16), 'PSR: %.2f' % self.psr)
def preprocess(self, img):
img = np.log(np.float32(img)+1.0)
img = (img-img.mean()) / (img.std()+eps)
return img*self.win
def correlate(self, img):
C = cv2.mulSpectrums(cv2.dft(img, flags=cv2.DFT_COMPLEX_OUTPUT), self.H, 0, conjB=True)
resp = cv2.idft(C, flags=cv2.DFT_SCALE | cv2.DFT_REAL_OUTPUT)
h, w = resp.shape
_, mval, _, (mx, my) = cv2.minMaxLoc(resp)
side_resp = resp.copy()
cv2.rectangle(side_resp, (mx-5, my-5), (mx+5, my+5), 0, -1)
smean, sstd = side_resp.mean(), side_resp.std()
psr = (mval-smean) / (sstd+eps)
return resp, (mx-w//2, my-h//2), psr
def update_kernel(self):
self.H = divSpec(self.H1, self.H2)
self.H[...,1] *= -1
class App:
def __init__(self, video_src, paused = False):
self.cap = video.create_capture(0)
_, self.frame = self.cap.read()
cv2.imshow('frame', self.frame)
self.rect_sel = RectSelector('frame', self.onrect)
self.trackers = []
self.paused = paused
def __exit__(self, video_src, exc_value, traceback):
for file in self.files:
os.unlink(file)
def onrect(self, rect):
frame_gray = cv2.cvtColor(self.frame, cv2.COLOR_BGR2GRAY)
tracker = MOSSE(frame_gray, rect)
self.trackers.append(tracker)
def run(self,one):
global required_area
global max_area
coun_max=0
while True:
if not self.paused:
ret, self.frame = self.cap.read()
if not ret:
break
frame_gray = cv2.cvtColor(self.frame, cv2.COLOR_BGR2GRAY)
blurgray = cv2.GaussianBlur(frame_gray,(5,5),0)
ret,thresh1 = cv2.threshold(blurgray,70,255,cv2.THRESH_BINARY_INV +cv2.THRESH_OTSU)
#cv2.imshow('thresh',thresh1)
image,contours, hierarchy = cv2.findContours(thresh1,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
drawing = np.zeros(self.frame.shape,np.uint8)
max_area=0
ci=0
for i in range(len(contours)):
cnt=contours[i]
area = cv2.contourArea(cnt)
#setting required_area
if(area>max_area):
max_area=area
if(max_area>required_area):
required_area = max_area
ci=i
cnt=contours[ci]
hull = cv2.convexHull(cnt)
moments = cv2.moments(cnt)
if moments['m00']!=0:
cx = int(moments['m10']/moments['m00']) # cx = M10/M00
cy = int(moments['m01']/moments['m00']) # cy = M01/M00
centr=(cx,cy)
cv2.circle(self.frame,centr,5,[0,0,255],2)
cv2.drawContours(drawing,[cnt],0,(0,255,0),2)
cv2.drawContours(drawing,[hull],0,(0,0,255),2)
cnt = cv2.approxPolyDP(cnt,0.01*cv2.arcLength(cnt,True),True)
hull = cv2.convexHull(cnt,returnPoints = False)
if(1):
defects = cv2.convexityDefects(cnt,hull)
mind=0
maxd=0
coun=0
for i in range(defects.shape[0]):
coun=coun+1
s,e,f,d = defects[i,0]
start = tuple(cnt[s][0])
end = tuple(cnt[e][0])
far = tuple(cnt[f][0])
dist = cv2.pointPolygonTest(cnt,centr,True)
cv2.line(self.frame,start,end,[0,255,0],2)
cv2.circle(self.frame,far,5,[0,0,255],-1)
cv2.circle(self.frame,start,5,[255,0,0],-1)
i=0
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(self.frame,str(coun),(0,40), font, 1,(0,0,0),2)
cv2.putText(self.frame,str(coun_max),(0,80), font, 1,(0,0,0),2)
(x, y) = win32api.GetCursorPos()
if(1):
'''if(max_area<required_area*4/5):
(x, y) = win32api.GetCursorPos()
if(y<screenHeight/2):
win32api.mouse_event(win32con.MOUSEEVENTF_WHEEL, x, y, 30, 0)
else:
win32api.mouse_event(win32con.MOUSEEVENTF_WHEEL, x, y, -30, 0)'''
if(coun == coun_max+1):
while(one==0):
(x,y) = win32api.GetCursorPos()
win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN,int(x),int(y),0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP,int(x),int(y),0,0)
end = 1
one=1
if(coun == coun_max-1):
while(one==0):
(x,y) = win32api.GetCursorPos()
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,int(x),int(y),0,0)
#win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,int(x),int(y),0,0)
end = 1
one=1
if(coun == coun_max):
(x,y) = win32api.GetCursorPos()
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,int(x),int(y),0,0)
end = 0
one=0
#else:
#pyautogui.hotkey('win','m')
for tracker in self.trackers:
tracker.update(frame_gray)
vis = self.frame.copy()
for tracker in self.trackers:
tracker.draw_state(vis,0)
if len(self.trackers) > 0:
cv2.imshow('tracker state', self.trackers[-1].state_vis)
self.rect_sel.draw(vis)
cv2.imshow('frame', vis)
ch = cv2.waitKey(10)
if ch == ord('s'):
coun_max = coun
a=cx-80
b=cy-80
c=cx+80
d=cy+80
frame_gray = cv2.cvtColor(self.frame, cv2.COLOR_BGR2GRAY)
tracker = MOSSE(frame_gray,(a,b,c,d))
self.trackers.append(tracker)
if ch == 27:
self.cap.release()
break
if ch == ord(' '):
self.paused = not self.paused
if ch == ord('c'):
self.trackers = []
coun_max = 0
if ch == ord('g'):
App1(video_src, paused = '--pause' in opts).run(one)
break
class App1:
def __init__(self, video_src, paused = False):
self.cap = video.create_capture(1)
_, self.frame = self.cap.read()
cv2.imshow('frame', self.frame)
self.rect_sel = RectSelector('frame', self.onrect)
self.trackers = []
self.paused = paused
def onrect(self, rect):
frame_gray = cv2.cvtColor(self.frame, cv2.COLOR_BGR2GRAY)
tracker = MOSSE(frame_gray, rect)
self.trackers.append(tracker)
def run(self,one):
global required_area
global max_area
coun_max=0
while True:
if not self.paused:
ret, self.frame = self.cap.read()
if not ret:
break
frame_gray = cv2.cvtColor(self.frame, cv2.COLOR_BGR2GRAY)
blurgray = cv2.GaussianBlur(frame_gray,(5,5),0)
ret,thresh1 = cv2.threshold(blurgray,70,255,cv2.THRESH_BINARY_INV +cv2.THRESH_OTSU)
#cv2.imshow('thresh',thresh1)
image,contours, hierarchy = cv2.findContours(thresh1,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
drawing = np.zeros(self.frame.shape,np.uint8)
max_area=0
ci=0
for i in range(len(contours)):
cnt=contours[i]
area = cv2.contourArea(cnt)
#setting required_area
if(area>max_area):
max_area=area
if(max_area>required_area):
required_area = max_area
ci=i
cnt=contours[ci]
hull = cv2.convexHull(cnt)
moments = cv2.moments(cnt)
if moments['m00']!=0:
cx = int(moments['m10']/moments['m00']) # cx = M10/M00
cy = int(moments['m01']/moments['m00']) # cy = M01/M00
centr=(cx,cy)
cv2.circle(self.frame,centr,5,[0,0,255],2)
cv2.drawContours(drawing,[cnt],0,(0,255,0),2)
cv2.drawContours(drawing,[hull],0,(0,0,255),2)
cnt = cv2.approxPolyDP(cnt,0.01*cv2.arcLength(cnt,True),True)
hull = cv2.convexHull(cnt,returnPoints = False)
if(1):
defects = cv2.convexityDefects(cnt,hull)
mind=0
maxd=0
coun=0
for i in range(defects.shape[0]):
coun=coun+1
s,e,f,d = defects[i,0]
start = tuple(cnt[s][0])
end = tuple(cnt[e][0])
far = tuple(cnt[f][0])
dist = cv2.pointPolygonTest(cnt,centr,True)
cv2.line(self.frame,start,end,[0,255,0],2)
cv2.circle(self.frame,far,5,[0,0,255],-1)
cv2.circle(self.frame,start,5,[255,0,0],-1)
i=0
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(self.frame,str(coun),(0,40), font, 1,(0,0,0),2)
cv2.putText(self.frame,str(coun_max),(0,80), font, 1,(0,0,0),2)
(x, y) = win32api.GetCursorPos()
if(1):
if(coun == coun_max+1):
while(one==0):
(x,y) = win32api.GetCursorPos()
win32api.mouse_event(win32con.MOUSEEVENTF_WHEEL, x, y, 60, 0)
end = 1
one=1
if(coun == coun_max-1):
while(one==0):
(x,y) = win32api.GetCursorPos()
win32api.mouse_event(win32con.MOUSEEVENTF_WHEEL, x, y, -60, 0)
end = 1
one=1
if(coun == coun_max):
(x,y) = win32api.GetCursorPos()
#win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,int(screenWidth -4*x),int(2*y),0,0)
end = 0
one=0
#else:
#pyautogui.hotkey('win','m')
for tracker in self.trackers:
tracker.update(frame_gray)
vis = self.frame.copy()
for tracker in self.trackers:
tracker.draw_state(vis,0)
if len(self.trackers) > 0:
cv2.imshow('tracker state', self.trackers[-1].state_vis)
self.rect_sel.draw(vis)
cv2.imshow('frame', vis)
ch = cv2.waitKey(10)
if ch == ord('s'):
coun_max = coun
a=cx-80
b=cy-80
c=cx+80
d=cy+80
frame_gray = cv2.cvtColor(self.frame, cv2.COLOR_BGR2GRAY)
tracker = MOSSE(frame_gray,(a,b,c,d))
self.trackers.append(tracker)
if ch == 27:
self.cap.release()
break
if ch == ord(' '):
self.paused = not self.paused
if ch == ord('c'):
self.trackers = []
coun_max = 0
if ch == ord('g'):
print "__chirayu__"
App1(video_src, paused = '--pause' in opts).run(one)
print "__harshit__"
print __doc__
import sys, getopt
opts, args = getopt.getopt(sys.argv[1:], '', ['pause'])
opts = dict(opts)
try: video_src = args[0]
except: video_src = '0'
App(video_src, paused = '--pause' in opts).run(one)
cv2.destroyAllWindows()