-
Notifications
You must be signed in to change notification settings - Fork 1
/
mutexvideocapture.cpp
102 lines (87 loc) · 2 KB
/
mutexvideocapture.cpp
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
//test opencv in threaded way
//capture images in a thread
//process in another thread
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <cstdio>
#include <thread>
#include <mutex>
using namespace std;
using namespace cv;
/** Function Headers */
void detectAndDisplay();
/** Global variables */
String video_read_path = "/home/nimo/NewCamera/RealTimePoseEstimation_WithAurcoDetect_Data/box_with_60FPS.avi";
string window_name = "Video";
Mat frm;
mutex mtx;
int c=0;
bool is_frame_prepare;
bool isMainRunning;
void run_capture()
{
VideoCapture capture;
capture.open(video_read_path);
int count = 0;
if( capture.isOpened() )
{
while (isMainRunning)
{
mtx.lock();
if (is_frame_prepare) {
// Nothing to do
} else {
cout <<"frame: "<< count++ <<" \n";
capture.read(frm);
if (frm.empty()) {
isMainRunning = false;
}
is_frame_prepare = true;
}
mtx.unlock();
}
}
}
/**
* @function detectAndDisplay
*/
void detect_and_display()
{
bool isExist;
Mat frame;
while (isMainRunning)
{
isExist = false;
mtx.lock();
if (is_frame_prepare) {
cout <<"get frame\n";
isExist = true;
frm.copyTo(frame);
is_frame_prepare = false;
}
mtx.unlock();
if (isExist)
{
//-- Show what you got
imshow(window_name, frame);
c = waitKey(1);
if( (char)c == 'c' ) {
break;
}
} else {
cout <<"sleep\n";
std::this_thread::sleep_for (std::chrono::milliseconds(1));
}
}
}
int main()
{
is_frame_prepare = false;
isMainRunning = true;
thread cap(run_capture);
thread dis(detect_and_display);
cap.join();
dis.join();
return 0;
}