-
Notifications
You must be signed in to change notification settings - Fork 1
/
ps7.py
323 lines (239 loc) · 15 KB
/
ps7.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
"""Problem Set 7: Particle Filter Tracking."""
import numpy as np
import cv2
from numpy.random import uniform,randn
from numpy.linalg import norm
import os
# I/O directories
input_dir = "input"
output_dir = "output"
# Assignment code
class ParticleFilter(object):
"""A particle filter tracker, encapsulating state, initialization and update methods. Refer to the method
run_particle_filter( ) in experiment.py to understand how this class and methods work.
"""
def __init__(self, frame, template, **kwargs):
"""Initializes the particle filter object.
The main components of your particle filter should at least be:
- self.particles (numpy.array): Here you will store your particles. This should be a N x 2 array where
N = self.num_particles. This component is used by the autograder so make sure
you define it appropriately.
- self.weights (numpy.array): Array of N weights, one for each particle.
Hint: initialize them with a uniform normalized distribution (equal weight for
each one). Required by the autograder.
- self.template (numpy.array): Cropped section of the first video frame that will be used as the template to
track.
- self.frame (numpy.array): Current video frame from cv2.VideoCapture().
Args:
frame (numpy.array): color BGR uint8 image of initial video frame, values in [0, 255].
template (numpy.array): color BGR uint8 image of patch to track, values in [0, 255].
kwargs: keyword arguments needed by particle filter model, including:
- num_particles (int): number of particles.
- sigma_mse (float): sigma value used in the similarity measure.
- sigma_dyn (float): sigma value that can be used when adding gaussian noise to u and v.
- template_rect (dict): Template coordinates with x, y, width, and height values.
"""
self.num_particles = kwargs.get('num_particles')
self.sigma_exp = kwargs.get('sigma_exp')
self.sigma_dyn = kwargs.get('sigma_dyn')
self.template_rect = kwargs.get('template_coords')
self.template = template
self.frame = frame
# Todo: Initialize your particles array. Read the docstring.
self.particles = np.column_stack( (uniform(template.shape[0]/2, frame.shape[0]-template.shape[0]/2, size=self.num_particles)#.reshape((self.num_particles, 1))
,uniform(template.shape[1]/2, frame.shape[1]-template.shape[1]/2, size=self.num_particles)#.reshape((self.num_particles, 1))
)
)
# Todo: Initialize your weights array. Read the docstring.
self.weights = np.ones(self.num_particles) /self.num_particles
def get_particles(self):
"""Returns the current particles state.
This method is used by the autograder. Do not modify this function.
Returns:
numpy.array: particles data structure.
"""
return self.particles
def get_weights(self):
"""Returns the current particle filter's weights.
This method is used by the autograder. Do not modify this function.
Returns:
numpy.array: weights data structure.
"""
return self.weights
def predict(self,particles, sigma_dyn, frame, template):
newP = particles + randn(len(particles),2)*sigma_dyn
newP[newP[:,0] > frame.shape[0]-template.shape[0]/2-1, 0] = frame.shape[0]-template.shape[0]/2-1
newP[newP[:,0] < template.shape[0]/2+1, 0] = template.shape[0]/2
newP[newP[:,1] > frame.shape[1]-template.shape[1]/2-1, 1] = frame.shape[1]-template.shape[1]/2-1
newP[newP[:,1] < template.shape[1]/2+1, 1] = template.shape[1]/2+1
self.particles = newP
def update(self,particles, weights, frame, sigma_exp, template):
distance = []
for k in range(len(particles)):
lx = int( particles[k,0]-template.shape[0]/2)
ux = int( particles[k,0]-template.shape[0]/2)+ template.shape[0]
ly = int( particles[k,1]-template.shape[1]/2)
uy = int( particles[k,1]-template.shape[1]/2)+ template.shape[1]
mse = (np.linalg.norm(frame[lx:ux,ly:uy,1].astype(float) - template[:,:,1].astype(float) )
+np.linalg.norm(frame[lx:ux,ly:uy,0].astype(float) - template[:,:,0].astype(float) )
+np.linalg.norm(frame[lx:ux,ly:uy,2].astype(float) - template[:,:,2].astype(float) )
)
distance.append(mse ) #np.sqrt(np.sum((pf - template_grey) **2))
p = np.exp(-np.array(distance)/2.0/(sigma_exp*2)) #+1.e-300
self.weights = p/sum(p)
def resample( self):
s = np.random.choice(self.num_particles,self.num_particles,replace=True,p=self.weights)
self.particles = self.particles[s,]
self.weights = self.weights[s,]/sum(self.weights[s,])
def process(self, frame):
"""Processes a video frame (image) and updates the filter's state.
Implement the particle filter in this method returning None (do not include a return call). This function
should update the particles and weights data structures.
Make sure your particle filter is able to cover the entire area of the image. This means you should address
particles that are close to the image borders.
Args:,
frame (numpy.array): color BGR uint8 image of current video frame, values in [0, 255].
Returns:
None.
"""
self.predict(self.particles, sigma_dyn=self.sigma_dyn , frame=self.frame, template=self.template )
self.update(self.particles, self.weights, frame, sigma_exp=self.sigma_exp, template=self.template )
self.resample( )
def render(self, frame_in):
"""Visualizes current particle filter state.
This method may not be called for all frames, so don't do any model updates here!
These steps will calculate the weighted mean. The resulting values should represent the
tracking window center point.
In order to visualize the tracker's behavior you will need to overlay each successive
frame with the following elements:
- Every particle's (u, v) location in the distribution should be plotted by drawing a
colored dot point on the image. Remember that this should be the center of the window,
not the corner.
- Draw the rectangle of the tracking window associated with the Bayesian estimate for
the current location which is simply the weighted mean of the (u, v) of the particles.
- Finally we need to get some sense of the standard deviation or spread of the distribution.
First, find the distance of every particle to the weighted mean. Next, take the weighted
sum of these distances and plot a circle centered at the weighted mean with this radius.
This function should work for all particle filters in this problem set.
Args:
frame_in (numpy.array): copy of frame to render the state of the particle filter.
"""
u_weighted_mean = 0
v_weighted_mean = 0
for i in range(len(self.particles)):
u_weighted_mean += self.particles[i, 0] * self.weights[i]
v_weighted_mean += self.particles[i, 1] * self.weights[i]
d = []
for j in range( len(self.particles)):
cv2.circle(frame_in, (int(self.particles[j, 1]), int(self.particles[j, 0]) ), 1, (0, 255, 0), -1)
d.append(np.linalg.norm( self.particles[j, :]- np.array([u_weighted_mean,v_weighted_mean]) ) * self.weights[j]) #find the distance of every particle to the weighted mean
# 'Draw the rectangle of the tracking window associated with the Bayesian estimate for
# the current location which is simply the weighted mean of the (u, v) of the self.particles. '''
cv2.circle(frame_in, (int(v_weighted_mean), int(u_weighted_mean) ), 10, (0,0,255), 1)
cv2.rectangle(frame_in
, (int(v_weighted_mean)-self.template.shape[1]/2, int(u_weighted_mean)-self.template.shape[0]/2)
, (int(v_weighted_mean)+self.template.shape[1]/2, int(u_weighted_mean)+self.template.shape[0]/2)
, (0,0,255), 2)
print 'circle radius', sum(d)
cv2.circle(frame_in
, (int(v_weighted_mean), int(u_weighted_mean) )
, int(sum(d))
, (0,0,255), 1)
class AppearanceModelPF(ParticleFilter):
"""A variation of particle filter tracker that updates its appearance model over time."""
def __init__(self, frame, template, **kwargs):
"""Initializes the appearance model particle filter object (parameters are the same as ParticleFilter).
The documentation for this class is the same as the ParticleFilter above. There is one element that is added
called alpha which is explained in the problem set documentation. By calling super(...) all the elements used
in ParticleFilter will be inherited so you do not have to declare them again.
"""
super(AppearanceModelPF, self).__init__(frame, template, **kwargs) # call base class constructor
self.alpha = kwargs.get('alpha') # required by the autograder
def process(self, frame):
"""Processes a video frame (image) and updates the filter's state.
This process is also inherited from ParticleFilter. Depending on your implementation, you may comment out this
function and use helper methods that implement the "Appearance Model" procedure.
Args:
frame (numpy.array): color BGR uint8 image of current video frame, values in [0, 255].
Returns:
None.
"""
self.predict(self.particles, sigma_dyn=self.sigma_dyn , frame=self.frame, template=self.template )
self.update(self.particles, self.weights, frame, sigma_exp=self.sigma_exp, template=self.template )
temp_center = self.particles[np.argmax(self.weights ), :]
temp_template = frame[(int(temp_center[0])-self.template.shape[0]/2) : (int(temp_center[0])-self.template.shape[0]/2+self.template.shape[0])
,(int(temp_center[1])-self.template.shape[1]/2) : (int(temp_center[1])-self.template.shape[1]/2+self.template.shape[1])]
self.template = self.alpha*temp_template + (1-self.alpha) *self.template
self.resample()
class MeanShiftLitePF(ParticleFilter):
"""A variation of particle filter tracker that uses the color distribution of the patch."""
def __init__(self, frame, template, **kwargs):
"""Initializes the Mean Shift Lite particle filter object (parameters are the same as ParticleFilter).
The documentation for this class is the same as the ParticleFilter above. There is one element that is added
called alpha which is explained in the problem set documentation. By calling super(...) all the elements used
in ParticleFilter will be inherited so you don't have to declare them again."""
super(MeanShiftLitePF, self).__init__(frame, template, **kwargs) # call base class constructor
self.num_bins = kwargs.get('hist_bins_num', 8) # required by the autograder
# If you want to add more parameters, make sure you set a default value so that
# your test doesn't fail the autograder because of an unknown or None value.
#
# The way to do it is:
# self.some_parameter_name = kwargs.get('parameter_name', default_value)
def update_ms(self,particles, weights, frame, sigma_exp, template):
distance = []
frame_green =frame[:,:,1].astype(float)
template_green = template[:,:,1].astype(float)
for k in range(len(particles)):
lx = int( particles[k,0]-template.shape[0]/2)
ux = int( particles[k,0]-template.shape[0]/2)+ template.shape[0]
ly = int( particles[k,1]-template.shape[1]/2)
uy = int( particles[k,1]-template.shape[1]/2)+ template.shape[1]
pf = frame [ lx:ux
,ly:uy ]
pf0 = cv2.calcHist([pf],[0],None,[8],[0,256])
pf1 = cv2.calcHist([pf],[1],None,[8],[0,256])
pf2 = cv2.calcHist([pf],[2],None,[8],[0,256])
tp0 = cv2.calcHist([template],[0],None,[8],[0,256])
tp1 = cv2.calcHist([template],[1],None,[8],[0,256])
tp2 = cv2.calcHist([template],[2],None,[8],[0,256])
pf_all = np.concatenate((pf0,pf1,pf2), 0)
tp_all = np.concatenate((tp0,tp1,tp2), 0)
num = (pf_all - tp_all) **2
den = pf_all + tp_all
with np.errstate(divide='ignore'):
d = num / den
d[den == 0] = 0
distance.append(np.sum(d))
p = np.exp(-np.array(distance)/2.0/(sigma_exp**2))
self.weights = p/sum(p)
def process(self, frame):
"""Processes a video frame (image) and updates the filter's state.
This process is also inherited from ParticleFilter. Depending on your implementation, you may comment out this
function and use helper methods that implement the "Mean Shift Lite" procedure.
Args:
frame (numpy.array): color BGR uint8 image of current video frame, values in [0, 255].
Returns:
None.
"""
self.predict(self.particles, sigma_dyn=self.sigma_dyn , frame=self.frame, template=self.template )
self.update_ms(self.particles, self.weights, frame, sigma_exp=self.sigma_exp, template=self.template )
self.resample( )
class MDParticleFilter(ParticleFilter):
"""A variation of particle filter tracker that incorporates more dynamics."""
def __init__(self, frame, template, **kwargs):
"""Initializes MD particle filter object (parameters same as ParticleFilter).
The documentation for this class is the same as the ParticleFilter above.
By calling super(...) all the elements used in ParticleFilter will be inherited so you
don't have to declare them again.
"""
super(MDParticleFilter, self).__init__(frame, template, **kwargs)
def process(self, frame):
"""Processes a video frame (image) and updates the filter's state.
This process is also inherited from ParticleFilter. Depending on your implementation, you may comment out this
function and use helper methods that implement the "More Dynamics" procedure.
Args:
frame (numpy.array): color BGR uint8 image of current video frame, values in [0, 255].
Returns:
None.
"""
pass