Ask Your Question
0

VideoCapture - passed invalid camera ID - revised

asked 2017-01-23 15:44:35 -0600

Vaclav gravatar image

updated 2017-01-26 10:24:08 -0600

VideoCapture cap(0); // open the default camera if(!cap.isOpened()) // check if we succeeded return -1;

The above snippet will fail if invalid parameter is passed to the constructor. The error returned exits the program.

How do I validate the camera ID prior to executing the VideoCapture? I need to control the program flow and NOT exit the program via VideoCapture class.

Revision

OK , I have better understanding how to access video camera, however, I still need some more assistance.

Here is my my debugging code for (int iVideoID = 0; iVideoID != 9; iVideoID ++) {

            if (cap.open(iVideoID)) {
                cerr << "success - opened video cam ID " << iVideoID
                        << endl;
                //return 15;
                break; //       return -1;
            } else {
                cerr << "Cannot open the video cam " << iVideoID
                        << endl;
                if(iVideoID  != 8)
                cerr << " try next ID " << endl;
                else
                {
                    cerr << "Failed to access any video camera " << iVideoID
                                                << endl;

ifdef TRACE

                    cerr << "Failed to access any video camera " << iVideoID
                                                                        << endl;
        cout << __func__ << endl;
        cerr << __LINE__ << endl;
        cerr << "EXIT "<< endl;
        return 42 ;

endif

It does do what I want, but... When there are NO USB cameras on the system , it correctly exists after ID is > 8 . No problem there. But when there is a USB camera and it is not accessible I get ton of library errors. I like to gracefully exit the application instead of collecting all these however valid errors.

Here is a copy of my outputs:

int main(int argc, char **argv) main libv4l2: error setting pixformat: Device or resource busy VIDEOIO ERROR: libv4l unable to ioctl S_FMT libv4l2: error setting pixformat: Device or resource busy libv4l1: error setting pixformat: Device or resource busy main libv4l2: error setting pixformat: Device or resource busy libv4l1: error setting pixformat: Device or resource busy VIDEOIO ERROR: libv4l unable to ioctl VIDIOCSPICT

Cannot open the video cam 0 try next ID VIDEOIO ERROR: V4L: index 1 is not correct! Cannot open the video cam 1 try next ID VIDEOIO ERROR: V4L: index 2 is not correct! Cannot open the video cam 2 try next ID VIDEOIO ERROR: V4L: index 3 is not correct! Cannot open the video cam 3 try next ID VIDEOIO ERROR: V4L: index 4 is not correct! Cannot open the video cam 4 try next ID VIDEOIO ERROR: V4L: index 5 is not correct! Cannot open the video cam 5 try next ID VIDEOIO ERROR: V4L: index 6 is not correct!

Basically - how do I access / process the lib4l2 errors ?

I did some search on this and found all of such problems are blamed on application. However, one discussion was about "USB bandwidth" or lack of it . Please NOTE that when my code works , when I can access the camera , it works. So if the USB bandwidth is the issue , I just want to recover from it. Essentially looking for a function which would return the error , not just true / false as cap.open(iVideoID) does. As it stands - "open" just sort of hides the errors and returns false, which it suppose to do. I hope I am making sense.

Any ... (more)

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-01-24 04:27:51 -0600

pi-null-mezon gravatar image

updated 2017-01-24 04:28:10 -0600

Change the logic to:

...
cv::VideoCapture _vc;
if(_vc.open(_ID)) {
   // DO THINGS
} else {
   std::cerr << "Can not open video device with id: " << _ID;
}
...
edit flag offensive delete link more

Comments

Thanks , my next step wast to read the source code. Appreciate your answer.

Vaclav gravatar imageVaclav ( 2017-01-24 09:13:30 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-01-23 15:44:35 -0600

Seen: 1,768 times

Last updated: Jan 26 '17