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
232 lines (202 loc) · 5.45 KB
/
VideoExample.h
File metadata and controls
232 lines (202 loc) · 5.45 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
#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 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;
std::vector<TrackingObject> m_tracks;
bool m_showLogs = true;
float m_fps = 25;
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(cv::Mat frame, regions_t& regions);
void Tracking(cv::Mat frame, const regions_t& regions);
virtual void DrawData(cv::Mat frame, 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:
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;
struct FrameInfo
{
cv::Mat m_frame;
regions_t m_regions;
int64 m_dt = 0;
std::condition_variable m_cond;
std::mutex m_mutex;
bool m_captured = false;
};
FrameInfo m_frameInfo[2];
bool OpenCapture(cv::VideoCapture& capture);
bool WriteFrame(cv::VideoWriter& writer, const cv::Mat& frame);
};