cv2.VideoCapture [gstreamer] issues

Hi there,

I’m attempting to add a gstreamer capture and output to my python script utilising open CV but I’m struggling to find success.

cap = cv2.VideoCapture('v4l2src device=“/dev/video0” ! “video/x-raw, width=640, height=480, format=(string)YUY2” ! videoconvert ! video/x-raw, format=(string)BGR ! appsink ', cv2.CAP_GSTREAMER)

w = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
h = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
fps = cap.get(cv2.CAP_PROP_FPS)
print(‘Src opened, %dx%d @ %d fps’ % (w, h, fps))

gst_out = "appsrc ! video/x-raw, format=BGR ! queue ! videoconvert ! video/x-raw,format=BGRx ! nvvidconv ! nvv4l2h264enc ! h264parse ! matroskamux ! filesink location=test.mkv "
out = cv2.VideoWriter(gst_out, cv2.CAP_GSTREAMER, 0, float(30), (int(640), int(480)))
if not out.isOpened():
print(“Failed to open Output”)
exit()

This will output

Src opened, 0x0 @ 0 fps
Failed to open Output

The USB camera is setup and working. If I run the following in terminal the camera displays as expected:

gst-launch-1.0 v4l2src device=“/dev/video0” !
“video/x-raw, width=640, height=480, format=(string)YUY2” !
xvimagesink -e

OpenCV 4.5 has been install via the AastaNV installation script with both Gstreamer and CUDA support.

Any assistance would be much appreciated.

Hi,
We can run this sample with E-Con CU135:

nvidia@nvidia-desktop:~$ cat cv_trancode.py
import sys
import cv2

def read_cam():
    cap = cv2.VideoCapture("v4l2src num-buffers=300 ! video/x-raw,format=UYVY,width=1280,height=720,framerate=30/1 ! videoconvert ! video/x-raw,format=BGR ! appsink  ")

    w = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
    h = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
    fps = cap.get(cv2.CAP_PROP_FPS)
    print('Src opened, %dx%d @ %d fps' % (w, h, fps))

    gst_out = "appsrc ! video/x-raw, format=BGR ! queue ! videoconvert ! video/x-raw,format=BGRx ! nvvidconv ! nvv4l2h264enc ! h264parse ! matroskamux ! filesink location=test.mkv "
    out = cv2.VideoWriter(gst_out, cv2.CAP_GSTREAMER, 0, float(fps), (int(w), int(h)))
    if not out.isOpened():
        print("Failed to open output")
        exit()

    if cap.isOpened():
        while True:
            ret_val, img = cap.read();
            if not ret_val:
                break;
            out.write(img);
            cv2.waitKey(1)
    else:
     print "pipeline open failed"

    print("successfully exit")
    cap.release()
    out.release()


if __name__ == '__main__':
    read_cam()

Don’t see framerate being set in your pipeline. You may set it for a try. In our test, we run in 1280x720p30:

video/x-raw,format=UYVY,width=1280,height=720,framerate=30/1

Thanks for your reply.

I tried the above and still encountering a similar issue.

cap = cv2.VideoCapture("v4l2src num-buffers=300 ! video/x-raw,format=UYVY,width=640,height=480,framerate=30/1 ! videoconvert ! video/x-raw,format=BGR ! appsink ")

w = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
h = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
fps = cap.get(cv2.CAP_PROP_FPS)

print(‘Src opened, %dx%d @ %d fps’ % (w, h, fps))

gst_out = "appsrc ! video/x-raw, format=BGR ! queue ! videoconvert ! video/x-raw,format=BGRx ! nvvidconv ! nvv4l2h264enc ! h264parse ! matroskamux ! filesink location=test.mkv "
out = cv2.VideoWriter(gst_out, cv2.CAP_GSTREAMER, 0, float(fps), (int(w), int(h)))
if not out.isOpened():
print(“Failed to open Output”)
exit()

Returns:

[ERROR:0] global /tmp/pip-req-build-zuuo394f/opencv/modules/videoio/src/cap.cpp (140) open VIDEOIO(CV_IMAGES): raised OpenCV exception:

