【问题标题】:gstreamer critical error when trying to capture video using webcam python opencv尝试使用网络摄像头 python opencv 捕获视频时出现 gstreamer 严重错误
【发布时间】:2023-03-25 17:32:01
【问题描述】:

我正在尝试使用带有简单代码的 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()

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

GStreamer-CRITICAL **:gst_element_get_state:断言“GST_IS_ELEMENT(元素)”失败 cap.isOpened

有关更多信息,我正在使用带有 ubuntu 16.04 的 odroid xu4,网络摄像头我使用罗技 c170(它在 webcamtest 和使用 guvcview 中正常工作)认为它不适用于奶酪和 camorama。

需要这方面的帮助..

【问题讨论】:

  • 在终端输入:python -c "import cv2; print(cv2.getBuildInformation())" | grep -iP "(v4l|ffmpeg)"。输出是什么?
  • @zindarod 这是我在终端上输入您所说的内容时遇到的错误Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'> BrokenPipeError: [Errno 32] Broken pipe
  • 我也尝试在 ffmpeg 关键字中查看 cv2.getBuildInformation(),这是值:` Video I/O: DC1394: YES (ver 2.2.4) FFMPEG: YES avcodec: YES (ver 57.89.100) avformat: YES (ver 57.71.100) avutil: YES (ver 55.58.100) swscale: YES (ver 4.6.100) avresample: YES (ver 3.5.0) `
  • 安装gst-libav 能解决这个问题吗?请参阅this OpenCV 问答帖子。另外,OpenCV 版本是多少?

标签: python opencv ubuntu gstreamer odroid


【解决方案1】:

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

cap = cv2.VideoCapture(0, cv2.CAP_V4L)

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

解决方法将后端切换到 V4L(从默认 GStreamer)用于我的 OpenCV 3.4.4 构建,在 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

如果解决方法对您不起作用,您可以使用 print(cv2.getBuildInformation()) 检查您的 OpenCV 构建是否支持 V4L。这里是我构建的相关部分:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-01
    • 1970-01-01
    • 2012-07-21
    • 2017-04-27
    • 2015-08-18
    • 1970-01-01
    • 1970-01-01
    • 2014-06-05
    相关资源
    最近更新 更多