Cannot open opencv VideoCapture with gstreamer pipeline

This is the output of v4l2-ctl:
ioctl: VIDIOC_ENUM_FMT
Index : 0
Type : Video Capture
Pixel Format: ‘MJPG’ (compressed)
Name : Motion-JPEG

Index : 1
Type : Video Capture
Pixel Format: ‘YUYV’
Name : YUYV 4:2:2

I want to capture images from my usb camera with this python code:

import cv2
cap=cv2.VideoCapture(PIPELINE_STR, cv2.CAP_GSTREAMER)
ret, img = cap.read()

I tried many pipelines but none of them can successfully open the VideoCapture object. The following one seems to work with gst-launch-1.0 but not in python (changing xvimagesync to appsink):
gst-launch-1.0 v4l2src device=\"/dev/video0\" ! xvimagesink

1 Like
  1. For reading RAW YUYV422 format you would try :
gst-launch-1.0 -v v4l2src device=/dev/video0 ! video/x-raw, format=YUY2 ! xvimagesink

If working, for capturing from opencv you would use:

cap = cv2.VideoCapture("v4l2src device=/dev/video0 ! video/x-raw, format=YUY2 ! videoconvert ! video/x-raw, format=BGR ! appsink drop=1", cv2.CAP_GSTREAMER)

# Or this one using HW for YUV -> BGRx conversion, so that videoconvert on CPU only removes the 4th byte.
cap = cv2.VideoCapture("v4l2src device=/dev/video0 ! video/x-raw, format=YUY2 ! nvvidconv ! video/x-raw(memory:NVMM) ! nvvidconv ! video/x-raw, format=BGRx ! videoconvert ! video/x-raw, format=BGR ! appsink drop=1", cv2.CAP_GSTREAMER)
  1. For reading MJPG format, you may try:
gst-launch-1.0 -v v4l2src device=/dev/video0 ! image/jpeg, format=MJPG ! jpegdec ! ximagesink

# Or this one using HW decoder:
gst-launch-1.0 -v v4l2src device=/dev/video0 ! image/jpeg, format=MJPG ! nvv4l2decoder mjpeg=1 ! nvvidconv ! xvimagesink

that would turn for opencv capture into:

cap = cv2.VideoCapture(" v4l2src device=/dev/video0 ! image/jpeg, format=MJPG ! jpegdec ! video/x-raw,format=BGR ! appsink drop=1", cv2.CAP_GSTREAMER)

cap = cv2.VideoCapture(" v4l2src device=/dev/video0 ! image/jpeg, format=MJPG ! nvv4l2decoder mjpeg=1 ! nvvidconv ! video/x-raw,format=BGRx ! videoconvert ! video/x-raw,format=BGR ! appsink drop=1", cv2.CAP_GSTREAMER)
1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.