OpenCV(4.5.1) /tmp/pip-req-build-zuuo394f/opencv/modules/videoio/src/cap_images.cpp:253: error: (-5:Bad argument) CAP_IMAGES: can’t find starting number (in the name of file): v4l2src num-buffers=300 ! video/x-raw,format=UYVY,width=640,height=480,framerate=30/1 ! videoconvert ! video/x-raw,format=BGR ! appsink in function ‘icvExtractPattern’

Src opened, 0x0 @ 0 fps
Failed to open Output

The only thing was altered was the width/height to match the parameters from the ’ v4l2-ctl -d0 --list-formats-ext ’ command.

$ v4l2-ctl -d0 --list-formats-ext
ioctl: VIDIOC_ENUM_FMT
Index : 0
Type : Video Capture
Pixel Format: ‘YUYV’
Name : YUYV 4:2:2
Size: Discrete 640x480
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.040s (25.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.067s (15.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Interval: Discrete 0.200s (5.000 fps)

I also tried adding the v2.CAP_GSTREAMER to the end of the capture parameter, stops the CAP_IMAGES error but it is still unable to open the stream.

Also tried as well as changing it to specifically target /dev/video0

cap = cv2.VideoCapture("v4l2src device=/dev/video0 ! video/x-raw,format=YUYV,width=640,height=480,framerate=30/1 ! videoconvert ! video/x-raw,format=BGR ! appsink ")

But I’m still getting the same errors.

Hi,
Yo may try

video/x-raw,format=YUY2,width=640,height=480,framerate=30/1

YUYV is named YUY2 in gstreamer. The source does not support UYVY.

Thanks again for your quick response.

Unfortunately still not working for me:

cap = cv2.VideoCapture("v4l2src device=/dev/video0 ! video/x-raw,format=YUY2,width=640,height=480,framerate=30/1 ! videoconvert ! video/x-raw,format=BGR ! appsink ")

Errors with:

[ERROR:0] global /tmp/pip-req-build-zuuo394f/opencv/modules/videoio/src/cap.cpp (140) open VIDEOIO(CV_IMAGES): raised OpenCV exception:

OpenCV(4.5.1) /tmp/pip-req-build-zuuo394f/opencv/modules/videoio/src/cap_images.cpp:253: error: (-5:Bad argument) CAP_IMAGES: can’t find starting number (in the name of file): v4l2src num-buffers=300 ! video/x-raw,format=YUY2,width=640,height=480,framerate=30/1 ! videoconvert ! video/x-raw,format=BGR ! appsink in function ‘icvExtractPattern’

Nor the addition of cv2.CAP_GSTREAMER at the end of the capture work (same error as before)

Hi,
Do you have other USB cameras for a try? Or try videotestsrc:

cap = cv2.VideoCapture("videotestsrc is-live=1 ! video/x-raw,format=YUY2,width=640,height=480,framerate=30/1 ! videoconvert ! video/x-raw,format=BGR ! appsink ")

Hi there,

I don’t have another camera currently on me. However, using that test string produces the same issue.

[ERROR:0] global /tmp/pip-req-build-zuuo394f/opencv/modules/videoio/src/cap.cpp (140) open VIDEOIO(CV_IMAGES): raised OpenCV exception:

OpenCV(4.5.1) /tmp/pip-req-build-zuuo394f/opencv/modules/videoio/src/cap_images.cpp:253: error: (-5:Bad argument) CAP_IMAGES: can’t find starting number (in the name of file): videotestsrc is-live=1 ! video/x-raw,format=YUY2,width=640,height=480,framerate=30/1 ! videoconvert ! video/x-raw,format=BGR ! appsink in function ‘icvExtractPattern’

Src opened, 0x0 @ 0 fps
Failed to open Output

Hi,
We suggest re-install OpenCV and try again. A user also sees misbehavior and it works after re-installation:
Jetson Nano doesn't find the latest version of OpenCV - #7 by DaneLLL