Listing available devices in python-opencv

Posted on

Question :

Listing available devices in python-opencv

I have two webcams attached to my laptop (one built in), both of which work. (If I use Cheese, a webcam thingy that comes with Ubuntu, it uses the external one). If I use

cap = cv.CreateCameraCapture(0)


cap = cv.CreateCameraCapture(-1)

I get my built in webcam. If I use

cap = cv.CreateCameraCapture(1)

It doesn’t work and the object `cap’ displays as:

<Capture (nil)>

Same with CaptureFromCAM. So I’d like to know what openCV is trying to do and why it doesn’t seem to know about the second camera. There should be two devices available (there are /dev/videoN entries for both).

Asked By: Lucas


Answer #1:

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:

Answered By: Sam

Answer #2:

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[0]:
    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.

Answered By: Patrick Yeadon

Answer #3:

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[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)
        index += 1
        i -= 1
    return arr

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

Answered By: pydrink

Answer #4:

For windows, you can build a .pyd extension.

There isn’t an easy crossplatform way yet; ideally someone collates a solution for each OS and then builds them into .pyd.

Answered By: aong152

Answer #5:

I believe that under Linux the valid indices for video inputs are the numbers of the videoN devices in the /dev directory.
Hence the following will give a list of valid indices:

import os
devs = os.listdir('/dev')
vid_indices = [int(dev[-1]) for dev in devs 
               if dev.startswith('video')]
vid_indices = sorted(vid_indices)
Answered By: Brandon Bennett

Answer #6:

I think you should try this:

import cv2

cap = cv2.VideoCapture(1)

while True:
    _, frame =
    cv2.imshow('frame', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
Answered By: N.S

Leave a Reply

Your email address will not be published. Required fields are marked *