gstreamer critical error when trying to capture video using webcam python opencv
Asked Answered
W

1

11

i'm trying to take a video with webcam using opencv and python with a simple code

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()

it's working fine if i try to play an existing video such as .mp4 file. but when i try using a webcam i got an error

GStreamer-CRITICAL **: gst_element_get_state: assertion 'GST_IS_ELEMENT (element)' failed cap.isOpened

for more information i'm using odroid xu4 with ubuntu 16.04, webcam i use logitech c170 ( it work properly in webcamtest and using guvcview) thought it doesn't workon cheese and camorama.

need help about this please..

Willman answered 12/7, 2018 at 10:30 Comment(4)
In terminal enter: python -c "import cv2; print(cv2.getBuildInformation())" | grep -iP "(v4l|ffmpeg)". What is the output?Scopophilia
@Scopophilia this is the error i got when i type what you say on my terminal Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'> BrokenPipeError: [Errno 32] Broken pipeWillman
also i try to see the cv2.getBuildInformation() in the ffmpeg keyword and this is the value : ` 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) `Willman
Does installing gst-libav solve this? See this OpenCV Q&A post. Also, what's the OpenCV version?Boundless
B
1

The following workaround has a reasonable chance of working:

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

The ability to select backends was added in OpenCV 3, see the VideoCapture() docs.

The workaround switches the backend to V4L (from default GStreamer) for my OpenCV 3.4.4 build with GStreamer support on a 16.04 box. Here the output of the question's code with workaround after 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

If the workaround does not work for you, you can check whether your OpenCV build supports V4L using print(cv2.getBuildInformation()). Here the relevant section for my build:

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
Boundless answered 29/11, 2018 at 16:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.