一次从 OpenCV 中的两个摄像头捕获视频 [英] Capturing video from two cameras in OpenCV at once

查看:35
本文介绍了一次从 OpenCV 中的两个摄像头捕获视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 Python API 使用 OpenCV 同时(或几乎)从两个或多个摄像头捕获视频?

How do you capture video from two or more cameras at once (or nearly) with OpenCV, using the Python API?

我有三个网络摄像头,它们都能够进行视频流传输,分别位于/dev/video0、/dev/video1 和/dev/video2.

I have three webcams, all capable of video streaming, located at /dev/video0, /dev/video1, and /dev/video2.

教程为例,从单个相机很简单:

Using the tutorial as an example, capturing images from a single camera is simply:

import cv2
cap0 = cv2.VideoCapture(0)
ret0, frame0 = cap0.read()
cv2.imshow('frame', frame0)
cv2.waitKey()

这很好用.

但是,如果我尝试初始化第二个摄像头,尝试从它read() 返回 None:

However, if I try to initialize a second camera, attempting to read() from it returns None:

import cv2
cap0 = cv2.VideoCapture(0)
cap1 = cv2.VideoCapture(1)
ret0, frame0 = cap0.read()
assert ret0 # succeeds
ret1, frame1 = cap1.read()
assert ret1 # fails?!

为了确保我不会不小心给 OpenCV 一个糟糕的相机索引,我单独测试了每个相机索引,它们都可以独立工作.例如

Just to ensure I wasn't accidentally giving OpenCV a bad camera index, I tested each camera index individually and they all work by themselves. e.g.

import cv2
#cap0 = cv2.VideoCapture(0)
cap1 = cv2.VideoCapture(1)
#ret0, frame0 = cap0.read()
#assert ret0
ret1, frame1 = cap1.read()
assert ret1 # now it works?!

我做错了什么?

我的硬件是运行 Ubuntu 的 Macbook Pro.专门在 Macbook 上研究这个问题,我发现其他人也遇到了这个问题,无论是在 OSX 上还是在不同类型的相机上.如果我访问 iSight,我的代码中的两个调用都会失败.

My hardware is a Macbook Pro running Ubuntu. Researching the issue specifically on Macbooks, I've found others that have run into this problem too, both on OSX and with different types of cameras. If I access the iSight, both calls in my code fail.

推荐答案

是的,您肯定受到 USB 带宽的限制.尝试以 full-rez 从两个设备读取您可能会出错:

Yes you're definitely limited by the USB bandwidth. Attempting to read from both devices at full-rez you probably got error:

libv4l2: error turning on stream: No space left on device
VIDIOC_STREAMON: No space left on device
Traceback (most recent call last):
  File "p.py", line 7, in <module>
    assert ret1 # fails?!
AssertionError

然后当您将分辨率降低到 160x120 时:

And then when you reduce the res to 160x120:

import cv2
cap0 = cv2.VideoCapture(0)
cap0.set(3,160)
cap0.set(4,120)
cap1 = cv2.VideoCapture(1)
cap1.set(3,160)
cap1.set(4,120)
ret0, frame0 = cap0.read()
assert ret0 # succeeds
ret1, frame1 = cap1.read()
assert ret1 # fails?!

现在看来可以了!我敢打赌,您的两个摄像头都连接在同一个 USB 卡上.您可以运行 lsusb 命令来确定,它应该显示如下内容:

now it seems to work! I bet you have both cams connected on the same USB card. You can run lsusb command to make sure, and it should indicate something like:

Bus 001 Device 006: ID 046d:081b Logitech, Inc. Webcam C310
Bus 001 Device 004: ID 0409:005a NEC Corp. HighSpeed Hub
Bus 001 Device 007: ID 046d:0990 Logitech, Inc. QuickCam Pro 9000
Bus 001 Device 005: ID 0409:005a NEC Corp. HighSpeed Hub
Bus 001 Device 003: ID 0409:005a NEC Corp. HighSpeed Hub
Bus 001 Device 002: ID 1058:0401 Western Digital Technologies, Inc. 
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub

(注意同一总线上的两个摄像头.)如果可能,您可以向您的机器添加另一个 USB 卡以获得更多带宽.我以前这样做是为了在一台机器上以全分辨率运行多个凸轮.尽管那是一个带有可用主板插槽的塔式工作站,但不幸的是,您在 MacBook 笔记本电脑上可能没有该选项.

(Note both cameras on same bus.) If possible, you can add another USB card to your machine to gain more bandwidth. I've done this before in order to run multiple cams at full resolution on a single machine. Albeit that was a tower workstation with available motherboard slots, and unfortunately you may not have that option on a MacBook laptop.

这篇关于一次从 OpenCV 中的两个摄像头捕获视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