-
Notifications
You must be signed in to change notification settings - Fork 672
Expand file tree
/
Copy pathVideoExample.h
More file actions
302 lines (258 loc) · 7.68 KB
/
VideoExample.h
File metadata and controls
302 lines (258 loc) · 7.68 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
#pragma once
#include <iostream>
#include <vector>
#include <thread>
#include <mutex>
#include <chrono>
#include <condition_variable>
#include <atomic>
#include "BaseDetector.h"
#include "BaseTracker.h"
#include "FileLogger.h"
#include "cvatAnnotationsGenerator.h"
#include "spdlog/spdlog.h"
#include "spdlog/async.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/sinks/rotating_file_sink.h"
///
/// \brief The Frame struct
///
class Frame
{
public:
Frame() = default;
Frame(cv::Mat imgBGR, bool useCLAHE)
{
m_mBGR = imgBGR;
if (useCLAHE)
{
m_clahe = cv::createCLAHE(1.2, cv::Size(4, 4));
AdjustMatBGR();
}
}
///
void SetUseAdjust(bool useCLAHE)
{
if (useCLAHE)
{
m_clahe = cv::createCLAHE(1.2, cv::Size(4, 4));
AdjustMatBGR();
}
else
{
m_clahe.reset();
}
}
///
bool empty() const noexcept
{
return m_mBGR.empty();
}
///
const cv::Mat& GetMatBGR() const noexcept
{
return m_mBGR;
}
///
cv::Mat& GetMatBGRWrite()
{
m_umBGRGenerated = false;
m_mGrayGenerated = false;
m_umGrayGenerated = false;
return m_mBGR;
}
///
bool AdjustMatBGR()
{
if (m_mBGR.empty() || m_clahe.empty())
return false;
cv::cvtColor(m_mBGR, m_mHSV, cv::COLOR_BGR2HSV);
cv::split(m_mHSV, m_chansHSV);
m_clahe->apply(m_chansHSV[2], m_chansHSV[2]);
cv::merge(m_chansHSV, m_mHSV);
cv::cvtColor(m_mHSV, m_mBGR, cv::COLOR_HSV2BGR);
//std::cout << "AdjustMatBGR()" << std::endl;
return true;
}
///
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;
cv::Ptr<cv::CLAHE> m_clahe;
cv::Mat m_mHSV;
std::vector<cv::Mat> m_chansHSV;
};
///
/// \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;
std::vector<time_point_t> m_frameTimeStamps;
size_t m_batchSize = 1;
int64 m_dt = 0;
std::condition_variable m_cond;
std::mutex m_mutex;
std::atomic<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<BaseTracker> m_tracker;
std::string m_showLogsLevel = "debug";
float m_fps = 25;
cv::Size m_frameSize;
int m_framesCount = 0;
bool m_useContrastAdjustment = false;
size_t m_batchSize = 1;
int m_captureTimeOut = 60000;
int m_trackingTimeOut = 60000;
ResultsLog m_resultsLog;
CVATAnnotationsGenerator m_cvatAnnotationsGenerator;
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;
virtual void DrawTrack(cv::Mat frame, const TrackingObject& track, bool drawTrajectory, int framesCounter, const std::string& userLabel = "");
TrackerSettings m_trackerSettings;
bool m_trackerSettingsLoaded = false;
std::vector<cv::Scalar> m_colors;
std::shared_ptr<spdlog::sinks::stdout_color_sink_mt> m_consoleSink;
std::shared_ptr<spdlog::sinks::rotating_file_sink_mt> m_fileSink;
std::shared_ptr<spdlog::logger> m_logger;
private:
std::vector<TrackingObject> m_tracks;
bool m_isTrackerInitialized = false;
bool m_isDetectorInitialized = false;
std::string m_inFile;
std::string m_outFile;
#if 0
int m_fourcc = cv::VideoWriter::fourcc('h', '2', '6', '4');
#else
int m_fourcc = cv::VideoWriter::fourcc('M', 'J', 'P', 'G');
#endif
int m_startFrame = 0;
int m_endFrame = 0;
int m_finishDelay = 0;
time_point_t m_startTimeStamp;
bool m_useArchieveTime = true;
FrameInfo m_frameInfo[2];
time_point_t GetNextTimeStamp(int framesCounter) const;
bool OpenCapture(cv::VideoCapture& capture);
bool WriteFrame(cv::VideoWriter& writer, const cv::Mat& frame);
};