Unable to read frames from VideoCapture from a secondary webcam using OpenCV

the code:

A simple example that works great with the main webcam (device 0):

VideoCapture cap(0); if (!cap.isOpened()) { std::cout << "Unable to read stream from specified device." << std::endl; return; } while (true) { // retrieve the frame: Mat frame; if (!cap.read(frame)) { std::cout << "Unable to retrieve frame from video stream." << std::endl; break; } // display it: imshow("MyVideo", frame); // check if Esc has been pressed: if (waitKey(1) == 27) { break; } // else continue: } cap.release(); 

Problem:

I have a second webcam that I would like to use. However, when I replace VideoCapture cap(0); on the VideoCapture cap(1); . , the thread opens correctly (or at least cap.isOpened() returns true ) , but calling cap.read(frame) returns false > , and I cannot find out why.

What I tried:

  • I'm trying to play with the VideoCapture settings a bit like a call:

     cap.set(CV_CAP_PROP_FORMAT, CV_8UC3); 

    and random things like this, but nothing helps.

  • I also found this: VideoCapture :: read with an error on an uncompressed video (error No. 2281) , which seems to be resolved on version 2.4.7 .. but I just upgraded OpenCV to 2.4.8 and it still doesn't work. ..

  • I tried using AMCap to capture raw video from this camera, save it as aaa.avi file and built VideoCapture by calling:

     VideoCapture cap("aaa.avi"); 

    and it works (when reading from a file) ... I need real-time processing with live viewing.

HW, OS, SW details:

My HW: HP ProBook 4510s with integrated webcam that always works great
+ CANYON CNR-FWCII3 external webcam designated by the OS as β€œUSB Video Device” (complex) OS, SW: Windows 8.1 Pro x86, Visual Studio 2012 Pro, OpenCV 2.4.8 ~ using the vc11 assembly

Questions:

  • Did I miss something?
  • Is there anything else I could do?
  • Is there any way to get more information about what could actually be?

... the OpenCV API seems pretty bad in this case and wherever people seem to run into a similar problem, someone claimed it was an "OS / HW depnendant" as an excuse.

Any help would be appreciated.

+7
c ++ opencv video video-capture usb
source share
2 answers

After a while, I found out that this is only the first read call that fails, and skipping the first frame started working fine, although the true reason for this behavior remained unknown.

Later James Barnett (see comments above) pointed out that the reason may be that it takes some time until the camera is ready to capture, and my current solution is as follows (C ++ 11 sleep):

 #include <chrono> #include <thread> ... VideoCapture cap(1); // give camera some extra time to get ready: std::this_thread::sleep_for(std::chrono::milliseconds(200)); if (!cap.isOpened()) { std::cout << "Unable to read stream from specified device." << std::endl; return; } while (true) { // retrieve the frame: Mat frame; if (!cap.read(frame)) { std::cout << "Unable to retrieve frame from video stream." << std::endl; continue; } // display it: imshow("LiveStream", frame); // stop if Esc has been pressed: if (waitKey(1) == 27) { break; } } cap.release(); 

We hope that some future visitors will find this useful :)

+5
source share

The easiest way to decide is to read it once before checking for success. This piece of code works for me. //

 cap.read(frame); if(!cap.read(frame)){ 

// ...

0
source share

All Articles