我正在使用OpenCV打开并阅读几个网络摄像头。我一切都很好,但我似乎无法找到一种方法来了解相机是否可用。
我试过这段代码(凸轮2不存在):
import cv2
try:
c = cv2.VideoCapture(2)
except:
print "Cam 2 is invalid."
但这只会打印很多错误:
VIDEOIO ERROR: V4L: index 2 is not correct!
failed to open /usr/lib64/dri/hybrid_drv_video.so
Failed to wrapper hybrid_drv_video.so
failed to open /usr/lib64/dri/hybrid_drv_video.so
Failed to wrapper hybrid_drv_video.so
GStreamer Plugin: Embedded video playback halted; module v4l2src0 reported: Internal data stream error.
OpenCV Error: Unspecified error (GStreamer: unable to start pipeline
) in cvCaptureFromCAM_GStreamer, file /builddir/build/BUILD/opencv-3.2.0/modules/videoio/src/cap_gstreamer.cpp, line 832
VIDEOIO(cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_V4L2, reinterpret_cast<char *>(index))): raised OpenCV exception:
/builddir/build/BUILD/opencv-3.2.0/modules/videoio/src/cap_gstreamer.cpp:832: error: (-2) GStreamer: unable to start pipeline
in function cvCaptureFromCAM_GStreamer
OpenCV Error: Unspecified error (unicap: failed to get info for device
) in CvCapture_Unicap::initDevice, file /builddir/build/BUILD/opencv-3.2.0/modules/videoio/src/cap_unicap.cpp, line 139
VIDEOIO(cvCreateCameraCapture_Unicap(index)): raised OpenCV exception:
/builddir/build/BUILD/opencv-3.2.0/modules/videoio/src/cap_unicap.cpp:139: error: (-2) unicap: failed to get info for device
in function CvCapture_Unicap::initDevice
CvCapture_OpenNI::CvCapture_OpenNI : Failed to enumerate production trees: Can't create any node of the requested type!
<VideoCapture 0x7fa5b5de0450>
不会抛出任何异常。稍后使用c.read()
时,我会得到False
,但我想在我的程序的初始化阶段执行此操作。
那么,我如何找出我有多少有效相机或检查某个数字是否“映射”到有效相机?
答案 0 :(得分:6)
使用cv2.VideoCapture( invalid device number )
不会抛出异常。它会构造一个包含无效设备的<VideoCapture object>
- 如果您使用,则会获得异常。
测试None
和not isOpened()
的构造对象以清除无效对象。
对我来说这很有用(1台笔记本电脑相机设备):
import cv2 as cv
def testDevice(source):
cap = cv.VideoCapture(source)
if cap is None or not cap.isOpened():
print('Warning: unable to open video source: ', source)
testDevice(0) # no printout
testDevice(1) # prints message
输出1:
Warning: unable to open video source: 1
示例来自:https://github.com/opencv/opencv_contrib/blob/master/samples/python2/video.py 159ff
cap = cv.VideoCapture(source) if 'size' in params: w, h = map(int, params['size'].split('x')) cap.set(cv.CAP_PROP_FRAME_WIDTH, w) cap.set(cv.CAP_PROP_FRAME_HEIGHT, h) if cap is None or not cap.isOpened(): print 'Warning: unable to open video source: ', source
答案 1 :(得分:2)
Linux中提供的另一种解决方案是在/dev/videoX
调用中使用VideoCapture()
设备。当插入凸轮时,设备就在那里。与glob()
一起,获取所有摄像机是微不足道的:
import cv2, glob
for camera in glob.glob("/dev/video?"):
c = cv2.VideoCapture(camera)
当然需要使用c
对isOpened()
进行检查,但您确定只扫描可用的相机。
答案 2 :(得分:0)
您可以尝试以下代码:
from __future__ import print_function
import numpy as np
import cv2
# detect all connected webcams
valid_cams = []
for i in range(8):
cap = cv2.VideoCapture(i)
if cap is None or not cap.isOpened():
print('Warning: unable to open video source: ', i)
else:
valid_cams.append(i)
caps = []
for webcam in valid_cams:
caps.append(cv2.VideoCapture(webcam))
while True:
# Capture frame-by-frame
for webcam in valid_cams:
ret, frame = caps[webcam].read()
# Display the resulting frame
cv2.imshow('webcam'+str(webcam), frame)
k = cv2.waitKey(1)
if k == ord('q') or k == 27:
break
# When everything done, release the capture
for cap in caps:
cap.release()
cv2.destroyAllWindows()
答案 3 :(得分:0)
这是一个“无法正常工作” 的解决方案,可帮助您防止在这种陷阱中翻滚:
componentDidUpdate