Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
686 views
in Technique[技术] by (71.8m points)

linux - 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)

or

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).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...