Rui Huang
Rui Huang

Reputation: 382

Opencv capture video from multiple cameras

I am verifying multiple video captures by Opencv 3.4, there are 3 cameras in use, one built-in camera in the laptop and 2 USB cameras connected two separated USB ports. I can't make the video captures happen, it always throws exception as:

libv4l2: error setting pixformat: Device or resource busy
OpenCV Error: Unspecified error (GStreamer: unable to start pipeline) in cvCaptureFromCAM_GStreamer, file /opt/opencv/modules/videoio/src/cap_gstreamer.cpp, line 890
VIDEOIO(cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_V4L2, reinterpret_cast<char *>(index))): raised OpenCV exception:

/opt/opencv/modules/videoio/src/cap_gstreamer.cpp:890: error: (-2) GStreamer: unable to start pipeline in function cvCaptureFromCAM_GStreamer

libv4l2: error setting pixformat: Device or resource busy
OpenCV Error: Unspecified error (GStreamer: unable to start pipeline) in cvCaptureFromCAM_GStreamer, file /opt/opencv/modules/videoio/src/cap_gstreamer.cpp, line 890
VIDEOIO(cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_V4L2, reinterpret_cast<char *>(index))): raised OpenCV exception:

/opt/opencv/modules/videoio/src/cap_gstreamer.cpp:890: error: (-2) GStreamer: unable to start pipeline in function cvCaptureFromCAM_GStreamer

cap1 doesn't work

Source code is quite simple:

using namespace cv;

int main(int, char**)
{
    VideoCapture cap0(0); // open the default camera
    VideoCapture cap1(1);
    VideoCapture cap2(2);
    if(!cap0.isOpened()) {
        std::cout << "cap0 doesn't work" << std::endl;
        return -1;
    }

    if(!cap1.isOpened()) {
        std::cout << "cap1 doesn't work" << std::endl;
        return -1;
    }

    if(!cap2.isOpened()) {
        std::cout << "cap2 doesn't work" << std::endl;
        return -1;
    }

    Mat frame0;
    Mat frame1;
    Mat frame2;
    for(;;)
    {

        cap0 >> frame0; // get a new frame from camera
        cap1 >> frame1;
        cap2 >> frame2;
        imshow("Video0", frame0);
        imshow("Video1", frame1);
        imshow("Video2", frame2);
        if(waitKey(30) >= 0) break;
    }

    return 0;
}

All cameras are recognized:

crw-rw----+ 1 root video 81, 0  1月 14 09:05 /dev/video0
crw-rw----+ 1 root video 81, 1  1月 14 09:30 /dev/video1
crw-rw----+ 1 root video 81, 2  1月 14 10:11 /dev/video2

I am using Ubuntu 14.04.

Any idea how to make multiple video captures happen?

Upvotes: 2

Views: 2360

Answers (1)

Dave B
Dave B

Reputation: 51

The first thing with multiple USB cameras, and unrelated to OpenCV is the USB bandwidth, which rarely supports more than one camera bandwidth per USB port, for which it is recommended to connect each camera to a separate USB port, and NOT to a USB hub.

The second thing (my experience in Ubuntu 16.04 and 18.04; haven't tested in Windows) that some cameras will take two indexes, such that if there are two cameras, you can try opening camera index 0 for the first camera, and camera index 2 for the second:

    VideoCapture camA(0);
    VideoCapture camB(2);

And third, it is good practice to control the apiPreference for the camera stream. For example, to use Linux V4L2 and remove GStreamer from the video pipeline, instead of the above use this (OpenCV must have been built selecting WITH_CAP_V4L2):

    VideoCapture camA(0, CAP_V4L2);
    VideoCapture camB(2, CAP_V4L2);

Upvotes: 2

Related Questions