Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.

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;

}

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;
}

}

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;
}