forked from Smorodov/Multitarget-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBYTETracker.cpp
More file actions
583 lines (509 loc) · 18.5 KB
/
BYTETracker.cpp
File metadata and controls
583 lines (509 loc) · 18.5 KB
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
#include "BYTETracker.h"
#include <limits>
#include "defines.h"
#include "trajectory.h"
#include "TrackerSettings.h"
///
byte_track::BYTETracker::BYTETracker(const int& frame_rate,
const int& track_buffer,
const float& track_thresh,
const float& high_thresh,
const float& match_thresh) :
track_thresh_(track_thresh),
high_thresh_(high_thresh),
match_thresh_(match_thresh),
max_time_lost_(static_cast<size_t>(frame_rate / 30.0 * track_buffer)),
frame_id_(0),
track_id_count_(0)
{
}
///
void byte_track::BYTETracker::GetTracks(std::vector<TrackingObject>& tracks) const
{
tracks.clear();
if (output_stracks_.size() > tracks.capacity())
tracks.reserve(output_stracks_.size());
for (const auto& track : output_stracks_)
{
std::chrono::duration<double> period = m_lastFrameTime - m_lastFrameTime;
cv::RotatedRect rr(track->getRect().tl(), cv::Point2f(static_cast<float>(track->getRect().x + track->getRect().width), static_cast<float>(track->getRect().y)), track->getRect().br());
TrackingObject to(rr, track->getTrackId(), track->getTrace(), false, cvRound(period.count()), false,
track->getType(), track->getScore(), track->getVelocity());
tracks.emplace_back(to);
}
}
///
void byte_track::BYTETracker::GetRemovedTracks(std::vector<track_id_t>& trackIDs) const
{
if (removed_stracks_.size() > trackIDs.capacity())
trackIDs.reserve(removed_stracks_.size());
for (const auto& remTrack : removed_stracks_)
{
trackIDs.emplace_back(remTrack->getTrackId());
}
}
///
void byte_track::BYTETracker::Update(const regions_t& regions, cv::UMat /*currFrame*/, time_point_t frameTime)
{
m_lastFrameTime = frameTime;
////////////////// Step 1: Get detections //////////////////
frame_id_++;
// Create new STracks using the result of object detection
std::vector<STrackPtr> det_stracks;
std::vector<STrackPtr> det_low_stracks;
for (const auto ®ion : regions)
{
const auto strack = std::make_shared<STrack>(region.m_brect, region.m_confidence, region.m_type, frameTime);
if (region.m_confidence >= track_thresh_)
det_stracks.push_back(strack);
else
det_low_stracks.push_back(strack);
}
// Create lists of existing STrack
std::vector<STrackPtr> active_stracks;
std::vector<STrackPtr> non_active_stracks;
std::vector<STrackPtr> strack_pool;
for (const auto& tracked_strack : tracked_stracks_)
{
if (!tracked_strack->isActivated())
non_active_stracks.push_back(tracked_strack);
else
active_stracks.push_back(tracked_strack);
}
strack_pool = jointStracks(active_stracks, lost_stracks_);
// Predict current pose by KF
for (auto &strack : strack_pool)
{
strack->predict();
}
////////////////// Step 2: First association, with IoU //////////////////
std::vector<STrackPtr> current_tracked_stracks;
std::vector<STrackPtr> remain_tracked_stracks;
std::vector<STrackPtr> remain_det_stracks;
std::vector<STrackPtr> refind_stracks;
{
std::vector<std::vector<int>> matches_idx;
std::vector<size_t> unmatch_detection_idx, unmatch_track_idx;
const auto dists = calcIouDistance(strack_pool, det_stracks);
linearAssignment(dists, strack_pool.size(), det_stracks.size(), match_thresh_,
matches_idx, unmatch_track_idx, unmatch_detection_idx);
for (const auto &match_idx : matches_idx)
{
const auto track = strack_pool[match_idx[0]];
const auto det = det_stracks[match_idx[1]];
if (track->getSTrackState() == STrackState::Tracked)
{
track->update(*det, frame_id_, frameTime);
current_tracked_stracks.push_back(track);
}
else
{
track->reActivate(*det, frame_id_, -1, frameTime);
refind_stracks.push_back(track);
}
}
for (const auto &unmatch_idx : unmatch_detection_idx)
{
remain_det_stracks.push_back(det_stracks[unmatch_idx]);
}
for (const auto &unmatch_idx : unmatch_track_idx)
{
if (strack_pool[unmatch_idx]->getSTrackState() == STrackState::Tracked)
remain_tracked_stracks.push_back(strack_pool[unmatch_idx]);
}
}
////////////////// Step 3: Second association, using low score dets //////////////////
std::vector<STrackPtr> current_lost_stracks;
{
std::vector<std::vector<int>> matches_idx;
std::vector<size_t> unmatch_track_idx, unmatch_detection_idx;
const auto dists = calcIouDistance(remain_tracked_stracks, det_low_stracks);
linearAssignment(dists, remain_tracked_stracks.size(), det_low_stracks.size(), 0.5,
matches_idx, unmatch_track_idx, unmatch_detection_idx);
for (const auto &match_idx : matches_idx)
{
const auto track = remain_tracked_stracks[match_idx[0]];
const auto det = det_low_stracks[match_idx[1]];
if (track->getSTrackState() == STrackState::Tracked)
{
track->update(*det, frame_id_, frameTime);
current_tracked_stracks.push_back(track);
}
else
{
track->reActivate(*det, frame_id_, -1, frameTime);
refind_stracks.push_back(track);
}
}
for (const auto &unmatch_track : unmatch_track_idx)
{
const auto track = remain_tracked_stracks[unmatch_track];
if (track->getSTrackState() != STrackState::Lost)
{
track->markAsLost();
current_lost_stracks.push_back(track);
}
}
}
////////////////// Step 4: Init new stracks //////////////////
std::vector<STrackPtr> current_removed_stracks;
{
std::vector<size_t> unmatch_detection_idx;
std::vector<size_t> unmatch_unconfirmed_idx;
std::vector<std::vector<int>> matches_idx;
// Deal with unconfirmed tracks, usually tracks with only one beginning frame
const auto dists = calcIouDistance(non_active_stracks, remain_det_stracks);
linearAssignment(dists, non_active_stracks.size(), remain_det_stracks.size(), 0.7,
matches_idx, unmatch_unconfirmed_idx, unmatch_detection_idx);
for (const auto &match_idx : matches_idx)
{
non_active_stracks[match_idx[0]]->update(*remain_det_stracks[match_idx[1]], frame_id_, frameTime);
current_tracked_stracks.push_back(non_active_stracks[match_idx[0]]);
}
for (const auto &unmatch_idx : unmatch_unconfirmed_idx)
{
const auto track = non_active_stracks[unmatch_idx];
track->markAsRemoved();
current_removed_stracks.push_back(track);
}
// Add new stracks
for (const auto &unmatch_idx : unmatch_detection_idx)
{
const auto track = remain_det_stracks[unmatch_idx];
if (track->getScore() < high_thresh_)
continue;
track_id_count_++;
track->activate(frame_id_, track_id_count_, frameTime);
current_tracked_stracks.push_back(track);
}
}
////////////////// Step 5: Update state //////////////////
for (const auto &lost_strack : lost_stracks_)
{
if (frame_id_ - lost_strack->getFrameId() > max_time_lost_)
{
lost_strack->markAsRemoved();
current_removed_stracks.push_back(lost_strack);
}
}
tracked_stracks_ = jointStracks(current_tracked_stracks, refind_stracks);
lost_stracks_ = subStracks(jointStracks(subStracks(lost_stracks_, tracked_stracks_), current_lost_stracks), removed_stracks_);
removed_stracks_ = jointStracks(removed_stracks_, current_removed_stracks);
std::vector<STrackPtr> tracked_stracks_out, lost_stracks_out;
removeDuplicateStracks(tracked_stracks_, lost_stracks_, tracked_stracks_out, lost_stracks_out);
tracked_stracks_ = tracked_stracks_out;
lost_stracks_ = lost_stracks_out;
output_stracks_.clear();
for (const auto &track : tracked_stracks_)
{
if (track->isActivated())
output_stracks_.push_back(track);
}
}
///
std::vector<byte_track::BYTETracker::STrackPtr> byte_track::BYTETracker::jointStracks(const std::vector<STrackPtr> &a_tlist,
const std::vector<STrackPtr> &b_tlist) const
{
std::map<size_t, size_t> exists;
std::vector<STrackPtr> res;
for (size_t i = 0; i < a_tlist.size(); i++)
{
exists.emplace(a_tlist[i]->getTrackId(), 1);
res.push_back(a_tlist[i]);
}
for (size_t i = 0; i < b_tlist.size(); i++)
{
const size_t &tid = b_tlist[i]->getTrackId();
if (!exists[tid] || exists.count(tid) == 0)
{
exists[tid] = 1;
res.push_back(b_tlist[i]);
}
}
return res;
}
///
std::vector<byte_track::BYTETracker::STrackPtr> byte_track::BYTETracker::subStracks(const std::vector<STrackPtr> &a_tlist,
const std::vector<STrackPtr> &b_tlist) const
{
std::map<size_t, STrackPtr> stracks;
for (size_t i = 0; i < a_tlist.size(); i++)
{
stracks.emplace(a_tlist[i]->getTrackId(), a_tlist[i]);
}
for (size_t i = 0; i < b_tlist.size(); i++)
{
const size_t&tid = b_tlist[i]->getTrackId();
if (stracks.count(tid) != 0)
stracks.erase(tid);
}
std::vector<STrackPtr> res;
std::map<size_t, STrackPtr>::iterator it;
for (it = stracks.begin(); it != stracks.end(); ++it)
{
res.push_back(it->second);
}
return res;
}
///
void byte_track::BYTETracker::removeDuplicateStracks(const std::vector<STrackPtr> &a_stracks,
const std::vector<STrackPtr> &b_stracks,
std::vector<STrackPtr> &a_res,
std::vector<STrackPtr> &b_res) const
{
const auto ious = calcIouDistance(a_stracks, b_stracks);
std::vector<std::pair<size_t, size_t>> overlapping_combinations;
for (size_t i = 0; i < ious.size(); i++)
{
for (size_t j = 0; j < ious[i].size(); j++)
{
if (ious[i][j] < 0.15)
overlapping_combinations.emplace_back(i, j);
}
}
std::vector<bool> a_overlapping(a_stracks.size(), false), b_overlapping(b_stracks.size(), false);
for (const auto &[a_idx, b_idx] : overlapping_combinations)
{
const int timep = a_stracks[a_idx]->getFrameId() - a_stracks[a_idx]->getStartFrameId();
const int timeq = b_stracks[b_idx]->getFrameId() - b_stracks[b_idx]->getStartFrameId();
if (timep > timeq)
b_overlapping[b_idx] = true;
else
a_overlapping[a_idx] = true;
}
for (size_t ai = 0; ai < a_stracks.size(); ai++)
{
if (!a_overlapping[ai])
a_res.push_back(a_stracks[ai]);
}
for (size_t bi = 0; bi < b_stracks.size(); bi++)
{
if (!b_overlapping[bi])
b_res.push_back(b_stracks[bi]);
}
}
///
void byte_track::BYTETracker::linearAssignment(const std::vector<std::vector<float>> &cost_matrix,
const size_t &cost_matrix_size,
const size_t &cost_matrix_size_size,
const float &thresh,
std::vector<std::vector<int>> &matches,
std::vector<size_t> &a_unmatched,
std::vector<size_t> &b_unmatched) const
{
if (cost_matrix.size() == 0)
{
for (size_t i = 0; i < cost_matrix_size; i++)
{
a_unmatched.push_back(i);
}
for (size_t i = 0; i < cost_matrix_size_size; i++)
{
b_unmatched.push_back(i);
}
return;
}
std::vector<int> rowsol;
std::vector<int> colsol;
execLapjv(cost_matrix, rowsol, colsol, true, thresh);
for (size_t i = 0; i < rowsol.size(); i++)
{
if (rowsol[i] >= 0)
{
std::vector<int> match;
match.push_back(i);
match.push_back(rowsol[i]);
matches.push_back(match);
}
else
{
a_unmatched.push_back(i);
}
}
for (size_t i = 0; i < colsol.size(); i++)
{
if (colsol[i] < 0)
b_unmatched.push_back(i);
}
}
///
std::vector<std::vector<float>> byte_track::BYTETracker::calcIous(const std::vector<cv::Rect2f> &a_rect,
const std::vector<cv::Rect2f> &b_rect) const
{
std::vector<std::vector<float>> ious;
if (a_rect.size() * b_rect.size() == 0)
return ious;
ious.resize(a_rect.size());
for (size_t i = 0; i < ious.size(); i++)
{
ious[i].resize(b_rect.size());
}
auto calcIoU = [](const cv::Rect2f& r1, const cv::Rect2f& r2)
{
const float box_area = (r2.width + 1) * (r2.height + 1);
const float iw = std::min(r1.x + r1.width, r2.x + r2.width) - std::max(r1.x, r2.x) + 1;
float iou = 0;
if (iw > 0)
{
const float ih = std::min(r1.y + r1.height, r2.y + r2.height) - std::max(r1.y, r2.y) + 1;
if (ih > 0)
{
const float ua = (r1.width + 1) * (r1.height + 1) + box_area - iw * ih;
iou = iw * ih / ua;
}
}
return iou;
};
for (size_t bi = 0; bi < b_rect.size(); bi++)
{
for (size_t ai = 0; ai < a_rect.size(); ai++)
{
ious[ai][bi] = calcIoU(b_rect[bi], a_rect[ai]);
}
}
return ious;
}
///
std::vector<std::vector<float> > byte_track::BYTETracker::calcIouDistance(const std::vector<STrackPtr> &a_tracks,
const std::vector<STrackPtr> &b_tracks) const
{
std::vector<cv::Rect2f> a_rects, b_rects;
for (size_t i = 0; i < a_tracks.size(); i++)
{
a_rects.push_back(a_tracks[i]->getRect());
}
for (size_t i = 0; i < b_tracks.size(); i++)
{
b_rects.push_back(b_tracks[i]->getRect());
}
const auto ious = calcIous(a_rects, b_rects);
std::vector<std::vector<float>> cost_matrix;
for (size_t i = 0; i < ious.size(); i++)
{
std::vector<float> iou;
for (size_t j = 0; j < ious[i].size(); j++)
{
iou.push_back(1 - ious[i][j]);
}
cost_matrix.push_back(iou);
}
return cost_matrix;
}
///
double byte_track::BYTETracker::execLapjv(const std::vector<std::vector<float>> &cost,
std::vector<int> &rowsol,
std::vector<int> &colsol,
bool extend_cost,
float cost_limit,
bool return_cost) const
{
std::vector<std::vector<float> > cost_c;
cost_c.assign(cost.begin(), cost.end());
std::vector<std::vector<float> > cost_c_extended;
size_t n_rows = cost.size();
size_t n_cols = cost[0].size();
rowsol.resize(n_rows);
colsol.resize(n_cols);
size_t n = 0;
if (n_rows == n_cols)
{
n = n_rows;
}
else
{
if (!extend_cost)
throw std::runtime_error("The `extend_cost` variable should set True");
}
if (extend_cost || cost_limit < std::numeric_limits<float>::max())
{
n = n_rows + n_cols;
cost_c_extended.resize(n);
for (size_t i = 0; i < cost_c_extended.size(); i++)
cost_c_extended[i].resize(n);
if (cost_limit < std::numeric_limits<float>::max())
{
for (size_t i = 0; i < cost_c_extended.size(); i++)
{
for (size_t j = 0; j < cost_c_extended[i].size(); j++)
{
cost_c_extended[i][j] = cost_limit / 2.0f;
}
}
}
else
{
float cost_max = -1;
for (size_t i = 0; i < cost_c.size(); i++)
{
for (size_t j = 0; j < cost_c[i].size(); j++)
{
if (cost_c[i][j] > cost_max)
cost_max = cost_c[i][j];
}
}
for (size_t i = 0; i < cost_c_extended.size(); i++)
{
for (size_t j = 0; j < cost_c_extended[i].size(); j++)
{
cost_c_extended[i][j] = cost_max + 1;
}
}
}
for (size_t i = n_rows; i < cost_c_extended.size(); i++)
{
for (size_t j = n_cols; j < cost_c_extended[i].size(); j++)
{
cost_c_extended[i][j] = 0;
}
}
for (size_t i = 0; i < n_rows; i++)
{
for (size_t j = 0; j < n_cols; j++)
{
cost_c_extended[i][j] = cost_c[i][j];
}
}
cost_c.clear();
cost_c.assign(cost_c_extended.begin(), cost_c_extended.end());
}
std::vector<int> x_c(n, -1);
std::vector<int> y_c(n, 0);
int ret = lapjv_internal(n, cost_c, x_c, y_c);
if (ret != 0)
throw std::runtime_error("The result of lapjv_internal() is invalid.");
double opt = 0.0;
if (n != n_rows)
{
for (size_t i = 0; i < n; i++)
{
if (x_c[i] >= n_cols)
x_c[i] = -1;
if (y_c[i] >= n_rows)
y_c[i] = -1;
}
for (size_t i = 0; i < n_rows; i++)
{
rowsol[i] = x_c[i];
}
for (size_t i = 0; i < n_cols; i++)
{
colsol[i] = y_c[i];
}
if (return_cost)
{
for (size_t i = 0; i < rowsol.size(); i++)
{
if (rowsol[i] != -1)
opt += cost_c[i][rowsol[i]];
}
}
}
else if (return_cost)
{
for (size_t i = 0; i < rowsol.size(); i++)
{
opt += cost_c[i][rowsol[i]];
}
}
return opt;
}