Luc*_*cas 7 python linux hardware opencv
我的笔记本电脑连接了两个摄像头(一个内置),两个都可以工作.(如果我使用Cheese,Ubuntu附带的网络摄像头,它使用外部的).如果我使用
cap = cv.CreateCameraCapture(0)
Run Code Online (Sandbox Code Playgroud)
要么
cap = cv.CreateCameraCapture(-1)
Run Code Online (Sandbox Code Playgroud)
我得到了内置摄像头.如果我使用
cap = cv.CreateCameraCapture(1)
Run Code Online (Sandbox Code Playgroud)
它不起作用,对象'cap'显示为:
<Capture (nil)>
Run Code Online (Sandbox Code Playgroud)
与CaptureFromCAM相同.所以我想知道openCV正在尝试做什么,为什么它似乎不知道第二台相机.应该有两个设备可用(两者都有/ dev/videoN条目).
pyd*_*ink 17
@Patrick 的回答很好,但我想对其进行改进并且无法发表评论。
我认为 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
Run Code Online (Sandbox Code Playgroud)
这成功地为我提供了所需的索引。再次感谢帕特里克的布局!
这是OpenCV的一般问题,如下所示.似乎只有内置或第一个USB凸轮(只有你没有构建凸轮)才能在OpenCV中运行:
目前,无法提取此功能请求中列出的摄像机数量:
https://code.ros.org/trac/opencv/ticket/935
小智 5
通过遍历网络摄像头索引,直到读取该摄像头不再返回任何内容,我已经能够解决此问题:
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
Run Code Online (Sandbox Code Playgroud)
该方法返回所有索引的列表,这些索引在读取时将返回某些内容。我敢肯定它可以改进,但是几乎没有几个网络摄像头,而且运行起来很快。