Skip to content
Advertisement

List available cameras OpenCV/Python

I have multiple webcams connected to my PC and I would like to select one camera based on its info (name, resolution etc.). Is there a way to list all the cameras available on a PC, instead of trying all the indices in cv2.VideoCapture()?

Advertisement

Answer

The answer is negative. OpenCV doesn’t have a method for listing the available video capture devices on your system. If you look at the code you see how currently OpenCV handles invalid device indices that don’t exist. For instance for MacOS here is the code:

if ( cameraNum < 0 || devices.count <= NSUInteger(cameraNum) ) {
    fprintf(stderr, "OpenCV: out device of bound (0-%ld): %dn", devices.count-1, cameraNum);
    [localpool drain];
    return 0;
}

You see devices.count returns the number of available devices but OpenCV doesn’t have a method to return that to the user.

The relevant code for Windows is here:

if ((unsigned)m_deviceID >= m_devices.Get()->Size)
{
    OutputDebugStringA("Video::initGrabber - no video device foundn");
    return false;
}

Again there is no function for returning m_devices.Get()->Size to the user. The Linux code is a bit more complex.

If you’re building OpenCV from code you could add a function that returns the number of available devices. Or even better submit a pull request to OpenCV with your patch.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement