Skip to content

Commit a07fbe4

Browse files
committed
Batch processing for continuous frames
1 parent 36d2fe9 commit a07fbe4

9 files changed

Lines changed: 2258 additions & 1977 deletions

File tree

example/VideoExample.cpp

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ void VideoExample::SyncProcess()
181181

182182
for (size_t i = 0; i < m_batchSize; ++i)
183183
{
184-
DrawData(frameInfo.m_frames[i], frameInfo.m_frameInds[i], currTime);
184+
DrawData(frameInfo.m_frames[i].GetMatBGR(), frameInfo.m_frameInds[i], currTime);
185185

186186
#ifndef SILENT_WORK
187187
cv::imshow("Video", frame);
@@ -196,7 +196,7 @@ void VideoExample::SyncProcess()
196196
std::this_thread::sleep_for(std::chrono::milliseconds(1));
197197
#endif
198198

199-
WriteFrame(writer, frameInfo.m_frames[i]);
199+
WriteFrame(writer, frameInfo.m_frames[i].GetMatBGR());
200200
}
201201
}
202202

@@ -245,8 +245,7 @@ void VideoExample::AsyncProcess()
245245

246246
if (!m_isTrackerInitialized)
247247
{
248-
cv::UMat ufirst = frameInfo.m_frames[0].getUMat(cv::ACCESS_READ);
249-
m_isTrackerInitialized = InitTracker(ufirst);
248+
m_isTrackerInitialized = InitTracker(frameInfo.m_frames[0].GetUMatBGR());
250249
if (!m_isTrackerInitialized)
251250
{
252251
std::cerr << "CaptureAndDetect: Tracker initialize error!!!" << std::endl;
@@ -269,14 +268,14 @@ void VideoExample::AsyncProcess()
269268
int key = 0;
270269
for (size_t i = 0; i < m_batchSize; ++i)
271270
{
272-
DrawData(frameInfo.m_frames[i], framesCounter, currTime);
271+
DrawData(frameInfo.m_frames[i].GetMatBGR(), framesCounter, currTime);
273272

274-
WriteFrame(writer, frameInfo.m_frames[i]);
273+
WriteFrame(writer, frameInfo.m_frames[i].GetMatBGR());
275274

276275
++framesCounter;
277276

278277
#ifndef SILENT_WORK
279-
cv::imshow("Video", frameInfo.m_frames[0]);
278+
cv::imshow("Video", frameInfo.m_frames[0].GetMatBGR());
280279

281280
int waitTime = manualMode ? 0 : 1;// std::max<int>(1, cvRound(1000 / m_fps - currTime));
282281
key = cv::waitKey(waitTime);
@@ -360,7 +359,7 @@ void VideoExample::CaptureAndDetect(VideoExample* thisPtr, std::atomic<bool>& st
360359

361360
for (size_t i = 1; i < frameInfo.m_batchSize; ++i)
362361
{
363-
capture >> frameInfo.m_frames[i];
362+
capture >> frameInfo.m_frames[i].GetMatBGRWrite();
364363
if (frameInfo.m_frames[i].empty())
365364
{
366365
std::cerr << "CaptureAndDetect: frame is empty!" << std::endl;
@@ -376,8 +375,7 @@ void VideoExample::CaptureAndDetect(VideoExample* thisPtr, std::atomic<bool>& st
376375

377376
if (!thisPtr->m_isDetectorInitialized)
378377
{
379-
cv::UMat ufirst = frameInfo.m_frames[0].getUMat(cv::ACCESS_READ);
380-
thisPtr->m_isDetectorInitialized = thisPtr->InitDetector(ufirst);
378+
thisPtr->m_isDetectorInitialized = thisPtr->InitDetector(frameInfo.m_frames[0].GetUMatBGR());
381379
if (!thisPtr->m_isDetectorInitialized)
382380
{
383381
std::cerr << "CaptureAndDetect: Detector initialize error!!!" << std::endl;
@@ -409,30 +407,21 @@ void VideoExample::CaptureAndDetect(VideoExample* thisPtr, std::atomic<bool>& st
409407
///
410408
void VideoExample::Detection(FrameInfo& frame)
411409
{
412-
cv::UMat uframe;
410+
for (const auto& track : m_tracks)
411+
{
412+
if (track.m_isStatic)
413+
m_detector->ResetModel(frame.m_frames[0].GetUMatBGR(), track.m_rrect.boundingRect());
414+
}
413415

416+
std::vector<cv::UMat> frames;
414417
for (size_t i = 0; i < frame.m_frames.size(); ++i)
415418
{
416-
if (!m_detector->CanGrayProcessing())
417-
uframe = frame.m_frames[i].getUMat(cv::ACCESS_READ);
418-
else
419-
cv::cvtColor(frame.m_frames[i], uframe, cv::COLOR_BGR2GRAY);
420-
421-
if (i == 0)
422-
{
423-
for (const auto& track : m_tracks)
424-
{
425-
if (track.m_isStatic)
426-
m_detector->ResetModel(uframe, track.m_rrect.boundingRect());
427-
}
428-
}
429-
430-
m_detector->Detect(uframe);
431-
432-
const regions_t& regs = m_detector->GetDetects();
433-
434-
frame.m_regions[i].assign(std::begin(regs), std::end(regs));
419+
if (m_detector->CanGrayProcessing())
420+
frames.emplace_back(frame.m_frames[i].GetUMatGray());
421+
else
422+
frames.emplace_back(frame.m_frames[i].GetUMatBGR());
435423
}
424+
m_detector->Detect(frames, frame.m_regions);
436425
}
437426

438427
///
@@ -442,16 +431,12 @@ void VideoExample::Detection(FrameInfo& frame)
442431
///
443432
void VideoExample::Tracking(FrameInfo& frame)
444433
{
445-
cv::UMat uframe;
446-
447434
for (size_t i = 0; i < frame.m_frames.size(); ++i)
448435
{
449436
if (m_tracker->CanColorFrameToTrack())
450-
uframe = frame.m_frames[i].getUMat(cv::ACCESS_READ);
437+
m_tracker->Update(frame.m_regions[i], frame.m_frames[i].GetUMatBGR(), m_fps);
451438
else
452-
cv::cvtColor(frame.m_frames[i], uframe, cv::COLOR_BGR2GRAY);
453-
454-
m_tracker->Update(frame.m_regions[i], uframe, m_fps);
439+
m_tracker->Update(frame.m_regions[i], frame.m_frames[i].GetUMatGray(), m_fps);
455440
}
456441
}
457442

example/VideoExample.h

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,75 @@ class ResultsLog
157157
}
158158
};
159159

160+
///
161+
/// \brief The Frame struct
162+
///
163+
class Frame
164+
{
165+
public:
166+
Frame() = default;
167+
Frame(cv::Mat imgBGR)
168+
{
169+
m_mBGR = imgBGR;
170+
}
171+
172+
///
173+
bool empty() const
174+
{
175+
return m_mBGR.empty();
176+
}
177+
178+
///
179+
const cv::Mat& GetMatBGR()
180+
{
181+
return m_mBGR;
182+
}
183+
///
184+
cv::Mat& GetMatBGRWrite()
185+
{
186+
return m_mBGR;
187+
}
188+
///
189+
const cv::Mat& GetMatGray()
190+
{
191+
if (m_mGray.empty())
192+
cv::cvtColor(m_mBGR, m_mGray, cv::COLOR_BGR2GRAY);
193+
return m_mGray;
194+
}
195+
///
196+
const cv::UMat& GetUMatBGR()
197+
{
198+
if (m_umBGR.empty())
199+
m_umBGR = m_mBGR.getUMat(cv::ACCESS_READ);
200+
return m_umGray;
201+
}
202+
///
203+
const cv::UMat& GetUMatGray()
204+
{
205+
if (m_umGray.empty())
206+
{
207+
if (m_mGray.empty())
208+
{
209+
if (m_umBGR.empty())
210+
cv::cvtColor(m_mBGR, m_umGray, cv::COLOR_BGR2GRAY);
211+
else
212+
cv::cvtColor(m_umBGR, m_umGray, cv::COLOR_BGR2GRAY);
213+
}
214+
else
215+
{
216+
m_umGray = m_mGray.getUMat(cv::ACCESS_READ);
217+
}
218+
}
219+
return m_umGray;
220+
}
221+
222+
private:
223+
cv::Mat m_mBGR;
224+
cv::Mat m_mGray;
225+
cv::UMat m_umBGR;
226+
cv::UMat m_umGray;
227+
};
228+
160229
///
161230
/// \brief The FrameInfo struct
162231
///
@@ -187,7 +256,7 @@ struct FrameInfo
187256
m_frameInds.reserve(m_batchSize);
188257
}
189258

190-
std::vector<cv::Mat> m_frames;
259+
std::vector<Frame> m_frames;
191260
std::vector<regions_t> m_regions;
192261
std::vector<int> m_frameInds;
193262

src/Detector/BaseDetector.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,21 @@ class BaseDetector
3535
///
3636
virtual void Detect(const cv::UMat& frame) = 0;
3737

38+
///
39+
/// \brief Detect
40+
/// \param frames
41+
/// \param regions
42+
///
43+
virtual void Detect(const std::vector<cv::UMat>& frames, std::vector<regions_t>& regions)
44+
{
45+
for (size_t i = 0; i < frames.size(); ++i)
46+
{
47+
Detect(frames[i]);
48+
auto res = GetDetects();
49+
regions[i].assign(std::begin(res), std::end(res));
50+
}
51+
}
52+
3853
///
3954
/// \brief ResetModel
4055
/// \param img

0 commit comments

Comments
 (0)