VideoCapture: access the same webcam from two threads
Simple question but I have been struggling a lot trying to implement a solution: I have a c++ program with two threads. One is a motion detection algorithm and the other one records a video. The Detector
tells the Recorder
when to start and stop the video recording. They both need to read video frames from the same webcam. How can I achieve this?
If I create two VideoCapture
objects in each class the program crashes on start when it tries to open the second VideoCapture
with the same index. There must be a way around that.
//edit: Code for my Camera
where I tried to implement a thread safe get function. I think something is wrong with my attempt
Camera::Camera(){
myWebcam.open(INDEX);
myWebcam.set(CV_CAP_PROP_FRAME_WIDTH, WIDTH);
myWebcam.set(CV_CAP_PROP_FRAME_HEIGHT, HEIGHT);
}
cv::Mat Camera::getWebcamFrame(){
myMutex.lock();
myWebcam.read(videoFrame);
myMutex.unlock();
return videoFrame;
}
It says "1 Answer" but I can't see anything?:(
Don't lock the camera read, lock the 'get' from the buffer where you store the frames captured from the camera thread. If you have two processing threads, they each need access to the buffer where the camera capture thread stores the captured frames.
I can see it, but I see 2 answers... Is there a bug on the site? I have also seen 1 answer and nothing posted.