-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathmain.cpp
More file actions
377 lines (313 loc) · 10.4 KB
/
main.cpp
File metadata and controls
377 lines (313 loc) · 10.4 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
///////////////////////////////////////////////////////////////////////////////
// SORT: A Simple, Online and Realtime Tracker
//
// This is a C++ reimplementation of the open source tracker in
// https://github.com/abewley/sort
// Based on the work of Alex Bewley, alex@dynamicdetection.com, 2016
//
// Cong Ma, mcximing@sina.cn, 2016
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
///////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <fstream>
#include <iomanip> // to format image names using setw() and setfill()
//#include <io.h> // to check file existence using POSIX function access(). On Linux include <unistd.h>.
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <set>
#include "Hungarian.h"
#include "KalmanTracker.h"
#include "opencv2/video/tracking.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
typedef struct TrackingBox
{
int frame;
int id;
Rect_<float> box;
}TrackingBox;
// Computes IOU between two bounding boxes
double GetIOU(Rect_<float> bb_test, Rect_<float> bb_gt)
{
float in = (bb_test & bb_gt).area();
float un = bb_test.area() + bb_gt.area() - in;
if (un < DBL_EPSILON)
return 0;
return (double)(in / un);
}
// global variables for counting
#define CNUM 20
int total_frames = 0;
double total_time = 0.0;
void TestSORT(string seqName, bool display);
int main()
{
vector<string> sequences = { "PETS09-S2L1", "TUD-Campus", "TUD-Stadtmitte", "ETH-Bahnhof", "ETH-Sunnyday", "ETH-Pedcross2", "KITTI-13", "KITTI-17", "ADL-Rundle-6", "ADL-Rundle-8", "Venice-2" };
for (auto seq : sequences)
TestSORT(seq, false);
//TestSORT("PETS09-S2L1", true);
// Note: time counted here is of tracking procedure, while the running speed bottleneck is opening and parsing detectionFile.
cout << "Total Tracking took: " << total_time << " for " << total_frames << " frames or " << ((double)total_frames / (double)total_time) << " FPS" << endl;
return 0;
}
void TestSORT(string seqName, bool display)
{
cout << "Processing " << seqName << "..." << endl;
// 0. randomly generate colors, only for display
RNG rng(0xFFFFFFFF);
Scalar_<int> randColor[CNUM];
for (int i = 0; i < CNUM; i++)
rng.fill(randColor[i], RNG::UNIFORM, 0, 256);
string imgPath = "/home/user6/Documents/cpp_sort/sort-cpp/MOT15/train/" + seqName + "/img1/";
if (display)
if (access(imgPath.c_str(), F_OK) == -1)
{
cerr << "Image path not found!" << endl;
display = false;
}
// 1. read detection file
ifstream detectionFile;
string detFileName = "data/" + seqName + "/det.txt";
detectionFile.open(detFileName);
if (!detectionFile.is_open())
{
cerr << "Error: can not find file " << detFileName << endl;
return;
}
string detLine;
istringstream ss;
vector<TrackingBox> detData;
char ch;
float tpx, tpy, tpw, tph;
while ( getline(detectionFile, detLine) )
{
TrackingBox tb;
ss.str(detLine);
ss >> tb.frame >> ch >> tb.id >> ch;
ss >> tpx >> ch >> tpy >> ch >> tpw >> ch >> tph;
ss.str("");
tb.box = Rect_<float>(Point_<float>(tpx, tpy), Point_<float>(tpx + tpw, tpy + tph));
detData.push_back(tb);
}
detectionFile.close();
// 2. group detData by frame
int maxFrame = 0;
for (auto tb : detData) // find max frame number
{
if (maxFrame < tb.frame)
maxFrame = tb.frame;
}
vector<vector<TrackingBox>> detFrameData;
vector<TrackingBox> tempVec;
for (int fi = 0; fi < maxFrame; fi++)
{
for (auto tb : detData)
if (tb.frame == fi + 1) // frame num starts from 1
tempVec.push_back(tb);
detFrameData.push_back(tempVec);
tempVec.clear();
}
// 3. update across frames
int frame_count = 0;
int max_age = 1;
int min_hits = 3;
double iouThreshold = 0.3;
vector<KalmanTracker> trackers;
KalmanTracker::kf_count = 0; // tracking id relies on this, so we have to reset it in each seq.
// variables used in the for-loop
vector<Rect_<float>> predictedBoxes;
vector<vector<double>> iouMatrix;
vector<int> assignment;
set<int> unmatchedDetections;
set<int> unmatchedTrajectories;
set<int> allItems;
set<int> matchedItems;
vector<cv::Point> matchedPairs;
vector<TrackingBox> frameTrackingResult;
unsigned int trkNum = 0;
unsigned int detNum = 0;
double cycle_time = 0.0;
int64 start_time = 0;
// prepare result file.
ofstream resultsFile;
string resFileName = "output/" + seqName + ".txt";
resultsFile.open(resFileName);
if (!resultsFile.is_open())
{
cerr << "Error: can not create file " << resFileName << endl;
return;
}
//////////////////////////////////////////////
// main loop
for (int fi = 0; fi < maxFrame; fi++)
{
total_frames++;
frame_count++;
//cout << frame_count << endl;
// I used to count running time using clock(), but found it seems to conflict with cv::cvWaitkey(),
// when they both exists, clock() can not get right result. Now I use cv::getTickCount() instead.
start_time = getTickCount();
if (trackers.size() == 0) // the first frame met
{
// initialize kalman trackers using first detections.
for (unsigned int i = 0; i < detFrameData[fi].size(); i++)
{
KalmanTracker trk = KalmanTracker(detFrameData[fi][i].box);
trackers.push_back(trk);
}
// output the first frame detections
for (unsigned int id = 0; id < detFrameData[fi].size(); id++)
{
TrackingBox tb = detFrameData[fi][id];
resultsFile << tb.frame << "," << id + 1 << "," << tb.box.x << "," << tb.box.y << "," << tb.box.width << "," << tb.box.height << ",1,-1,-1,-1" << endl;
}
continue;
}
///////////////////////////////////////
// 3.1. get predicted locations from existing trackers.
predictedBoxes.clear();
for (auto it = trackers.begin(); it != trackers.end();)
{
Rect_<float> pBox = (*it).predict();
if (pBox.x >= 0 && pBox.y >= 0)
{
predictedBoxes.push_back(pBox);
it++;
}
else
{
it = trackers.erase(it);
//cerr << "Box invalid at frame: " << frame_count << endl;
}
}
///////////////////////////////////////
// 3.2. associate detections to tracked object (both represented as bounding boxes)
// dets : detFrameData[fi]
trkNum = predictedBoxes.size();
detNum = detFrameData[fi].size();
iouMatrix.clear();
iouMatrix.resize(trkNum, vector<double>(detNum, 0));
for (unsigned int i = 0; i < trkNum; i++) // compute iou matrix as a distance matrix
{
for (unsigned int j = 0; j < detNum; j++)
{
// use 1-iou because the hungarian algorithm computes a minimum-cost assignment.
iouMatrix[i][j] = 1 - GetIOU(predictedBoxes[i], detFrameData[fi][j].box);
}
}
// solve the assignment problem using hungarian algorithm.
// the resulting assignment is [track(prediction) : detection], with len=preNum
HungarianAlgorithm HungAlgo;
assignment.clear();
HungAlgo.Solve(iouMatrix, assignment);
// find matches, unmatched_detections and unmatched_predictions
unmatchedTrajectories.clear();
unmatchedDetections.clear();
allItems.clear();
matchedItems.clear();
if (detNum > trkNum) // there are unmatched detections
{
for (unsigned int n = 0; n < detNum; n++)
allItems.insert(n);
for (unsigned int i = 0; i < trkNum; ++i)
matchedItems.insert(assignment[i]);
set_difference(allItems.begin(), allItems.end(),
matchedItems.begin(), matchedItems.end(),
insert_iterator<set<int>>(unmatchedDetections, unmatchedDetections.begin()));
}
else if (detNum < trkNum) // there are unmatched trajectory/predictions
{
for (unsigned int i = 0; i < trkNum; ++i)
if (assignment[i] == -1) // unassigned label will be set as -1 in the assignment algorithm
unmatchedTrajectories.insert(i);
}
else
;
// filter out matched with low IOU
matchedPairs.clear();
for (unsigned int i = 0; i < trkNum; ++i)
{
if (assignment[i] == -1) // pass over invalid values
continue;
if (1 - iouMatrix[i][assignment[i]] < iouThreshold)
{
unmatchedTrajectories.insert(i);
unmatchedDetections.insert(assignment[i]);
}
else
matchedPairs.push_back(cv::Point(i, assignment[i]));
}
///////////////////////////////////////
// 3.3. updating trackers
// update matched trackers with assigned detections.
// each prediction is corresponding to a tracker
int detIdx, trkIdx;
for (unsigned int i = 0; i < matchedPairs.size(); i++)
{
trkIdx = matchedPairs[i].x;
detIdx = matchedPairs[i].y;
trackers[trkIdx].update(detFrameData[fi][detIdx].box);
}
// create and initialise new trackers for unmatched detections
for (auto umd : unmatchedDetections)
{
KalmanTracker tracker = KalmanTracker(detFrameData[fi][umd].box);
trackers.push_back(tracker);
}
// get trackers' output
frameTrackingResult.clear();
for (auto it = trackers.begin(); it != trackers.end();)
{
if (((*it).m_time_since_update < 1) &&
((*it).m_hit_streak >= min_hits || frame_count <= min_hits))
{
TrackingBox res;
res.box = (*it).get_state();
res.id = (*it).m_id + 1;
res.frame = frame_count;
frameTrackingResult.push_back(res);
it++;
}
else
it++;
// remove dead tracklet
if (it != trackers.end() && (*it).m_time_since_update > max_age)
it = trackers.erase(it);
}
cycle_time = (double)(getTickCount() - start_time);
total_time += cycle_time / getTickFrequency();
for (auto tb : frameTrackingResult)
resultsFile << tb.frame << "," << tb.id << "," << tb.box.x << "," << tb.box.y << "," << tb.box.width << "," << tb.box.height << ",1,-1,-1,-1" << endl;
if (display) // read image, draw results and show them
{
ostringstream oss;
oss << imgPath << setw(6) << setfill('0') << fi + 1;
Mat img = imread(oss.str() + ".jpg");
if (img.empty())
continue;
for (auto tb : frameTrackingResult)
cv::rectangle(img, tb.box, randColor[tb.id % CNUM], 2, 8, 0);
imshow(seqName, img);
waitKey(40);
}
}
resultsFile.close();
if (display)
destroyAllWindows();
}