如何正确检查相机是否可用? [英] How to correctly check if a camera is available?
问题描述
我正在使用OpenCV打开和读取多个网络摄像头.一切正常,但是我似乎找不到找到相机可用的方法.
I am using OpenCV to open and read from several webcams. It all works fine, but I cannot seem to find a way to know if a camera is available.
我尝试了此代码(摄像头2不存在):
I tried this code (cam 2 does not exist):
import cv2
try:
c = cv2.VideoCapture(2)
except:
print "Cam 2 is invalid."
但这只会打印出很多错误:
But this just prints a lot of errors:
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
,但是我想在程序的初始化阶段执行此操作.
No exception is thrown. When using c.read()
later, I do get False
, but I would like to do this in the initialisation phase of my program.
那么,如何确定我拥有多少个有效摄像机,或者如何确定某个数量映射"到一个有效摄像机?
So, how do I find out how many valid cameras I have or check if a certain number 'maps' to a valid one?
推荐答案
使用cv2.VideoCapture( invalid device number )
不会引发异常.它构造了一个包含无效设备的<VideoCapture object>
-如果您使用它会得到异常.
Using cv2.VideoCapture( invalid device number )
does not throw exceptions. It constructs a <VideoCapture object>
containing an invalid device - if you use it you get exceptions.
测试None
和not isOpened()
的构造对象以清除无效的对象.
Test the constructed object for None
and not isOpened()
to weed out invalid ones.
对我来说这有效(1个便携式摄像头设备):
For me this works (1 laptop camera device):
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行
Example from: https://github.com/opencv/opencv_contrib/blob/master/samples/python2/video.py lines 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
这篇关于如何正确检查相机是否可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!