forked from Smorodov/Multitarget-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVideoExample.h
More file actions
384 lines (339 loc) · 8.34 KB
/
VideoExample.h
File metadata and controls
384 lines (339 loc) · 8.34 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
#pragma once
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include <thread>
#include <mutex>
#include <chrono>
#include <condition_variable>
#include <atomic>
#include "BaseDetector.h"
#include "Ctracker.h"
///
/// \brief The ResultsLog class
///
class ResultsLog
{
public:
///
ResultsLog(const std::string& fileName)
: m_fileName(fileName)
{
}
///
~ResultsLog()
{
WriteAll(true);
}
///
bool Open()
{
m_resCSV.close();
if (m_fileName.size() > 5)
{
m_resCSV.open(m_fileName);
return m_resCSV.is_open();
}
return false;
}
///
bool AddTrack(int framesCounter, size_t trackID, const cv::Rect& brect, objtype_t type, float confidence)
{
if (m_resCSV.is_open())
{
auto frame = m_frames.find(framesCounter);
if (frame == std::end(m_frames))
{
DetectsOnFrame tmpFrame;
tmpFrame.m_detects.emplace_back(trackID, brect, type, confidence);
m_frames.emplace(framesCounter, tmpFrame);
}
else
{
frame->second.m_detects.emplace_back(trackID, brect, type, confidence);
}
return true;
}
return false;
}
///
void AddRobustTrack(size_t trackID)
{
m_robustIDs.insert(trackID);
}
private:
std::string m_fileName;
std::ofstream m_resCSV;
///
struct Detection
{
cv::Rect m_rect;
objtype_t m_type;
float m_conf = 0.f;
size_t m_trackID = 0;
Detection(size_t trackID, const cv::Rect& brect, objtype_t type, float confidence)
{
m_type = type;
m_rect = brect;
m_conf = confidence;
m_trackID = trackID;
}
};
///
struct DetectsOnFrame
{
std::vector<Detection> m_detects;
};
std::map<int, DetectsOnFrame> m_frames;
std::set<size_t> m_robustIDs;
///
void WriteAll(bool byFrames)
{
if (byFrames)
{
#if 1
char delim = ',';
for (const auto& frame : m_frames)
{
for (const auto& detect : frame.second.m_detects)
{
if (m_robustIDs.find(detect.m_trackID) != std::end(m_robustIDs))
{
m_resCSV << frame.first << delim << TypeConverter::Type2Str(detect.m_type) << delim << detect.m_rect.x << delim << detect.m_rect.y << delim <<
detect.m_rect.width << delim << detect.m_rect.height << delim <<
detect.m_conf << delim << std::endl;
}
}
}
#else
char delim = ' ';
for (const auto& frame : m_frames)
{
for (const auto& detect : frame.second.m_detects)
{
if (m_robustIDs.find(detect.m_trackID) != std::end(m_robustIDs))
{
m_resCSV << frame.first << delim << TypeConverter::Type2Str(detect.m_type) << delim << detect.m_rect.x << delim << detect.m_rect.y << delim <<
(detect.m_rect.x + detect.m_rect.width) << delim << (detect.m_rect.y + detect.m_rect.height) << delim <<
detect.m_conf << delim << detect.m_trackID << std::endl;
}
}
}
#endif
}
else
{
char delim = ',';
for (size_t id : m_robustIDs)
{
for (const auto& frame : m_frames)
{
for (const auto& detect : frame.second.m_detects)
{
if (detect.m_trackID == id)
{
m_resCSV << frame.first << delim << id << delim << detect.m_rect.x << delim << detect.m_rect.y << delim <<
detect.m_rect.width << delim << detect.m_rect.height << delim <<
detect.m_conf << ",-1,-1,-1," << std::endl;
break;
}
}
}
}
}
}
};
///
/// \brief The Frame struct
///
class Frame
{
public:
Frame() = default;
Frame(cv::Mat imgBGR)
{
m_mBGR = imgBGR;
}
///
bool empty() const
{
return m_mBGR.empty();
}
///
const cv::Mat& GetMatBGR()
{
return m_mBGR;
}
///
cv::Mat& GetMatBGRWrite()
{
m_umBGRGenerated = false;
m_mGrayGenerated = false;
m_umGrayGenerated = false;
return m_mBGR;
}
///
const cv::Mat& GetMatGray()
{
if (m_mGray.empty() || !m_mGrayGenerated)
{
if (m_umGray.empty() || !m_umGrayGenerated)
cv::cvtColor(m_mBGR, m_mGray, cv::COLOR_BGR2GRAY);
else
m_mGray = m_umGray.getMat(cv::ACCESS_READ);
m_mGrayGenerated = true;
}
return m_mGray;
}
///
const cv::UMat& GetUMatBGR()
{
std::thread::id lastThreadID = std::this_thread::get_id();
if (m_umBGR.empty() || !m_umBGRGenerated || lastThreadID != m_umBGRThreadID)
{
m_umBGR = m_mBGR.getUMat(cv::ACCESS_READ);
m_umBGRGenerated = true;
m_umBGRThreadID = lastThreadID;
}
return m_umBGR;
}
///
const cv::UMat& GetUMatGray()
{
std::thread::id lastThreadID = std::this_thread::get_id();
if (m_umGray.empty() || !m_umGrayGenerated || lastThreadID != m_umGrayThreadID)
{
if (m_mGray.empty() || !m_mGrayGenerated)
{
if (m_umBGR.empty() || !m_umBGRGenerated || lastThreadID != m_umGrayThreadID)
cv::cvtColor(m_mBGR, m_umGray, cv::COLOR_BGR2GRAY);
else
cv::cvtColor(m_umBGR, m_umGray, cv::COLOR_BGR2GRAY);
}
else
{
m_umGray = m_mGray.getUMat(cv::ACCESS_READ);
}
m_umGrayGenerated = true;
m_umGrayThreadID = lastThreadID;
}
return m_umGray;
}
private:
cv::Mat m_mBGR;
cv::Mat m_mGray;
cv::UMat m_umBGR;
cv::UMat m_umGray;
bool m_umBGRGenerated = false;
bool m_mGrayGenerated = false;
bool m_umGrayGenerated = false;
std::thread::id m_umBGRThreadID;
std::thread::id m_umGrayThreadID;
};
///
/// \brief The FrameInfo struct
///
struct FrameInfo
{
///
FrameInfo()
{
m_frames.reserve(m_batchSize);
m_regions.reserve(m_batchSize);
m_frameInds.reserve(m_batchSize);
}
///
FrameInfo(size_t batchSize)
: m_batchSize(batchSize)
{
m_frames.reserve(m_batchSize);
m_regions.reserve(m_batchSize);
m_frameInds.reserve(m_batchSize);
}
///
void SetBatchSize(size_t batchSize)
{
m_batchSize = batchSize;
m_frames.reserve(m_batchSize);
m_regions.reserve(m_batchSize);
m_frameInds.reserve(m_batchSize);
}
///
void CleanRegions()
{
if (m_regions.size() != m_batchSize)
m_regions.resize(m_batchSize);
for (auto& regions : m_regions)
{
regions.clear();
}
}
///
void CleanTracks()
{
if (m_tracks.size() != m_batchSize)
m_tracks.resize(m_batchSize);
for (auto& tracks : m_tracks)
{
tracks.clear();
}
}
std::vector<Frame> m_frames;
std::vector<regions_t> m_regions;
std::vector<std::vector<TrackingObject>> m_tracks;
std::vector<int> m_frameInds;
size_t m_batchSize = 1;
int64 m_dt = 0;
std::condition_variable m_cond;
std::mutex m_mutex;
bool m_captured = false;
};
///
/// \brief The VideoExample class
///
class VideoExample
{
public:
VideoExample(const cv::CommandLineParser& parser);
VideoExample(const VideoExample&) = delete;
VideoExample(VideoExample&&) = delete;
VideoExample& operator=(const VideoExample&) = delete;
VideoExample& operator=(VideoExample&&) = delete;
virtual ~VideoExample() = default;
void AsyncProcess();
void SyncProcess();
protected:
std::unique_ptr<BaseDetector> m_detector;
std::unique_ptr<CTracker> m_tracker;
bool m_showLogs = true;
float m_fps = 25;
size_t m_batchSize = 1;
int m_captureTimeOut = 60000;
int m_trackingTimeOut = 60000;
ResultsLog m_resultsLog;
static void CaptureAndDetect(VideoExample* thisPtr, std::atomic<bool>& stopCapture);
virtual bool InitDetector(cv::UMat frame) = 0;
virtual bool InitTracker(cv::UMat frame) = 0;
void Detection(FrameInfo& frame);
void Tracking(FrameInfo& frame);
virtual void DrawData(cv::Mat frame, const std::vector<TrackingObject>& tracks, int framesCounter, int currTime) = 0;
void DrawTrack(cv::Mat frame, int resizeCoeff, const TrackingObject& track, bool drawTrajectory, int framesCounter);
TrackerSettings m_trackerSettings;
bool m_trackerSettingsLoaded = false;
bool ParseTrackerSettings(const std::string& settingsFile);
private:
std::vector<TrackingObject> m_tracks;
bool m_isTrackerInitialized = false;
bool m_isDetectorInitialized = false;
std::string m_inFile;
std::string m_outFile;
int m_fourcc = cv::VideoWriter::fourcc('M', 'J', 'P', 'G');
int m_startFrame = 0;
int m_endFrame = 0;
int m_finishDelay = 0;
std::vector<cv::Scalar> m_colors;
FrameInfo m_frameInfo[2];
bool OpenCapture(cv::VideoCapture& capture);
bool WriteFrame(cv::VideoWriter& writer, const cv::Mat& frame);
};