forked from goktug97/TargetPersonTracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracker.py
executable file
·505 lines (417 loc) · 16.8 KB
/
tracker.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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
#!/usr/bin/env python
"""Tracking target person with ORB features and Lucas Kanade Optical Flow."""
import cv2 # noqa: I201
from detector import Detector # noqa: I201
import numpy as np # noqa: I201
from scipy.spatial import cKDTree # noqa: I201
from sklearn.cluster import MeanShift # noqa: I201
import utils # noqa: I201
def extract_features(frame, n_features, ftype, mask):
"""Extract maximum n_features ORB features from given frame."""
orb = cv2.ORB_create(n_features)
if ftype == 'orb':
kps = orb.detect(frame, mask)
elif ftype == 'good':
pts = cv2.goodFeaturesToTrack(
np.mean(frame, axis=2).astype(np.uint8),
3000, qualityLevel=0.05, minDistance=7,
mask=mask)
if pts is not None:
kps = [cv2.KeyPoint(x=f[0][0], y=f[0][1], _size=20) for f in pts]
else:
return [], []
else:
raise ValueError('Not Implemented')
kps, des = orb.compute(frame, kps)
kps = [[int(kp.pt[0]), int(kp.pt[1])] for kp in kps]
return kps, des
def create_upper_mask(det, frame_shape):
"""Create mask for the upper half of the given detection."""
xmin, ymin, xmax, ymax = det
center_y = (ymax - ymin)//2
mask = np.zeros(frame_shape[:2], dtype=np.uint8)
mask[ymin:center_y, xmin:xmax] = 255
return mask
def find_detection(dets, x, y):
"""Find detection which includes given points."""
"""
Returns first satisfied detection which includes given
points.
"""
for det in dets:
xmin, ymin, xmax, ymax = det
if (xmin < x and xmax > x and
ymin < y and ymax > y):
return xmin, ymin, xmax, ymax
return None
def match_features(des1, des2):
"""Match features with given descriptors with brute force."""
idx1, idx2 = [], []
bf = cv2.BFMatcher(cv2.NORM_HAMMING)
try:
matches = bf.knnMatch(des1, des2, k=2)
except cv2.error:
return idx1, idx2
for match in matches:
if len(match) < 2:
continue
else:
m, n = match
# Lowe's ratio test
if m.distance < 0.75*n.distance:
if m.distance < 32:
idx1.append(m.queryIdx)
idx2.append(m.trainIdx)
return idx1, idx2
def filter_features(kps, des, det, distance):
"""Pick center features only contained by the given detection."""
point_tree = cKDTree(kps)
xmin, ymin, xmax, ymax = det
height = ymax-ymin
n_sps = int(height / (distance*2)) # Number of sample points
x = (xmin+xmax)//2
# choose points around the center vertical line
sample_features = []
sample_descriptors = []
for sp in range(n_sps):
y = int(ymin + (sp * (distance*2)) + distance)
idxs = point_tree.query_ball_point([x, y], distance)
if len(idxs) > 0:
sample_features.extend([point_tree.data[idx] for idx in idxs])
sample_descriptors.extend([des[idx] for idx in idxs])
sample_features = [[int(sample_feature[0]), int(sample_feature[1])]
for sample_feature in sample_features]
return (np.array(sample_features),
np.array(sample_descriptors, dtype=np.uint8))
def reduce_area_of_detection(det, width_multiplier, height_multiplier):
"""Reduce area of detection by width and height multiplier."""
xmin, ymin, xmax, ymax = det
width, height = xmax - xmin, ymax - ymin
width = width*width_multiplier
height = height*height_multiplier
center_x = (xmin+xmax)//2
center_y = (ymin+ymax)//2
xmin, xmax = (int(center_x - width//2),
int(center_x + width//2))
ymin, ymax = (int(center_y - height//2),
int(center_y + height//2))
return xmin, ymin, xmax, ymax
def find_clusters(tracks):
"""Find clusters in tracked points."""
tracks = list(map(lambda x: [x[-1][0], x[-1][1]], tracks))
ms = MeanShift(bandwidth=30, bin_seeding=True)
ms.fit(tracks)
return ms.cluster_centers_
class Tracker(object):
"""Track chosen person using orb features and optical flow."""
def __init__(self, args):
"""Initiliaze the tracker."""
self.detector = Detector(args)
self.args = args
cv2.namedWindow(args.window_name)
# Chosen features for tracking
self.tkps = []
self.tdes = []
# Features around the tracked points
self.kps = []
self.des = []
# tracked points
self.track_points = []
# Lucas Optical Flow Params
self.lk_params = {'winSize': (15, 15),
'maxLevel': 2,
'criteria': (
cv2.TERM_CRITERIA_EPS |
cv2.TERM_CRITERIA_COUNT,
10, 0.03)}
def collect_features(self):
"""Collect features from chosen target for tracking."""
ret, self.frame = self.get_frame()
if self.frame is None:
return True
vis = self.frame.copy()
# Detect people
dets = self.detector.detect(self.frame)
if not len(dets):
return True
utils.draw_detections(vis, dets)
# Find center detection
h, w, _ = self.frame.shape
center_x = w//2
center_y = h//2
det = find_detection(
dets, center_x, center_y)
# Detection might not be centered
if det is None:
return True
# Reduce area of the detection
det = reduce_area_of_detection(
det, self.args.width_multiplier,
self.args.height_multiplier)
# Create mask with detection
mask = create_upper_mask(det, self.frame.shape[:2])
kps, des = extract_features(self.frame,
self.args.n_features,
self.args.ftype,
mask)
if not len(kps):
return True
# Keep center features only
kps, des = filter_features(kps, des, det, self.args.distance)
# Draw features
for i, kp in enumerate(kps):
cv2.circle(vis, tuple(kps[i]), 2, (255, 165, 0), -1)
# check matches to reduce duplicates
# and to collect features evenly
if len(self.tdes) and len(des):
idx1, idx2 = match_features(
np.array(self.tdes, dtype=np.uint8), des)
des = np.delete(des, idx2, 0)
kps = np.delete(kps, idx2, 0)
# Populate the tracked features
if len(des):
self.tkps.extend(kps)
self.tdes.extend(des)
# break if threshold is satisfied
utils.draw_str(vis, (20, 20), 'Features: %d' % len(self.tkps))
if len(self.tkps) > self.args.n_tracked:
return False
if not self.args.no_gui:
cv2.imshow(self.args.window_name, vis)
if cv2.waitKey(1) == 27:
cv2.destroyAllWindows()
return False
self.collection_output()
return True
def initiliaze_target(self, kps=None, des=None):
"""Initiliaze tracking points."""
if kps is None or des is None:
while self.collect_features():
pass
else:
self.tkps, self.tdes = kps, des
def collection_output(self):
"""Output wrapper for collect_features."""
pass
def output_function(self):
"""Overload this function to interact with the tracked points."""
return True
def finish(self):
"""Execute when the tracker is closed."""
pass
def get_frame(self):
"""Get frame from initialized source."""
ret, self.frame = self.cap.read()
return ret, self.frame
def init_track_points(self):
"""Find track points in current frame to start tracking."""
ret, self.frame = self.get_frame()
dets = self.detector.detect(self.frame)
h, w, _ = self.frame.shape
center_x = w//2
center_y = h//2
if len(dets) > 0:
det = find_detection(
dets, center_x, center_y)
if det is None:
return False
det = reduce_area_of_detection(det,
self.args.width_multiplier,
self.args.height_multiplier)
mask = create_upper_mask(det, self.frame.shape)
kps, des = extract_features(self.frame, self.args.n_features,
self.args.ftype, mask)
if len(kps):
idx1, idx2 = match_features(np.array(self.tdes), des)
self.track_points = [
[(kps[idx][0], kps[idx][1])] for idx in idx2]
def track(self):
"""Track points on current frame."""
ret, self.frame = self.get_frame()
if not ret:
self.running = False
return False
# Optical Flow
if len(self.track_points):
self.optical_flow_tracking()
# Add new features
if (not self.frame_idx % self.args.add_every and
len(self.track_points) > self.args.min_tracked):
# Find center of the tracked points
cluster_centers = find_clusters(self.track_points)
# extract features
kps, des = extract_features(
self.frame, self.args.n_features,
self.args.ftype, None)
point_tree = cKDTree(kps)
# Pick features around the center
for cluster_center in cluster_centers:
idxs = point_tree.query_ball_point(
cluster_center, 20)
if len(idxs):
print('Added {} new features'.format(len(idxs)))
self.kps = []
self.des = []
for idx in idxs:
self.kps.append(kps[idx])
self.des.append(des[idx])
self.tkps.append(kps[idx])
self.tdes.append(des[idx])
self.new_points_len += len(idxs)
# Remove false positive features
# NOTE: Might remove wrong features in some situations
if (len(self.track_points) > self.args.min_tracked and
not self.frame_idx % self.args.remove_every):
x, y = self.track_points[-1][-1]
dets = self.detector.detect(self.frame)
# Find bounding box of current target
det = find_detection(dets, int(x), int(y))
if det is not None:
# Create inverse mask
xmin, ymin, xmax, ymax = det
mask = np.ones(self.frame.shape[:2],
dtype=np.uint8) * 255
mask[ymin:ymax, xmin:xmax] = 0
kps, des = extract_features(self.frame,
self.args.n_features,
self.args.ftype, mask)
idx1, idx2 = match_features(np.array(self.tdes), des)
# Remove matches
if len(idx1):
print(
'Removed {} False Positives'.format(
len(idx1)))
for idx in sorted(idx1, reverse=True):
del self.tdes[idx]
del self.tkps[idx]
# Remove duplicates
if not self.frame_idx % self.args.remove_duplicates_every:
if self.new_points_len:
# Match old points with recently added points
idx1, idx2 = match_features(
np.array(self.tdes[:-self.new_points_len]),
np.array(self.tdes[-self.new_points_len:]))
# Remove matches
if len(idx2):
print('Removed {} duplicates'.format(len(idx2)))
for idx in sorted(idx2, reverse=True):
del self.tdes[self.new_points_len + idx]
del self.tkps[self.new_points_len + idx]
self.new_points_len = 0
# Retracking
if (len(self.track_points) < self.args.tracking_thresh or
(not self.frame_idx % self.args.retrack_every and
len(self.track_points) < 100)):
dets = self.retrack()
if not self.args.no_gui:
vis = self.frame.copy()
utils.draw_str(vis, (20, 20),
'track count: %d' % len(self.track_points))
utils.draw_str(vis, (20, 40),
'target features: %d' % len(self.tkps))
# Draw tracked points
for pts in self.track_points:
cv2.polylines(vis, np.array([pts], dtype=np.int32),
False, utils.colors[min(len(pts), 9)])
# Show frame
cv2.imshow(self.args.window_name, vis)
if cv2.waitKey(1) == 27:
self.running = False
return False
if not self.output_function():
self.running = False
return False
self.prev_frame = self.frame.copy()
self.frame_idx += 1
return True
def run(self):
"""Start tracking chosen target."""
# Find tracked points in current frame to start optical flow
if not len(self.tkps):
print('No features to track')
return False
while not len(self.track_points):
self.init_track_points()
self.new_points_len = 0
self.prev_frame = self.frame.copy()
self.frame_idx = 0
self.running = True
while self.running:
self.track()
if not self.args.no_gui:
cv2.destroyAllWindows()
self.finish()
def optical_flow_tracking(self):
"""Lucas Kanade Optical Flow tracking."""
p0 = np.float32(
[tr[-1] for tr in self.track_points]).reshape(-1, 1, 2)
p1, _, _ = cv2.calcOpticalFlowPyrLK(
self.prev_frame, self.frame, p0, None, **self.lk_params)
p0r, _, _ = cv2.calcOpticalFlowPyrLK(
self.frame, self.prev_frame, p1, None, **self.lk_params)
# Keep good features
d = abs(p0-p0r).reshape(-1, 2).max(-1)
good = d < 1
new_tracks = []
for tr, (x, y), good_flag in zip(
self.track_points, p1.reshape(-1, 2), good):
if not good_flag:
continue
tr.append((x, y))
if len(tr) > self.args.track_len:
del tr[0]
new_tracks.append(tr)
self.track_points = new_tracks
def retrack(self):
"""Initiliaze retracking for recognized features."""
# If tracking is not zero activate retracking
# on current area, else it will check features
# for all detections.
dets = self.detector.detect(self.frame)
if len(self.track_points):
x, y = self.track_points[-1][-1]
dets = [find_detection(dets, int(x), int(y))]
if len(dets) and dets[0] is not None:
for det in dets:
det = reduce_area_of_detection(
det,
self.args.width_multiplier,
self.args.height_multiplier)
mask = create_upper_mask(det, self.frame.shape)
kps, des = extract_features(
self.frame, self.args.n_features,
self.args.ftype, mask=mask)
if len(kps):
idx1, idx2 = match_features(
np.array(self.tdes), des)
# Check whether it is larger than specified threshold
# to reduce false positives.
if len(idx1) > self.args.min_match_threshold:
kps, des = filter_features(
[kps[idx] for idx in idx2],
[des[idx] for idx in idx2],
det,
self.args.distance)
for kp in kps:
x, y = kp
self.track_points.append([(x, y)])
else:
# if no detection is found remove noise
if len(self.track_points) < 10:
self.track_points = []
return dets
@property
def cap(self):
if not hasattr(self, '_cap'):
self._cap = cv2.VideoCapture(self.args.input)
# Camera Settings
self._cap.set(cv2.CAP_PROP_FRAME_WIDTH, self.args.camera_width)
self._cap.set(cv2.CAP_PROP_FRAME_HEIGHT, self.args.camera_height)
if (self.args.camera_fps):
self._cap.set(cv2.CAP_PROP_FPS, self.args.camera_fps)
return self._cap
if __name__ == '__main__':
from options import args
tracker = Tracker(args)
tracker.initiliaze_target()
tracker.run()