0

I have Raspberry pi 4 with 8GB ram, I have connected two USB webcams (Eternico Webcam ET101 HD) to it, I need to read video from both cameras at once for triangulation of detected objects.

I am using C++ code from OpenCV docs: https://docs.opencv.org/3.4/d8/dfe/classcv_1_1VideoCapture.html

#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <stdio.h>
using namespace cv;
using namespace std;
int main(int, char**)
{
    Mat frame;
    //--- INITIALIZE VIDEOCAPTURE
    VideoCapture cap;
    // open the default camera using default API
    // cap.open(0);
    // OR advance usage: select any API backend
    int deviceID = 0;             // 0 = open default camera
    int apiID = cv::CAP_ANY;      // 0 = autodetect default API
    // open selected camera using selected API
    cap.open(deviceID, apiID);
    // check if we succeeded
    if (!cap.isOpened()) {
        cerr << "ERROR! Unable to open camera\n";
        return -1;
    }
    //--- GRAB AND WRITE LOOP
    cout << "Start grabbing" << endl
        << "Press any key to terminate" << endl;
    for (;;)
    {
        // wait for a new frame from camera and store it into 'frame'
        cap.read(frame);
        // check if we succeeded
        if (frame.empty()) {
            cerr << "ERROR! blank frame grabbed\n";
            break;
        }
        // show live and wait for a key with timeout long enough to show images
        imshow("Live", frame);
        if (waitKey(5) >= 0)
            break;
    }
    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0;
}

Problem is when I try to load video from both cameras at once, whole video stream just frezzes and I have to kill it.

I tried to use python code for OpenCV, but with same behaviour.

I ran it as separate processes, just with different deviceID -> not working (The first camera opens correctly, when the second tries to open it freezes and I have to kill it)

I also tried it like this: https://stackoverflow.com/a/13926879/17053282 -> not working - same as above

After searching for answers I found out that problem could be related to USB bandwidth, but.. Interesting is that when I tried to load video using JS in web browser it works perfectly fine. Screen below..

Live video from both cameras displayed in webrowser

Any ideas what could be wrong? Or how to run it in C++?

Goal: I need to read video from cameras simultaneously, and stream it over network in C++.

2
  • 1
    attempt to reproduce this with something other than opencv. use guvcview or ffmpeg or whatever. Commented Nov 26, 2023 at 13:34
  • Did you try specifying the backend (apiID) explicitly, rather than depending on auto-detection? | What backend does it actually use when auto-detecting? | Also, if you just need to stream the video from a camera, do you really need to write your own program using OpenCV, as opposed to just using some existing solution? Commented Nov 26, 2023 at 22:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.