-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdemo_GMPHD-OGM.cpp
773 lines (666 loc) · 29.7 KB
/
demo_GMPHD-OGM.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
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
/*
BSD 2-Clause License
Copyright (c) 2019, Young-min Song,
Machine Learning and Vision Lab (https://sites.google.com/view/mlv/),
Gwangju Institute of Science and Technology(GIST), South Korea.
All rights reserved.
This software is an implementation of the GMPHD-OGM tracker,
which not only refers to the paper entitled
"Online Multi-Object Tracking with GMPHD Filter and Occlusion Group Management"
but also is available at https://github.com/SonginCV/GMPHD-OGM.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// demo_GMPHD-OGM.cpp
#pragma once
#include "stdafx.h"
using namespace std;
#define DB_TYPE_MOT15 0 // MOT Challenge 2015 Dataset
#define DB_TYPE_MOT17 1 // MOT Challenge 2017 Dataset
#define DB_TYPE_CVPR19 2 // MOT Challenge 2019 (CVPR 2019) Dataset
namespace SYM {
enum {
OBJ_TYPE_PERSON = 0, OBJ_TYPE_CAR = 1,
OBJ_TYPE_BICYCLE = 2, OBJ_TYPE_TRUCK = 3,
OBJ_TYPE_BUS = 4, OBJ_TYPE_DONT_CARE = 5
};
std::string OBJECT_STRINGS[6] = \
{ "Person", "Car",
"Bicycle", "Truck",
"Bus", "DontCare"};
std::string DB_NAMES[3] = { "MOT15", "MOT17", "CVPR19" };
CvScalar OBJECT_COLORS[7] = \
{ cvScalar(255, 255, 0), /*Person: Lite Blue*/
cvScalar(255, 255, 255),/*Car: White*/
cvScalar(255, 0, 255), /*Bicycle: Pink*/
cvScalar(0, 0, 255), /*Truck: Red*/
cvScalar(0, 255, 255), /*Bus: Yellow*/
cvScalar(0, 255, 0), /*Green*/
cvScalar(255, 0, 0) /*Blue*/};
}
/**
* @brief The Function for Loading the Experimental Environments (including Datasets, Parameters,...)
* @details
* @param[in] seqList The file path of sequences' list txt (e.g., seqs\\MOT15train.txt).
* @param[in] paramsFile The file path of parameter txt (e.g., params\\MOT15train.txt).
* @param[out] seqNames The list of all sequneces' names from seqList file.
* @param[out] seqPaths The folder path of all image sequences.
* @param[out] allDets The detection results for the image sequences.
* @param[out] sceneParams The scene-parameters from paramsFile txt.
* @return
* @throws
*/
void LoadEnvSettings(const string seqFile, const string paramsFile, vector<string>& seqNames, vector<string>& seqPaths, vector<vector<vector<vector<float>>>>& allDets, vector<GMPHDOGMparams>& sceneParams);
// Read All Images' Path and Detections as strings
void ReadAllSequences(const string seqFile, vector<string>& seqNames, vector<string>& seqPaths, vector<string>& detTxts);
// The Functions for Reading Scene Parameters from txt
vector<GMPHDOGMparams> ReadSceneOptParams(int nums_of_scenes, string paramsFilePath, int MERGE_METRIC);
void TokenizeParamStrings(vector<string> src, vector<GMPHDOGMparams>& dst);
// The Function for Reading Detection Results for txt
void ReadAllDetections(const vector<string> detTxts, vector<vector<vector<vector<float>>>>& allSeqDets);
// The Function for Sorting Detection Responses by frame number (ascending order)
vector<string> SortAllDetections(const vector<string> allLines, int DB_TYPE = DB_TYPE_MOT17);
/**
* @brief The Function for Testing the GMPHD-OGM tracker in All Scenes
* @details
* @param[in] seqNames The sequences' names.
* @param[in] imgRootPaths The folder paths for the image sequences.
* @param[in] allSeqDets THe detection results' for the image sequences.
* @param[in] sceneParams The parameters for the GMPHD-OGM tracker on all the scenes.
* @param[out] totalProcSecs The processing time (seconds) of the GMPHD-OGM tracker on all the scenes.
* @return int The total number of all the image sequences.
* @throws
*/
int DoBatchTest(const vector<string> seqNames, const vector<string> imgRootPaths, const vector<vector<vector<vector<float>>>> allSeqDets, const vector<GMPHDOGMparams> sceneParams, double& totalProcSecs);
/**
* @brief The Function for Testing the GMPHD-OGM tracker in One Scene
* @details
* @param[in] seqName The name of a sequence.
* @param[in] imgFolderPath The folder path of the image sequence.
* @param[in] seqDets The detection results on the sequence.
* @param[in] params The parameters for the GMPHD-OGM tracker on the scene.
* @param[out] procSecs The processing time (seconds) of the GMPHD-OGM tracker on the scene.
* @return int The number of the image sequence.
* @throws
*/
int DoSequenceTest(const string seqName, const string imgFolderPath, const vector<vector<vector<float>>> seqDets, const GMPHDOGMparams params, double& procSecs);
// The Function for Approximating the Lost Tracks which were recovered by T2TA.
void InterpolateAllTracks(vector<vector<BBTrk>> inputTracks, vector<vector<BBTrk>>& outputTracks);
/**
* @brief The Function for Writing the Tracking Results into a Text File
* @details
* @param
* @param
* @param
* @return
* @throws
*/
void WriteTracksTxt(const int DB_TYPE, string train_or_test, string seqName, GMPHD_OGM* tracker);
void CreateDirsRecursive(string path);
// Drawing Functions
// Draw Detection and Tracking Results
void DrawDetandTrk(cv::Mat& img_det, cv::Mat& img_trk, GMPHD_OGM tracker, const vector<vector<float>> dets, const vector<vector<float>> trks);
/// Draw the Detection Bounding Box
void DrawDetBBS(cv::Mat& img, int iter, cv::Rect bb, double conf, double conf_th, int digits, cv::Scalar color, int thick = 3);
/// Draw the Tracking Bounding Box
void DrawTrkBBS(cv::Mat& img, cv::Rect rec, cv::Scalar color, int thick, int id, double fontScale, string type);
// Draw Frame Number and FPS
void DrawFrameNumberAndFPS(int iFrameCnt, cv::Mat& img, double scale, int thick, int frameOffset = 0, int frames_skip_interval = 1, double sec = -1.0);
// Global Environmental Settings
int DB_TYPE = DB_TYPE_MOT17; // DB_TYPE_MOT15 or DB_TYPE_MOT17
string mode = "train"; // "train" or "test"
string detector = "DPM"; // "ACF", "DPM", "FRCNN", or "SDP"
int main()
{
// Load Local Environmental Settings including image sequences, detection results, and the parameters for the GMPHD-OGM tracker
/// MOT15 or MOT17
// MOT15train.txt, MOT17train_DPM.txt, MOT17train_FRCNN.txt, or MOT17train_SDP.txt
string seqList = "seqs\\" + SYM::DB_NAMES[DB_TYPE] + mode + "_" + detector + ".txt";
string paramsFile = "params\\" + SYM::DB_NAMES[DB_TYPE] + mode + "_" + detector + ".txt";
vector<string> seqNames,imgRootPaths;
vector<vector<vector<vector<float>>>> allDets;
vector<GMPHDOGMparams> sceneParams;
LoadEnvSettings(seqList, paramsFile, seqNames, imgRootPaths, allDets, sceneParams);
// Test "One Image Sequences" or "Batch: More than One Image Sequences"
int64 t_start= cv::getTickCount();
double procSecsDoMOT = 0.0;
//int totalProcFrames = DoSequenceTest(seqNames[2], imgRootPaths[2], allDets[2], sceneParams[2], procSecsDoMOT);
int totalProcFrames = DoBatchTest(seqNames, imgRootPaths, allDets, sceneParams, procSecsDoMOT);
int64 t_end = cv::getTickCount();
float totalProcSecs = (float)((t_end - t_start) / cv::getTickFrequency());
cout <<"-------------------------------------------------------";
cout <<"-------------------------------------------------------"<<endl;
cout << "Total processing frames: " << totalProcFrames << "." << endl;
cout << "Total processing time: " << totalProcSecs << " secs. (DoMOT: " << procSecsDoMOT <<" secs.)"<< endl;
cout << "Avg. frames per second: " << totalProcFrames / totalProcSecs << " FPS. (DoMOT: " << totalProcFrames/procSecsDoMOT <<" FPS)" << endl;
return 0;
}
// The Function for Loading the Experimental Environments (including Datasets, Parameters,...)
void LoadEnvSettings(const string seqList, const string paramsFile, vector<string>& seqNames, vector<string>& seqPaths, vector<vector<vector<vector<float>>>>& allDets, vector<GMPHDOGMparams>& sceneParams) {
cout << "Images and detections are loaded: \"" << seqList << "\"" << endl;
vector<string> detTxts;
ReadAllSequences(seqList, seqNames, seqPaths, detTxts);
ReadAllDetections(detTxts, allDets);
cout << "Scene parameters are loaded: \"" << paramsFile << "\"" << endl;
sceneParams = ReadSceneOptParams(seqPaths.size(), paramsFile, MERGE_METRIC_OPT);
}
void ReadAllSequences(const string seqFile, vector<string>& seqNames, vector<string>& seqPaths, vector<string>& detTxts) {
vector<string> allLines;
if (_access(seqFile.c_str(), 0) == 0) {
ifstream infile(seqFile);
string line,dataFolder;
if (getline(infile, line)) {
dataFolder = line;// The first line indicates dataset's root location.
}
cout << line << endl;
int sq = 1;
while (getline(infile, line)) {
string imgPath = dataFolder + line +"\\img1\\";
string detPath = dataFolder + line +"\\det\\det.txt";
cout << sq++ <<": "<< line << endl;
seqNames.push_back(line);
if (_access(imgPath.c_str(), 0) == 0) seqPaths.push_back(imgPath);
else cout << imgPath << " doesn't exist!!" << endl;
if (_access(detPath.c_str(), 0) == 0) detTxts.push_back(detPath);
else cout << detPath << " doesn't exist!!" << endl;
}
//cout << endl;
}
else {
printf("%s doesn't exist!\n", seqFile.c_str());
}
}
vector<GMPHDOGMparams> ReadSceneOptParams(int nums_of_scenes, string paramsFilePath, int MERGE_METRIC) {
vector<string> str_params_IOU, str_params_SIOA;
ifstream infile(paramsFilePath);
string line;
int cnt = 1;
while (getline(infile, line)) {
if (cnt > 1) {
if (cnt >= 3 && cnt < 3 + nums_of_scenes) {
str_params_SIOA.push_back(line);
//cout << line << endl;
}
else if (cnt >= 4 + nums_of_scenes && cnt < 4 + 2 * nums_of_scenes) {
str_params_IOU.push_back(line);
//cout << line << endl;
}
}
++cnt;
}
vector<GMPHDOGMparams> params_IOU, params_SIOA;
TokenizeParamStrings(str_params_SIOA, params_SIOA);
TokenizeParamStrings(str_params_IOU, params_IOU);
if (MERGE_METRIC == MERGE_METRIC_IOU) return params_IOU;
else if (MERGE_METRIC == MERGE_METRIC_SIOA) return params_SIOA;
}
void TokenizeParamStrings(vector<string> src, vector<GMPHDOGMparams>& dst) {
vector<string>::iterator iter;
for (iter = src.begin(); iter != src.end(); ++iter) {
boost::char_separator<char> bTok(", ");
boost::tokenizer < boost::char_separator<char>>tokens(iter[0], bTok);
vector<string> vals;
for (const auto& t : tokens)
{
vals.push_back(t);
}
GMPHDOGMparams tParams;
tParams.DET_MIN_CONF = boost::lexical_cast<double>(vals.at(1)); // Detection Confidence Threshold
tParams.T2TA_MAX_INTERVAL = boost::lexical_cast<int>(vals.at(2)); // T2TA Maximum Interval
tParams.TRACK_MIN_SIZE = boost::lexical_cast<int>(vals.at(3)); // Track Minium Length
tParams.FRAMES_DELAY_SIZE = tParams.TRACK_MIN_SIZE - 1;
tParams.GROUP_QUEUE_SIZE = tParams.TRACK_MIN_SIZE * 10;
//cout << tParams.DET_MIN_CONF << ", " << tParams.T2TA_MAX_INTERVAL << "," << tParams.TRACK_MIN_SIZE << endl;
if (vals.size() > 4) boost::lexical_cast<int>(vals.at(4)); // Optional Settings (IOU or L1L1 -> D2TA_T2TA : L1 or L2 norm)
dst.push_back(tParams);
}
}
vector<string> SortAllDetections(const vector<string> allLines, int DB_TYPE) {
// ascending sort by frame number
/// http://azza.tistory.com/entry/STL-vector-%EC%9D%98-%EC%A0%95%EB%A0%AC
class T {
public:
int frameNum;
string line;
T(string s, int DB_TYPE) {
line = s;
char tok[8];
if (DB_TYPE == DB_TYPE_MOT15 || DB_TYPE == DB_TYPE_MOT17 || DB_TYPE == DB_TYPE_CVPR19) {
strcpy_s(tok, ", ");
}
boost::char_separator<char> bTok(tok);
boost::tokenizer < boost::char_separator<char>>tokens(s, bTok);
vector<string> vals;
for (const auto& t : tokens)
{
vals.push_back(t);
}
frameNum = boost::lexical_cast<int>(vals.at(0));
}
bool operator<(const T &t) const {
return (frameNum < t.frameNum);
}
};
// Reconstruct the vector<T> from vector<string> for sorting
vector<T> tempAllLines;
vector<string>::const_iterator iter = allLines.begin();
for (; iter != allLines.end(); iter++) {
if (iter[0].size() < 2) continue;
tempAllLines.push_back(T(iter[0], DB_TYPE));
}
// Sort the vector<T> by frame number
std::sort(tempAllLines.begin(), tempAllLines.end());
// Copy the sorted vector<T> to vector<string>
vector<string> sortedAllLines;
vector<T>::iterator iterT = tempAllLines.begin();
for (; iterT != tempAllLines.end(); iterT++) {
sortedAllLines.push_back(iterT[0].line);
}
return sortedAllLines;
}
void ReadAllDetections(const vector<string> detTxts, vector<vector<vector<vector<float>>>>& allDets) {
// Read All Detections with Strings
vector<string>::const_iterator it;
vector<vector<string>> allDetLines; // all detections of all sequences (in forms of strings)
//int i = 0;
for (it = detTxts.begin(); it != detTxts.end(); ++it) {
vector<string> detLines;
if (_access(it[0].c_str(), 0) == 0) {
ifstream infile(it[0]);
string line;
while (!infile.eof()) {
getline(infile, line);
detLines.push_back(line);
//cout << line << endl;
}
}
else {
printf("%s doesn't exist!\n", it[0].c_str());
}
detLines = SortAllDetections(detLines, DB_TYPE);
allDetLines.push_back(detLines);
}
// Convert Strings into vector<float>
vector<vector<string>>::iterator itSeqs;
for (itSeqs = allDetLines.begin(); itSeqs != allDetLines.end(); ++itSeqs) {
vector<vector<vector<float>>> bbsSeq;
vector<vector<float>> bbsFrame;
vector<string>::iterator itLines;
int iFrmCnt = 1;
int cnt = 1;
for (itLines = itSeqs[0].begin(); itLines != itSeqs[0].end(); ++itLines) {
boost::char_separator<char> bTok(", ");
boost::tokenizer < boost::char_separator<char>>tokens(itLines[0], bTok);
vector<string> vals;
for (const auto& t : tokens)
{
vals.push_back(t);
}
if (vals.empty()) {
bbsSeq.push_back(bbsFrame); // is deep copy?
bbsFrame.clear();
break;
}
int curFrm;
curFrm = (int)boost::lexical_cast<float>(vals.at(0)); // frame number
vector<float> bb;
bb.push_back(curFrm); // frame number
bb.push_back(boost::lexical_cast<float>(vals.at(2))); // y
bb.push_back(boost::lexical_cast<float>(vals.at(3))); // y
bb.push_back(boost::lexical_cast<float>(vals.at(4))); // width
bb.push_back(boost::lexical_cast<float>(vals.at(5))); // height
bb.push_back(boost::lexical_cast<float>(vals.at(6))); // detection score
if (iFrmCnt == curFrm) {
bbsFrame.push_back(bb);
//printf("%d:(%d:%d,%d,%d,%d,%.2f)\n", (int)bb[0], bbsFrame.size(),(int)bb[1], (int)bb[2], (int)bb[3], (int)bb[4], bb[5]);
}
else if (iFrmCnt < curFrm) { // Next frame
bbsSeq.push_back(bbsFrame); // is deep copy? Yes
bbsFrame.clear();
bbsFrame.push_back(bb);
iFrmCnt++;
}
}
// End Frame
bbsSeq.push_back(bbsFrame);
allDets.push_back(bbsSeq);
}
}
// The Function for Testing the GMPHD-OGM tracker in All Scenes
int DoBatchTest(const vector<string> seqNames, const vector<string> imgRootPaths, const vector<vector<vector<vector<float>>>> allSeqDets, const vector<GMPHDOGMparams> sceneParams, double& totalProcSecs) {
int totalFrames = 0;
totalProcSecs = 0.0;
for (int sq = 0; sq<imgRootPaths.size(); ++sq) {
cout << "-Sequence " << sq + 1 << ": ";
double procSecs = 0.0;
int nFrames = DoSequenceTest(seqNames[sq], imgRootPaths[sq], allSeqDets[sq], sceneParams[sq], procSecs);
totalFrames += nFrames;
totalProcSecs += procSecs;
printf(" ... %d frames / %.3f secs = %.3f FPS.\n", nFrames, procSecs, (float)nFrames / (float)procSecs);
}
return totalFrames;
}
// The Function for Testing the GMPHD-OGM tracker in One Scene
int DoSequenceTest(const string seqName, const string imgFolderPath, const vector<vector<vector<float>>> seqDets, const GMPHDOGMparams params, double& procSecs) {
// Init a Tracker
GMPHD_OGM *tracker=new GMPHD_OGM();
tracker->SetParams(params);
// vector<string> for All Images' Paths
vector<string> imgs;
// Read Image Files Paths
boost::filesystem::path p(imgFolderPath);
boost::filesystem::directory_iterator end_itr;
// cycle through the directory
for (boost::filesystem::directory_iterator itr(p); itr != end_itr; ++itr)
{
// If it's not a directory, list it. If you want to list directories too, just remove this check.
if (boost::filesystem::is_regular_file(itr->path())) {
// assign current file name to current_file and echo it out to the console.
string imgFile = itr->path().string();
imgs.push_back(imgFile);
}
}
// Specification of the Images and Detections
cv::Mat tImg = cv::imread(imgs[0]);
int frmWidth = tImg.cols, frmHeight = tImg.rows;
tImg.release();
int nImages = imgs.size();
tracker->SetTotalFrames(nImages);
int sumDets = 0;
for (int iFrmCnt = 0; iFrmCnt < nImages; ++iFrmCnt) {
sumDets += seqDets[iFrmCnt].size();
}
printf("Tracking in %d (%dx%d) images with (total detections:%d, density:%.2lf)\n",nImages, frmWidth, frmHeight, sumDets, sumDets/(float)nImages);
if(tracker->GetParams().DET_MIN_CONF == -100.00) printf(" params: (ALL, %d, %d)\n", tracker->GetParams().T2TA_MAX_INTERVAL, tracker->GetParams().TRACK_MIN_SIZE);
else printf(" params: (%.2lf, %d, %d)\n", tracker->GetParams().DET_MIN_CONF, tracker->GetParams().T2TA_MAX_INTERVAL, tracker->GetParams().TRACK_MIN_SIZE);
if (VISUALIZATION_MAIN_ON) {
cv::namedWindow("Detection"); cv::moveWindow("Detection", 0, 0);
cv::namedWindow("Tracking"); cv::moveWindow("Tracking", frmWidth + 10, 0);
}
// Multi-Object Tracking
// Tracking-by-Detection Paradigm: input is detections
// Online Approach: load detections and do tracking, frame-by-frame
procSecs = 0.0;
for (int iFrmCnt = 0; iFrmCnt<nImages; ++iFrmCnt) {
// Image
cv::Mat img = cv::imread(imgs[iFrmCnt]);
cv::Mat img_det = img.clone();
cv::Mat img_trk = img.clone();
// Tracking
double t_start = (double)cv::getTickCount();
vector<vector<float>> tracks = tracker->DoMOT(iFrmCnt, img_trk, seqDets[iFrmCnt]);
double t_end = (double)cv::getTickCount();
double sec = (t_end- t_start)/cv::getTickFrequency();
procSecs += sec;
// Visualization
/// Console
std::cerr << "(" << (iFrmCnt + FRAME_OFFSET);
std::cerr << "/";
std::cerr << nImages;
std::cerr << ") ";
std::cerr << "\r";
/// Window
if (VISUALIZATION_MAIN_ON) {
cv::Mat img_trk_vis = tracker->imgBatch[0].clone();
DrawDetandTrk(img_det, img_trk_vis, tracker[0], seqDets[iFrmCnt], tracks);
DrawFrameNumberAndFPS(iFrmCnt, img_det, 2.0, 2, FRAME_OFFSET);
DrawFrameNumberAndFPS(iFrmCnt - tracker->GetParams().FRAMES_DELAY_SIZE, img_trk_vis, 2.0, 2, FRAME_OFFSET, 1, sec);
//DrawFrameNumberAndFPS(iFrmCnt - FRAMES_DELAY+offset, img_trk_delay, 2.0, 2, 0, frames_skip_interval, tTrk);
cv::imshow("Detection", img_det);
cv::imshow("Tracking", img_trk_vis);
if(iFrmCnt==0) cv::waitKey();
else {
if(SKIP_FRAME_BY_FRAME) cv::waitKey();
else cv::waitKey(10);
}
img_trk_vis.release();
}
img.release();
img_det.release();
img_trk.release();
}
// Write the Tracking Results into a txt file.
WriteTracksTxt(DB_TYPE, mode, seqName, tracker);
// Free the tracker
delete tracker;
return nImages;
}
// The Function for Writing the Tracking Results into a Text File
void WriteTracksTxt(const int DB_TYPE, string train_or_test, string seqName, GMPHD_OGM* tracker) {
vector<vector<BBTrk>> allTracksINTP;
InterpolateAllTracks(tracker->allLiveReliables, allTracksINTP);
char filePath[256],dirPath[256];
if (DB_TYPE == DB_TYPE_MOT15) {
sprintf_s(dirPath, 256, "res\\MOT15\\%s", train_or_test);
sprintf_s(filePath, 256, "res\\MOT15\\%s\\%s.txt", train_or_test, seqName);
CreateDirsRecursive(string(dirPath));
}
else if (DB_TYPE == DB_TYPE_MOT17) {
sprintf_s(dirPath, 256, "res\\MOT17\\%s", train_or_test);
sprintf_s(filePath, 256, "res\\MOT17\\%s\\%s.txt", train_or_test, seqName);
CreateDirsRecursive(string(dirPath));
}
cout << " GMPHD-OGM" << ":" << filePath ;
FILE* fp;
fopen_s(&fp, filePath, "w+");
for (int i = 0; i < allTracksINTP.size(); ++i) { // frame by frame
if (!allTracksINTP[i].empty()) {
for (int tr = 0; tr < allTracksINTP[i].size(); ++tr) {
fprintf_s(fp, "%d,%d,%.2lf,%.2f,%.2f,%.2f,-1,-1,-1,-1\n", i + FRAME_OFFSET,\
allTracksINTP[i][tr].id,\
(float)allTracksINTP[i][tr].rec.x, (float)allTracksINTP[i][tr].rec.y, (float)allTracksINTP[i][tr].rec.width, (float)allTracksINTP[i][tr].rec.height);
}
}
}
fclose(fp);
}
void CreateDirsRecursive(string path) {
boost::char_separator<char> bTok("\\/");
boost::tokenizer < boost::char_separator<char>>tokens(path, bTok);
vector<string> vals;
vector<string> dir_paths;
string path_appended = ".";
for (const auto& t : tokens)
{
path_appended = path_appended + "\\" + t;
dir_paths.push_back(path_appended);
}
for (const auto& dir : dir_paths) {
//cout << dir << endl;
boost::filesystem::path res_folder(dir);
if (!boost::filesystem::exists(res_folder)) {
boost::filesystem::create_directory(res_folder);
//printf("Create %s\n", res_folder.c_str());
}
}
}
void InterpolateAllTracks(vector<vector<BBTrk>> inputTracks, vector<vector<BBTrk>>& outputTracks) {
// Make vector<vector> tracks to map<vector> tracks
map<int, vector<BBTrk>> allTracks;
map<int, vector<BBTrk>> allTracksINTP;
for (int i = 0; i < inputTracks.size(); ++i) { // iterate all frames
if (!inputTracks[i].empty()) {
for (int j = 0; j < inputTracks[i].size(); ++j) { // iterate objects at a frame
int id = inputTracks[i][j].id;
vector<BBTrk> tracklet;
tracklet.push_back(inputTracks[i][j]);
pair< map<int, vector<BBTrk>>::iterator, bool> isEmpty = allTracks.insert(map<int, vector<BBTrk>>::value_type(id, tracklet));
if (isEmpty.second == false) { // already has a element with target.at(j).id
allTracks[id].push_back(inputTracks[i][j]);
}
}
}
}
// Find the tracks' lost interval
map<int, vector<BBTrk>>::iterator iterAllTrk;
for (iterAllTrk = allTracks.begin(); iterAllTrk != allTracks.end(); ++iterAllTrk) { // iterate all tracks (ID by ID)
if (!iterAllTrk->second.empty()) {
if (iterAllTrk->second.size() >= 2) {
int i = 0;
do {
int prev_fn = iterAllTrk->second[i].fn;
int cur_fn = iterAllTrk->second[i + 1].fn;
if (prev_fn + 1 < cur_fn) {
double fd = cur_fn - prev_fn;
cv::Rect prevRec = iterAllTrk->second[i].rec;
cv::Rect curRec = iterAllTrk->second[i + 1].rec;
double xd = (curRec.x - prevRec.x) / fd;
double yd = (curRec.y - prevRec.y) / fd;
double wd = (curRec.width - prevRec.width) / fd;
double hd = (curRec.height - prevRec.height) / fd;
vector<BBTrk> trks[3]; // 0:track in pre-lost interval, 1: track in lost interval, 2: track in post-lost interval;
int nSize = iterAllTrk->second.size();
for (int f = 0; f < nSize; ++f) {
if (f < i + 1) trks[0].push_back(iterAllTrk->second[f]);
else trks[2].push_back(iterAllTrk->second[f]);
}
for (int f = 1; f < fd; ++f) {
BBTrk intp;//= iterAllTrk->second[i];
intp.id = iterAllTrk->first;
intp.fn = prev_fn + f;
intp.rec.x = prevRec.x + f*xd;
intp.rec.y = prevRec.y + f*yd;
intp.rec.width = prevRec.width + f*wd;
intp.rec.height = prevRec.height + f*hd;
intp.isInterpolated = true;
trks[1].push_back(intp);
}
vector<BBTrk> intpTrk;
for (int t = 0; t < 3; t++) {
for (int f = 0; f < trks[t].size(); f++) {
intpTrk.push_back(trks[t][f]);
}
}
iterAllTrk->second.clear();
iterAllTrk->second.assign(intpTrk.begin(), intpTrk.end());
i = i + fd - 1; // ??
}
++i;
if (i == iterAllTrk->second.size() - 1) break;
} while (1);
}
vector<BBTrk> track = iterAllTrk->second;
int id = iterAllTrk->first;
pair< map<int, vector<BBTrk>>::iterator, bool> isEmpty = allTracksINTP.insert(map<int, vector<BBTrk>>::value_type(id, track));
//if (isEmpty.second == false) { // already has a element with target.at(j).id
// printf("ID%d is already added\n", iterAllTrk->first);
//}
}
}
// Make map<vector> tracks to vector<vector> tracks
map<int, vector<BBTrk>>::iterator iterMapINTP;
for (int f = 0; f < inputTracks.size(); ++f) {
map<int, vector<BBTrk>>::iterator iterMapINTP;
vector<BBTrk> trkVecINTP;
for (iterMapINTP = allTracksINTP.begin(); iterMapINTP != allTracksINTP.end(); ++iterMapINTP)
{
if (!iterMapINTP->second.empty()) {
vector<BBTrk>::iterator iterT;
for (iterT = iterMapINTP->second.begin(); iterT != iterMapINTP->second.end(); ++iterT) {
if (iterT->fn == f) {
trkVecINTP.push_back(*iterT);
}
}
}
}
outputTracks.push_back(trkVecINTP);
}
}
// Draw Detection and Tracking Results
void DrawDetandTrk(cv::Mat& img_det, cv::Mat& img_trk, GMPHD_OGM tracker, const vector<vector<float>> dets, const vector<vector<float>> trks) {
vector<vector<float>>::const_iterator iterD;
int i = 1;
for (iterD = dets.begin(); iterD != dets.end(); ++iterD) {
cv::Rect rec((int)iterD[0][1], (int)iterD[0][2], (int)iterD[0][3], (int)iterD[0][4]);
float confidence = iterD[0][5];
DrawDetBBS(img_det, i++, rec, confidence, tracker.GetParams().DET_MIN_CONF, 5, SYM::OBJECT_COLORS[0], 4);
}
vector<vector<float>>::const_iterator iterT;
for (iterT = trks.begin(); iterT != trks.end(); ++iterT) {
int id = iterT[0][0];
cv::Rect rec((int)iterT[0][1], (int)iterT[0][2], (int)iterT[0][3], (int)iterT[0][4]);
DrawTrkBBS(img_trk, rec, tracker.color_tab[id % (MAX_OBJECTS - 1)], BOUNDING_BOX_THICK, id, ID_CONFIDENCE_FONT_SIZE - 1, SYM::OBJECT_STRINGS[0]);
}
}
// Draw the Detection Bounding Box
void DrawDetBBS(cv::Mat& img, int iter, cv::Rect bb, double conf, double conf_th, int digits, cv::Scalar color, int thick) {
int xc = bb.x + bb.width / 2;
int yc = bb.y + bb.height / 2;
std::ostringstream ost;
ost << conf;
std::string str = ost.str();
char cArrConfidence[8];
int c;
for (c = 0; c < digits && c < str.size(); c++) cArrConfidence[c] = str.c_str()[c];
cArrConfidence[c] = '\0';
if (conf >= conf_th) {
/// Draw Detection Bounding Boxes with detection cofidence score
cv::rectangle(img, bb, color, thick);
cv::rectangle(img, cv::Point(bb.x, bb.y), cv::Point(bb.x + bb.width, bb.y - 30), color, -1);
cv::putText(img, cArrConfidence, cvPoint(bb.x, bb.y - 5), cv::FONT_HERSHEY_SIMPLEX, 0.6, CV_RGB(0, 0, 0), 2);
}
else {
cv::rectangle(img, bb, color, 1);
//cv::rectangle(img, cv::Point(bb.x, bb.y+ bb.height), cv::Point(bb.x + bb.width, bb.y + bb.height + 30), color, -1);
//cv::putText(img, cArrConfidence, cvPoint(bb.x, bb.y + bb.height + 15), FONT_HERSHEY_SIMPLEX, 0.4, CV_RGB(0, 0, 0), 1);
}
/// Draw Observation ID (not target ID)
char cArrObsID[8];
sprintf_s(cArrObsID, 8, "%d", iter);
cv::putText(img, cArrObsID, cvPoint(bb.x + 5, bb.y + 30), cv::FONT_HERSHEY_SIMPLEX, 1.0, color, 2);
}
// Draw the Tracking Bounding Box
void DrawTrkBBS(cv::Mat& img, cv::Rect rec, cv::Scalar color, int thick, int id, double fontScale, string type) {
if (id >= 0) {
string strID;
if (type.empty()) strID = to_string(id);
else strID = type.substr(0, 1) + " " + to_string(id);
int wid = 0;
if (id > 0) wid = log10f(id);
else wid = 0;
int bgRecWidth = fontScale*(int)(wid + 3) * 20;
int bgRecHeight = fontScale * 40;
cv::Point pt;
cv::Rect bg;
pt.x = rec.x;
if ((rec.y + rec.height / 2.0) < img.rows / 2) { // y < height/2 (higher)
pt.y = rec.y + rec.height + 40; // should be located on the bottom of the bouding box
bg = cv::Rect(rec.x - 5, rec.y + rec.height, bgRecWidth, bgRecHeight + 10);
}
else { // y >= height/2 (lower)
pt.y = rec.y - 15; // should be located on the top of the bouding box
bg = cv::Rect(rec.x - 5, rec.y - 40, bgRecWidth, bgRecHeight);
}
cv::rectangle(img, bg, cv::Scalar(50, 50, 50), -1);
cv::putText(img, strID, pt, cv::FONT_HERSHEY_SIMPLEX, fontScale, cv::Scalar(0, 255, 255)/*color*/, thick);
}
cv::rectangle(img, rec, color, thick);
}
// Draw Frame Number on Image
void DrawFrameNumberAndFPS(int iFrameCnt, cv::Mat& img, double scale, int thick, int frameOffset, int frames_skip_interval, double sec) {
// Draw Frame Number
char frameCntBuf[8];
sprintf_s(frameCntBuf, 8, "%d", (iFrameCnt + frameOffset) / frames_skip_interval);
cv::putText(img, frameCntBuf, cv::Point(10, 65), CV_FONT_HERSHEY_SIMPLEX, scale, cvScalar(255, 255, 255), thick);
// Draw Frames Per Second
if (sec > 0.0) {
string text = cv::format("%0.1f fps", 1.0 / sec * frames_skip_interval);
cv::Scalar textColor(0, 0, 250);
cv::putText(img, text, cv::Point(10, 100), cv::FONT_HERSHEY_PLAIN, 2, textColor, 2);
}
}