Linux 在 python-opencv 中列出可用设备

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8044539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 07:05:16  来源:igfitidea点击:

Listing available devices in python-opencv

pythonlinuxopencvhardware

提问by Lucas

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

我的笔记本电脑上连接了两个网络摄像头(一个内置),它们都可以工作。(如果我使用 Cheese,Ubuntu 附带的一种网络摄像头,它会使用外部摄像头)。如果我使用

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:

它不起作用,对象“cap”显示为:

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

与 CaptureFromCAM 相同。所以我想知道 openCV 试图做什么以及为什么它似乎不知道第二个摄像头。应该有两个设备可用(两者都有 /dev/videoN 条目)。

采纳答案by Sam

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:

这是 OpenCV 的一个普遍问题,如下所示。似乎只有内置的或第一个 USB cam(仅当您没有内置 cam 时)才能在 OpenCV 中工作:

How to use a camera with OpenCV

如何在 OpenCV 中使用相机

Cannot access usb webcam through OpenCV, Cygwin

无法通过 OpenCV、Cygwin 访问 USB 网络摄像头

OpenCV capture from USB not iSight (OSX)

从 USB 而非 iSight (OSX) 捕获 OpenCV

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

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

回答by N.S

I think you should try this:

我认为你应该试试这个:

import cv2

cap = cv2.VideoCapture(1)

while True:
    _, frame = cap.read()
    cv2.imshow('frame', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

回答by Patrick Yeadon

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.

此方法返回读取时返回某些内容的所有索引的列表;我相信它可以改进,但几乎没有超过几个网络摄像头,而且运行速度非常快。

回答by pydrink

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

@Patrick 的回答很好,但我想对其进行改进并且无法发表评论。

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.

我认为 Patricks 设置假定相机之间没有空索引。但就我而言,我的内置摄像头在索引 0 处,USB 网络摄像头在索引 2 处。所以“if not cap.read()[0]”在索引 1 处跳出 while 循环,永远不会捕捉到其他的. 我们必须指定我们愿意检查和检查的索引数量,而不是添加那些为空的索引。

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!

这成功地为我提供了所需的索引。再次感谢帕特里克的布局!