Skip to content

Commit 464d815

Browse files
committed
Added example with future|async
1 parent 2fc2a1a commit 464d815

2 files changed

Lines changed: 138 additions & 0 deletions

File tree

example/VideoExample.cpp

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "VideoExample.h"
22
#include <iomanip>
33
#include <ctime>
4+
#include <future>
45

56
#define MULTITHREADING_LOGS 0
67

@@ -176,6 +177,142 @@ void VideoExample::SyncProcess()
176177
#endif
177178
}
178179

180+
///
181+
/// \brief VideoExample::FutureProcess
182+
///
183+
void VideoExample::FutureProcess()
184+
{
185+
m_currFrame = 1;
186+
187+
cv::VideoWriter writer;
188+
189+
#ifndef SILENT_WORK
190+
cv::namedWindow("Video", cv::WINDOW_NORMAL | cv::WINDOW_KEEPRATIO);
191+
bool manualMode = false;
192+
#endif
193+
194+
double freq = cv::getTickFrequency();
195+
196+
int64 allTime = 0;
197+
198+
int framesCounter = m_startFrame + 1;
199+
200+
cv::VideoCapture capture;
201+
if (!OpenCapture(capture))
202+
{
203+
LOG_ERR_TIME << "Can't open " << m_inFile << std::endl;
204+
return;
205+
}
206+
207+
FrameInfo frameInfo[2];
208+
int currFrame = 0;
209+
210+
capture >> frameInfo[currFrame].m_frame;
211+
if (frameInfo[currFrame].m_frame.empty())
212+
{
213+
LOG_ERR_TIME << "Can't capture first frame" << std::endl;
214+
return;
215+
}
216+
cv::cvtColor(frameInfo[currFrame].m_frame, frameInfo[currFrame].m_gray, cv::COLOR_BGR2GRAY);
217+
218+
if (!m_isTrackerInitialized)
219+
{
220+
m_isTrackerInitialized = InitTracker(frameInfo[currFrame].m_gray);
221+
if (!m_isTrackerInitialized)
222+
{
223+
std::cerr << "CaptureAndDetect: Tracker initilize error!!!" << std::endl;
224+
return;
225+
}
226+
}
227+
228+
int k = 0;
229+
230+
int64 startLoopTime = cv::getTickCount();
231+
232+
struct CaptureAndDetect2
233+
{
234+
CaptureAndDetect2(VideoExample* parent, cv::VideoCapture& capture)
235+
: m_parent(parent), m_capture(capture)
236+
{
237+
238+
}
239+
240+
VideoExample* m_parent = nullptr;
241+
cv::VideoCapture& m_capture;
242+
243+
bool operator()(FrameInfo* frameInfo)
244+
{
245+
m_capture >> frameInfo->m_frame;
246+
if (!frameInfo->m_frame.empty())
247+
{
248+
cv::cvtColor(frameInfo->m_frame, frameInfo->m_gray, cv::COLOR_BGR2GRAY);
249+
m_parent->Detection(frameInfo->m_frame, frameInfo->m_gray, frameInfo->m_regions);
250+
return true;
251+
}
252+
return false;
253+
}
254+
};
255+
256+
CaptureAndDetect2 cad(this, capture);
257+
258+
for (; k != 27; )
259+
{
260+
auto detectResult = std::async(std::launch::async, std::ref(cad), (currFrame ? &frameInfo[0] : &frameInfo[1]));
261+
262+
int64 t1 = cv::getTickCount();
263+
264+
Tracking(frameInfo[currFrame].m_frame, frameInfo[currFrame].m_gray, frameInfo[currFrame].m_regions);
265+
266+
int64 t2 = cv::getTickCount();
267+
268+
allTime += t2 - t1;
269+
int currTime = cvRound(1000 * (t2 - t1) / freq);
270+
271+
DrawData(frameInfo[currFrame].m_frame, framesCounter, currTime);
272+
273+
#ifndef SILENT_WORK
274+
cv::imshow("Video", frameInfo[currFrame].m_frame);
275+
276+
int waitTime = manualMode ? 0 : std::max<int>(1, cvRound(1000 / m_fps - currTime));
277+
k = cv::waitKey(waitTime);
278+
if (k == 'm' || k == 'M')
279+
{
280+
manualMode = !manualMode;
281+
}
282+
#else
283+
std::this_thread::sleep_for(std::chrono::milliseconds(1));
284+
#endif
285+
286+
WriteFrame(writer, frameInfo[currFrame].m_frame);
287+
288+
if (detectResult.wait_for(std::chrono::milliseconds(m_captureTimeOut)) == std::future_status::timeout)
289+
{
290+
std::cerr << "Process: Frame capture timeout" << std::endl;
291+
break;
292+
}
293+
if (!detectResult.get())
294+
{
295+
std::cerr << "Can't capture " << framesCounter << " frame" << std::endl;
296+
break;
297+
}
298+
299+
currFrame = currFrame ? 0 : 1;
300+
++framesCounter;
301+
if (m_endFrame && framesCounter > m_endFrame)
302+
{
303+
std::cout << "Process: riched last " << m_endFrame << " frame" << std::endl;
304+
break;
305+
}
306+
}
307+
308+
int64 stopLoopTime = cv::getTickCount();
309+
310+
std::cout << "algorithms time = " << (allTime / freq) << ", work time = " << ((stopLoopTime - startLoopTime) / freq) << std::endl;
311+
#ifndef SILENT_WORK
312+
cv::waitKey(m_finishDelay);
313+
#endif
314+
}
315+
179316
///
180317
/// \brief VideoExample::Process
181318
///

example/VideoExample.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class VideoExample
7070

7171
void AsyncProcess();
7272
void SyncProcess();
73+
void FutureProcess();
7374

7475
protected:
7576
std::unique_ptr<BaseDetector> m_detector;

0 commit comments

Comments
 (0)