forked from uoip/KCFpy
-
Notifications
You must be signed in to change notification settings - Fork 7
/
kcftracker_cnn.py
329 lines (264 loc) · 11.3 KB
/
kcftracker_cnn.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
import numpy as np
import cv2
import fhog
import time
xrange = range
# ffttools
def fftd(img, backwards=False):
# shape of img can be (m,n), (m,n,1) or (m,n,2)
# in my test, fft provided by numpy and scipy are slower than cv2.dft
return cv2.dft(np.float32(img), flags = ((cv2.DFT_INVERSE | cv2.DFT_SCALE) if backwards else cv2.DFT_COMPLEX_OUTPUT)) # 'flags =' is necessary!
def real(img):
return img[:,:,0]
def imag(img):
return img[:,:,1]
def complexMultiplication(a, b):
res = np.zeros(a.shape, a.dtype)
res[:,:,0] = a[:,:,0]*b[:,:,0] - a[:,:,1]*b[:,:,1]
res[:,:,1] = a[:,:,0]*b[:,:,1] + a[:,:,1]*b[:,:,0]
return res
def complexDivision(a, b):
res = np.zeros(a.shape, a.dtype)
divisor = 1. / (b[:,:,0]**2 + b[:,:,1]**2)
res[:,:,0] = (a[:,:,0]*b[:,:,0] + a[:,:,1]*b[:,:,1]) * divisor
res[:,:,1] = (a[:,:,1]*b[:,:,0] + a[:,:,0]*b[:,:,1]) * divisor
return res
def rearrange(img):
#return np.fft.fftshift(img, axes=(0,1))
assert(img.ndim==2)
img_ = np.zeros(img.shape, img.dtype)
xh, yh = img.shape[1]//2, img.shape[0]//2
img_[0:yh,0:xh], img_[yh:2*yh,xh:2*xh] = img[yh:2*yh,xh:2*xh], img[0:yh,0:xh]
img_[0:yh,xh:2*xh], img_[yh:2*yh,0:xh] = img[yh:2*yh,0:xh], img[0:yh,xh:2*xh]
return img_
# recttools
def x2(rect):
return rect[0] + rect[2]
def y2(rect):
return rect[1] + rect[3]
def limit(rect, limit):
if(rect[0]+rect[2] > limit[0]+limit[2]):
rect[2] = limit[0]+limit[2]-rect[0]
if(rect[1]+rect[3] > limit[1]+limit[3]):
rect[3] = limit[1]+limit[3]-rect[1]
if(rect[0] < limit[0]):
rect[2] -= (limit[0]-rect[0])
rect[0] = limit[0]
if(rect[1] < limit[1]):
rect[3] -= (limit[1]-rect[1])
rect[1] = limit[1]
if(rect[2] < 0):
rect[2] = 0
if(rect[3] < 0):
rect[3] = 0
assert(rect[0] >= limit[0])
assert(rect[1] >= limit[1])
assert(rect[0] + rect[2] <= limit[0] + limit[2])
assert(rect[1]+ rect[3] <= limit[1] + limit[3])
return rect
def getBorder(original, limited):
res = [0,0,0,0]
res[0] = limited[0] - original[0]
res[1] = limited[1] - original[1]
res[2] = x2(original) - x2(limited)
res[3] = y2(original) - y2(limited)
assert(np.all(np.array(res) >= 0))
return res
def subwindow(img, window, borderType=cv2.BORDER_CONSTANT):
cutWindow = [x for x in window]
cutWindow = limit(cutWindow, [0,0,img.shape[1],img.shape[0]]) # modify cutWindow
assert(cutWindow[2]>0 and cutWindow[3]>0)
border = getBorder(window, cutWindow)
res = img[cutWindow[1]:cutWindow[1]+cutWindow[3], cutWindow[0]:cutWindow[0]+cutWindow[2]]
# #print border, img.shape, window
if(border != [0,0,0,0]):
res = cv2.copyMakeBorder(res, border[1], border[3], border[0], border[2], borderType)
return res
# KCF tracker
class KCFTracker:
def __init__(self, fixed_window=True, multiscale=False, ftype = 'None'):
self.lambdar = 0.0001 # regularization
self.padding = 2.5 # extra area surrounding the target
self.output_sigma_factor = 0.125 # bandwidth of gaussian target
self.ftype = ftype
self.i=0
# raw gray-scale image # aka CSK tracker
self.interp_factor = 0.075
self.sigma = 0.2
self.cell_size = 1
if(multiscale):
self.template_size = 96 # template size
self.scale_step = 1.05 # scale step for multi-scale estimation
self.scale_weight = 0.96 # to downweight detection scores of other scales for added stability
elif(fixed_window):
self.template_size = 96
self.scale_step = 1
else:
self.template_size = 1
self.scale_step = 1
self._tmpl_sz = [0,0] # cv::Size, [width,height] #[int,int]
self._roi = [0.,0.,0.,0.] # cv::Rect2f, [x,y,width,height] #[float,float,float,float]
self.size_patch = [0,0,0] #[int,int,int]
self._scale = 1. # float
self._alphaf = None # numpy.ndarray (size_patch[0], size_patch[1], 2)
self._alpha_init = None
self._prob = None # numpy.ndarray (size_patch[0], size_patch[1], 2)
self._tmpl = None # numpy.ndarray raw: (size_patch[0], size_patch[1]) hog: (size_patch[2], size_patch[0]*size_patch[1])
self.hann = None # numpy.ndarray raw: (size_patch[0], size_patch[1]) hog: (size_patch[2], size_patch[0]*size_patch[1])
def subPixelPeak(self, left, center, right):
divisor = 2*center - right - left #float
return (0 if abs(divisor)<1e-3 else 0.5*(right-left)/divisor)
def createHanningMats(self):
start = time.time()
hann2t, hann1t = np.ogrid[0:self.size_patch[0], 0:self.size_patch[1]]
#print(hann2t.shape)
hann1t = 0.5 * (1 - np.cos(2*np.pi*hann1t/(self.size_patch[1]-1)))
hann2t = 0.5 * (1 - np.cos(2*np.pi*hann2t/(self.size_patch[0]-1)))
hann2d = hann2t * hann1t
#print("hann", hann1t.shape,hann2t.shape, hann2d.shape)
self.hann = hann2d
self.hann = self.hann.astype(np.float32)
#print("Final Hann", self.hann.shape)
#print("createhann_start %s" %(time.time()-start))
def createGaussianPeak(self, sizey, sizex):
syh, sxh = sizey/2, sizex/2
output_sigma = np.sqrt(sizex*sizey) / self.padding * self.output_sigma_factor
mult = -0.5 / (output_sigma*output_sigma)
y, x = np.ogrid[0:sizey, 0:sizex]
y, x = (y-syh)**2, (x-sxh)**2
res = np.exp(mult * (y+x))
return fftd(res)
def gaussianCorrelation(self, x1, x2):
start = time.time()
c = cv2.mulSpectrums(fftd(x1), fftd(x2), 0, conjB = True) # 'conjB=' is necessary!
c = fftd(c, True)
c = real(c)
c = rearrange(c)
if(x1.ndim==3 and x2.ndim==3):
d = (np.sum(x1[:,:,0]*x1[:,:,0]) + np.sum(x2[:,:,0]*x2[:,:,0]) - 2.0*c) / (self.size_patch[0]*self.size_patch[1]*self.size_patch[2])
elif(x1.ndim==2 and x2.ndim==2):
d = (np.sum(x1*x1) + np.sum(x2*x2) - 2.0*c) / (self.size_patch[0]*self.size_patch[1]*self.size_patch[2])
d = d * (d>=0)
d = np.exp(-d / (self.sigma*self.sigma))
#print(d.shape)
#print("gaussianCorrelation %s" %(time.time()-start))
return d
def getFeatures(self, image, inithann, scale_adjust=1.0):
start = time.time()
extracted_roi = [0,0,0,0] #[int,int,int,int]
cx = self._roi[0] + self._roi[2]/2 #float
cy = self._roi[1] + self._roi[3]/2 #float
if(inithann):
padded_w = self._roi[2] * self.padding
padded_h = self._roi[3] * self.padding
if(self.template_size > 1):
if(padded_w >= padded_h):
self._scale = padded_w / float(self.template_size)
else:
self._scale = padded_h / float(self.template_size)
self._tmpl_sz[0] = int(padded_w / self._scale)
self._tmpl_sz[1] = int(padded_h / self._scale)
else:
self._tmpl_sz[0] = int(padded_w)
self._tmpl_sz[1] = int(padded_h)
self._scale = 1.
self._tmpl_sz[0] = int(self._tmpl_sz[0]) / 2 * 2
self._tmpl_sz[1] = int(self._tmpl_sz[1]) / 2 * 2
extracted_roi[2] = int(scale_adjust * self._scale * self._tmpl_sz[0])
extracted_roi[3] = int(scale_adjust * self._scale * self._tmpl_sz[1])
extracted_roi[0] = int(cx - extracted_roi[2]/2)
extracted_roi[1] = int(cy - extracted_roi[3]/2)
extracted_roi = limit(extracted_roi, [0,0, image.shape[1], image.shape[0]])
z = subwindow(image, extracted_roi, cv2.BORDER_REPLICATE)
if(z.shape[1]!=self._tmpl_sz[0] or z.shape[0]!=self._tmpl_sz[1]):
z = cv2.resize(z, tuple(map(int,self._tmpl_sz)))
if self.ftype == 'cnn':
FeaturesMap = self.feat(z)
else:
if(z.ndim==3 and z.shape[2]==3):
FeaturesMap = cv2.cvtColor(z, cv2.COLOR_BGR2GRAY)
#FeaturesMap = self.feat(z)
#cv2.imwrite("gray/gray%d.jpg" %self.i,FeaturesMap1)
#cv2.imwrite("feat/feat%d.jpg" %self.i,FeaturesMap)
#self.i+=1
#print(FeaturesMap.shape,z.shape) # z:(size_patch[0], size_patch[1], 3) FeaturesMap:(size_patch[0], size_patch[1]) #np.int8 #0~255
elif(z.ndim==2):
FeaturesMap = self.feat(z)
#print(FeaturesMap.shape,z.shape) #(size_patch[0], size_patch[1]) #np.int8 #0~255
FeaturesMap = FeaturesMap.astype(np.float32) / 255.0 - 0.5
self.size_patch = [z.shape[0], z.shape[1], 1]
if(inithann):
self.createHanningMats() # createHanningMats need size_patch
FeaturesMap = self.hann * FeaturesMap
#print("Final feature_map", FeaturesMap.shape)
#print("get_Features %s" %(time.time()-start))
return FeaturesMap
def detect(self, z, x):
k = self.gaussianCorrelation(x, z)
res = real(fftd(complexMultiplication(self._alphaf, fftd(k)), True))
_, pv, _, pi = cv2.minMaxLoc(res) # pv:float pi:tuple of int
p = [float(pi[0]), float(pi[1])] # cv::Point2f, [x,y] #[float,float]
if(pi[0]>0 and pi[0]<res.shape[1]-1):
p[0] += self.subPixelPeak(res[pi[1],pi[0]-1], pv, res[pi[1],pi[0]+1])
if(pi[1]>0 and pi[1]<res.shape[0]-1):
p[1] += self.subPixelPeak(res[pi[1]-1,pi[0]], pv, res[pi[1]+1,pi[0]])
p[0] -= res.shape[1] / 2.
p[1] -= res.shape[0] / 2.
return p, pv
def train(self, x, train_interp_factor):
k = self.gaussianCorrelation(x, x)
alphaf = complexDivision(self._prob, fftd(k)+self.lambdar)
# self._alphaf = alphaf
self._tmpl = (1-train_interp_factor)*self._tmpl + train_interp_factor*x
if self._alphaf is not None:
# self._alphaf = 0.5*(1-train_interp_factor)*self._alpha_init + 0.5*(1-train_interp_factor)*self._alphaf + train_interp_factor*alphaf
self._alphaf = (1-train_interp_factor)*self._alphaf + train_interp_factor*alphaf
# self._alphaf = (1-train_interp_factor)*self._alphaf + 0.5*train_interp_factor*alphaf + 0.5*train_interp_factor*self._alpha_init
else:
self._alphaf = np.zeros((self.size_patch[0], self.size_patch[1], 2), np.float32)
self._alphaf = alphaf
self._alpha_init = alphaf
def init(self, roi, image, feat = None):
self._roi = list(map(float, roi))
self.feat = feat
assert(roi[2]>0 and roi[3]>0)
self._tmpl = self.getFeatures(image, 1)
self._prob = self.createGaussianPeak(self.size_patch[0], self.size_patch[1])
self._alphaf = np.zeros((self.size_patch[0], self.size_patch[1], 2), np.float32)
self.train(self._tmpl, 1.0)
def update(self, image, feat = None):
self.feat = feat
if(self._roi[0]+self._roi[2] <= 0): self._roi[0] = -self._roi[2] + 1
if(self._roi[1]+self._roi[3] <= 0): self._roi[1] = -self._roi[2] + 1
if(self._roi[0] >= image.shape[1]-1): self._roi[0] = image.shape[1] - 2
if(self._roi[1] >= image.shape[0]-1): self._roi[1] = image.shape[0] - 2
cx = self._roi[0] + self._roi[2]/2.
cy = self._roi[1] + self._roi[3]/2.
loc, peak_value = self.detect(self._tmpl, self.getFeatures(image, 0, 1.0))
if(self.scale_step != 1):
# Test at a smaller _scale
new_loc1, new_peak_value1 = self.detect(self._tmpl, self.getFeatures(image, 0, 1.0/self.scale_step))
# Test at a bigger _scale
new_loc2, new_peak_value2 = self.detect(self._tmpl, self.getFeatures(image, 0, self.scale_step))
if(self.scale_weight*new_peak_value1 > peak_value and new_peak_value1>new_peak_value2):
loc = new_loc1
peak_value = new_peak_value1
self._scale /= self.scale_step
self._roi[2] /= self.scale_step
self._roi[3] /= self.scale_step
elif(self.scale_weight*new_peak_value2 > peak_value):
loc = new_loc2
peak_value = new_peak_value2
self._scale *= self.scale_step
self._roi[2] *= self.scale_step
self._roi[3] *= self.scale_step
self._roi[0] = cx - self._roi[2]/2.0 + loc[0]*self.cell_size*self._scale
self._roi[1] = cy - self._roi[3]/2.0 + loc[1]*self.cell_size*self._scale
if(self._roi[0] >= image.shape[1]-1): self._roi[0] = image.shape[1] - 1
if(self._roi[1] >= image.shape[0]-1): self._roi[1] = image.shape[0] - 1
if(self._roi[0]+self._roi[2] <= 0): self._roi[0] = -self._roi[2] + 2
if(self._roi[1]+self._roi[3] <= 0): self._roi[1] = -self._roi[3] + 2
assert(self._roi[2]>0 and self._roi[3]>0)
x = self.getFeatures(image, 0, 1.0)
self.train(x, self.interp_factor)
return self._roi