-
Notifications
You must be signed in to change notification settings - Fork 5
/
tracker.cpp
382 lines (351 loc) · 10.2 KB
/
tracker.cpp
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
#include "tracker.h"
#include "matching.h"
int BaseTrack::_count = 0;
STrack::STrack(cv::Rect_<float>& tlwh, float score_, cv::Mat temp_feat, int buffer_size)
:_tlwh(tlwh)
{
score = score_;
update_features(temp_feat);
}
STrack::~STrack()
{
}
void STrack::update_features(cv::Mat &feat)
{
cv::Mat norm_feat;
cv::normalize(feat, norm_feat, 1.0, 0, cv::NORM_L2);
feat = norm_feat;
curr_feat = feat.clone();
if (smooth_feat.empty()) smooth_feat = feat;
else
{
smooth_feat = alpha * smooth_feat + (1 - alpha)*feat;
features.push_back(feat);
cv::normalize(smooth_feat, norm_feat, 1.0, 0, cv::NORM_L2);
smooth_feat = norm_feat;
}
}
void STrack::predict()
{
if (this->state != TrackState::Tracked)
{
this->mean(7) = 0;
}
this->kf->predict(this->mean, this->covariance);
}
// Convert bounding box to format `(center x, center y, aspect ratio,
// height)`, where the aspect ratio is `width / height`.
DETECTBOX STrack::tlwh_to_xyah(const cv::Rect_<float>& tlwh)
{
DETECTBOX box;
float x = tlwh.x + tlwh.width / 2;
float y = tlwh.y + tlwh.height / 2;
box << x, y, tlwh.width / tlwh.height, tlwh.height;
return box;
}
//Start a new tracklet
void STrack::activate(std::shared_ptr<KalmanFilterTracking> kf, int frame_id)
{
this->kf = kf;
this->track_id = next_id();
auto ret = this->kf->initiate(tlwh_to_xyah(this->_tlwh));
this->mean = ret.first;
this->covariance = ret.second;
tracklet_len = 0;
state = TrackState::Tracked;
if (frame_id == 1)
{
is_activated = true;
}
this->frame_id = frame_id;
start_frame = frame_id;
}
void STrack::re_activate(std::shared_ptr<STrack> new_track, int frame_id, bool new_id)
{
auto ret = this->kf->update(this->mean, this->covariance, tlwh_to_xyah(new_track->to_tlwh_rect()));
this->mean = ret.first;
this->covariance = ret.second;
update_features(new_track->curr_feat);
tracklet_len = 0;
state = TrackState::Tracked;
is_activated = true;
this->frame_id = frame_id;
if (new_id)
{
track_id = next_id();
}
}
DETECTBOX STrack::to_tlwh_box()
{
if (this->mean.isZero())
{
DETECTBOX box;
box << _tlwh.x, _tlwh.y, _tlwh.width, _tlwh.height;
return box;
}
DETECTBOX ret = this->mean.leftCols(4);
ret(2) *= ret(3);
ret.leftCols(2) -= (ret.rightCols(2) / 2);
return ret;
}
cv::Rect_<float> STrack::to_tlwh_rect()
{
DETECTBOX ret = to_tlwh_box();
return cv::Rect_<float>(cv::Point_<float>(ret[0], ret[1]), cv::Point_<float>(ret[0]+ret[2], ret[1] +ret[3]));
}
void STrack::update(std::shared_ptr<STrack> new_track, int frame_id, bool update_feature)
{
this->frame_id = frame_id;
this->tracklet_len += 1;
auto ret = this->kf->update(this->mean, this->covariance, tlwh_to_xyah(new_track->to_tlwh_rect()));
this->mean = ret.first;
this->covariance = ret.second;
state = TrackState::Tracked;
is_activated = true;
score = new_track->score;
if (update_feature)
{
update_features(new_track->curr_feat);
}
}
JDETracker::JDETracker(JDETrackerConfig &config, int frame_rate)
:opt(config)
{
det_thresh = opt.conf_thres;
buffer_size = int(frame_rate / 30.0 * opt.track_buffer);
max_time_lost = buffer_size;
max_per_image = opt.K;
kalman_filter = std::shared_ptr<KalmanFilterTracking>(new KalmanFilterTracking());
ptr_detection_ = std::unique_ptr<Detection>(DetectorFactory::create_object(config.det_config));
}
JDETracker::~JDETracker()
{
}
std::vector<std::shared_ptr<STrack>> JDETracker::update(std::vector<DetectionBox>& dets, std::vector<cv::Mat>& id_feature)
{
frame_id += 1;
std::vector<std::shared_ptr<STrack>> activated_starcks;
std::vector<std::shared_ptr<STrack>> refind_stracks;
std::vector<std::shared_ptr<STrack>> lost_stracks;
std::vector<std::shared_ptr<STrack>> removed_stracks;
std::vector<std::shared_ptr<STrack>> detections;
for (int i = 0; i < dets.size(); i++)
{
auto&box = dets[i].box;
detections.push_back(std::shared_ptr<STrack>(new STrack(box, dets[i].score, id_feature[i], 30)));
}
//Add newly detected tracklets to tracked_stracks'''
std::vector<std::shared_ptr<STrack>> unconfirmed;
std::vector<std::shared_ptr<STrack>> tracked_stracks;
for (auto& track : this->tracked_stracks)
{
if (!track->is_activated)
{
unconfirmed.push_back(track);
}
else
{
tracked_stracks.push_back(track);
}
}
// Step 2: First association, with embedding'''
auto strack_pool = joint_stracks(tracked_stracks, this->lost_stracks);
//# Predict the current location with KF
for (auto& strack : strack_pool)
strack->predict();
//#for strack in strack_pool :
auto dists = matching::embedding_distance(strack_pool, detections);
matching::fuse_motion(this->kalman_filter, dists, strack_pool, detections);
auto[matches, u_track, u_detection] = matching::linear_assignment(dists, 0.4f, strack_pool.size(), detections.size());
for (auto pt : matches)
{
auto& track = strack_pool[pt.x];
auto& det = detections[pt.y];
if (track->state == TrackState::Tracked)
{
track->update(det, this->frame_id);
activated_starcks.push_back(track);
}
else
{
track->re_activate(det, this->frame_id, false);
refind_stracks.push_back(track);
}
}
//''' Step 3: Second association, with IOU'''
std::vector<std::shared_ptr<STrack>> detections_tmp;
for (auto& ud : u_detection) detections_tmp.push_back(detections[ud]);
detections = detections_tmp;
std::vector<std::shared_ptr<STrack>> r_tracked_stracks;
for (auto& ut : u_track)
{
if (strack_pool[ut]->state == TrackState::Tracked)
{
r_tracked_stracks.push_back(strack_pool[ut]);
}
}
dists = matching::iou_distance(r_tracked_stracks, detections);
auto[matches2, u_track2, u_detection2] = matching::linear_assignment(dists, 0.5f, r_tracked_stracks.size(), detections.size());
for (auto pt : matches2)
{
auto& track = r_tracked_stracks[pt.x];
auto& det = detections[pt.y];
if (track->state == TrackState::Tracked)
{
track->update(det, this->frame_id);
activated_starcks.push_back(track);
}
else
{
track->re_activate(det, this->frame_id, false);
refind_stracks.push_back(track);
}
}
for (auto& it:u_track2)
{
auto& track = r_tracked_stracks[it];
if (track->state != TrackState::Lost)
{
track->mark_lost();
lost_stracks.push_back(track);
}
}
//Deal with unconfirmed tracks, usually tracks with only one beginning frame'''
detections_tmp.clear();
for (auto& ud : u_detection2) detections_tmp.push_back(detections[ud]);
detections = detections_tmp;
dists = matching::iou_distance(unconfirmed, detections);
auto[matches3, u_unconfirmed3, u_detection3] = matching::linear_assignment(dists, 0.7f, unconfirmed.size(), detections.size());
for (auto pt : matches3)
{
unconfirmed[pt.x]->update(detections[pt.y], this->frame_id);
activated_starcks.push_back(unconfirmed[pt.x]);
}
for (auto& it : u_unconfirmed3)
{
auto& track = unconfirmed[it];
track->mark_removed();
removed_stracks.push_back(track);
}
//Step 4: Init new stracks"""
for (auto& inew : u_detection3)
{
auto& track = detections[inew];
if (track->score < this->det_thresh)
{
continue;
}
track->activate(this->kalman_filter, this->frame_id);
activated_starcks.push_back(track);
}
//Step 5: Update state"""
for (auto& track:this->lost_stracks)
{
if ((this->frame_id - track->end_frame()) > this->max_time_lost)
{
track->mark_removed();
removed_stracks.push_back(track);
}
}
std::vector<std::shared_ptr<STrack>> tracked_stracks_tmp;
for (auto&t:this->tracked_stracks)
{
if (t->state == TrackState::Tracked)
{
tracked_stracks_tmp.push_back(t);
}
}
this->tracked_stracks = tracked_stracks_tmp;
this->tracked_stracks = joint_stracks(this->tracked_stracks, activated_starcks);
this->tracked_stracks = joint_stracks(this->tracked_stracks, refind_stracks);
this->lost_stracks = sub_stracks(this->lost_stracks, this->tracked_stracks);
this->lost_stracks.insert(this->lost_stracks.end(), lost_stracks.begin(), lost_stracks.end());
this->lost_stracks = sub_stracks(this->lost_stracks, this->removed_stracks);
this->removed_stracks.insert(this->removed_stracks.end(), removed_stracks.begin(), removed_stracks.end());
auto[stracksa, stracksb] = remove_duplicate_stracks(this->tracked_stracks, this->lost_stracks);
this->tracked_stracks = stracksa;
this->lost_stracks = stracksb;
std::vector<std::shared_ptr<STrack>> output_stracks;
for (auto& track : this->tracked_stracks)
{
if (track->is_activated)
{
output_stracks.push_back(track);
}
}
return output_stracks;
}
std::vector<std::shared_ptr<STrack>> JDETracker::joint_stracks(std::vector<std::shared_ptr<STrack>>& tlista, std::vector<std::shared_ptr<STrack>>& tlistb)
{
std::map<int, int> exists;
std::vector<std::shared_ptr<STrack>> res;
for (auto& t: tlista)
{
exists[t->track_id] = 1;
res.push_back(t);
}
for (auto& t : tlistb)
{
int tid = t->track_id;
if (exists.find(tid) == exists.end())
{
exists[tid] = 1;
res.push_back(t);
}
}
return res;
}
std::vector<std::shared_ptr<STrack>> JDETracker::sub_stracks(std::vector<std::shared_ptr<STrack>>& tlista, std::vector<std::shared_ptr<STrack>>& tlistb)
{
std::vector<std::shared_ptr<STrack>> res;
std::map<int, std::shared_ptr<STrack>> stracks;
for (auto&t:tlista)
{
stracks[t->track_id] = t;
}
for (auto&t : tlistb)
{
auto tid = t->track_id;
auto key = stracks.find(tid);
if (key != stracks.end())
{
stracks.erase(key);
}
}
for (auto &v : stracks)
res.push_back(v.second);
return res;
}
std::tuple<std::vector<std::shared_ptr<STrack>>, std::vector<std::shared_ptr<STrack>>> JDETracker::remove_duplicate_stracks(std::vector<std::shared_ptr<STrack>>& stracksa, std::vector<std::shared_ptr<STrack>>& stracksb)
{
auto pdist = matching::iou_distance(stracksa, stracksb);
std::set<int>dupa;
std::set<int>dupb;
for (int p = 0; p < pdist.size(); p++)
{
for (int q = 0; q < pdist[p].size(); q++)
{
if (pdist[p][q] < 0.15f)
{
auto timep = stracksa[p]->frame_id - stracksa[p]->start_frame;
auto timeq = stracksb[q]->frame_id - stracksb[q]->start_frame;
if (timep > timeq)
dupb.insert(q);
else
dupa.insert(p);
}
}
}
std::vector<std::shared_ptr<STrack>>resa, resb;
for (int i = 0; i < stracksa.size(); i++)
{
if (dupa.find(i) == dupa.end())
resa.push_back(stracksa[i]);
}
for (int i = 0; i < stracksb.size(); i++)
{
if (dupb.find(i) == dupb.end())
resb.push_back(stracksb[i]);
}
return { resa, resb };
}