尝试使用webcam python opencv捕获视频时gstreamer严重错误

Shi*_*Rei 10 python ubuntu opencv gstreamer odroid

我正在尝试使用opencv和python以及简单的代码来使用网络摄像头拍摄视频

import numpy as np
import cv2

cap = cv2.VideoCapture(0)
print('cap.isOpened')
if cap.isOpened():
    print ('cap is opened')
    while(True):
        re,img=cap.read()
        cv2.imshow("video output", img)
        k = cv2.waitKey(10)&0xFF
        if k==27:
            break
cap.release()
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)

如果我尝试播放现有的视频,如.mp4文件,它工作正常.但是当我尝试使用网络摄像头时出现错误

GStreamer-CRITICAL**:gst_element_get_state:断言'GST_IS_ELEMENT(element)'cap.isOpened失败

有关更多信息,我正在使用odroid xu4和ubuntu 16.04,网络摄像头我使用logitech c170(它在网络摄像头测试和使用guvcview正常工作)认为它不适用于奶酪和camorama.

需要帮助,请...

Ulr*_*ern 1

以下解决方法有合理的工作机会:

cap = cv2.VideoCapture(0, cv2.CAP_V4L)
Run Code Online (Sandbox Code Playgroud)

OpenCV 3 中添加了选择后端的功能,请参阅VideoCapture() 文档

对于我的 OpenCV 3.4.4 版本,该解决方法将后端切换为V4L(从默认的 GStreamer),并在 16.04 机器上支持 GStreamer。这里是问题代码的输出以及之后的解决方法export OPENCV_VIDEOIO_DEBUG=TRUE

[ WARN:0] VIDEOIO(cvCreateCameraCapture_V4L(index)): trying ...

[ WARN:0] VIDEOIO(cvCreateCameraCapture_V4L(index)): result=0x20b1470 ...

cap.isOpened
cap is opened
Run Code Online (Sandbox Code Playgroud)

如果该解决方法不适合您,您可以检查您的 OpenCV 版本是否支持V4L使用print(cv2.getBuildInformation()). 这是我的构建的相关部分:

Video I/O:
  DC1394:                      YES (ver 2.2.4)
  FFMPEG:                      YES
    avcodec:                   YES (ver 56.60.100)
    avformat:                  YES (ver 56.40.101)
    avutil:                    YES (ver 54.31.100)
    swscale:                   YES (ver 3.1.101)
    avresample:                NO
  GStreamer:                  
    base:                      YES (ver 1.8.3)
    video:                     YES (ver 1.8.3)
    app:                       YES (ver 1.8.3)
    riff:                      YES (ver 1.8.3)
    pbutils:                   YES (ver 1.8.3)
  libv4l/libv4l2:              NO
  v4l/v4l2:                    linux/videodev2.h
Run Code Online (Sandbox Code Playgroud)