Listing available devices in python-opencv

I have been able to work around this problem by iterating over the webcam indexes until reading that camera no longer returns anything:

index = 0
arr = []
while True:
    cap = cv2.VideoCapture(index)
    if not cap.read()[0]:
        break
    else:
        arr.append(index)
    cap.release()
    index += 1
return arr

This method returns a list of all indexes that return something when read; I'm sure it can be improved upon, but there are hardly ever more than a few webcams and this runs pretty quickly.


Great answer by @Patrick, but I'd like to improve on it and can't comment yet.

I think Patricks setup assumes that the cameras do not have empty indexes in between them. But in my case, my built-in camera was at index 0, and USB webcam was at index 2. So "if not cap.read()[0]" broke out of the while loop at index 1, never catching the others. We have to specify how many indexes we're willing to go over and check, and just not add the ones that are null.

def returnCameraIndexes():
    # checks the first 10 indexes.
    index = 0
    arr = []
    i = 10
    while i > 0:
        cap = cv2.VideoCapture(index)
        if cap.read()[0]:
            arr.append(index)
            cap.release()
        index += 1
        i -= 1
    return arr

This successfully gave me the indexes I need. Again, thanks to Patrick for the layout!


This is a general problem of the OpenCV, as you can see below. It seems that only the builtin, or the first USB cam (only if you do not have a buildin cam) works in OpenCV:

How to use a camera with OpenCV

Cannot access usb webcam through OpenCV, Cygwin

OpenCV capture from USB not iSight (OSX)

Currently, there is no way to extract the number of cameras, as listed in this feature request:

https://code.ros.org/trac/opencv/ticket/935


It is possible to create a list of cameras without using cap.read().

import cv2

index = 0
arr = []
while True:
    cap = cv2.VideoCapture(index)
    try:
        if cap.getBackendName()=="MSMF":
            arr.append(index)
    except:
        break
    cap.release()
    index += 1

print(arr)