
我试过这个代码(凸轮2不存在):
import cv2try: 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.soFailed to wrapper hybrID_drv_vIDeo.soFailed to open /usr/lib64/dri/hybrID_drv_vIDeo.soFailed to wrapper hybrID_drv_vIDeo.soGStreamer 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 832VIDEOIO(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_GStreamerOpenCV 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 139VIDEOIO(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::initDeviceCvCapture_OpenNI::CvCapture_OpenNI : Failed to enumerate production trees: Can't create any node of the requested type!<VIDeoCapture 0x7fa5b5de0450>
没有异常被抛出.在以后使用c.read()时,我确实得到False,但我想在程序的初始化阶段执行此 *** 作.
那么,我如何找出我有多少有效相机或检查某个数字是否“映射”到有效相机?
解决方法 使用cv2.VIDeoCapture(无效的设备号)不会抛出异常.它构造了一个< VIDeoCapture对象>包含无效设备 – 如果您使用它,您将获得例外.测试构造的对象为None而不是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 printouttestDevice(1) # prints message 输出1:
Warning: unable to open vIDeo source: 1
示例来自:https://github.com/opencv/opencv_contrib/blob/master/samples/python2/video.py
159ff线
总结06002
以上是内存溢出为你收集整理的python – 如何正确检查相机是否可用?全部内容,希望文章能够帮你解决python – 如何正确检查相机是否可用?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)