Problems with reading frames using v4l2 in python

While I was working, found a problem that I’m not able to run this code in python:

import sys
import cv2

def read_cam():
cap = cv2.VideoCapture(“v4l2src device=/dev/video0 ! jpegdec ! video/x-raw, width=640, height=480 ! xvimagesink”, cv2.CAP_GSTREAMER)
if True:
cv2.namedWindow(“demo”, cv2.WINDOW_AUTOSIZE)
while True:
ret_val, img = cap.read();
cv2.imshow(‘demo’,img)
cv2.waitKey(10)
else:
print(“camera open failed”)

cv2.destroyAllWindows()

if name == ‘main’:
read_cam()

However if I’m trying to run « v4l2src device=/dev/video0 ! jpegdec ! video/x-raw, width=640, height=480 ! xvimagesink» in terminal, it works, so what is the problem?

Hi,
You would need to link to appsink when running gstreamer command in cv2.VideoCapture(). Please try

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

Thanks for reply, but it didn’t help me, now it throws me a mistake:

cv2.error: OpenCV(4.6.0) /io/opencv/modules/highgui/src/window.cpp:967: error: (-215:Assertion failed) size.width>0 && size.height>0 in function ‘imshow’

Also this is info about my webcam:

ioctl: VIDIOC_ENUM_FMT
Type: Video Capture

Size: Discrete 1280x720
  Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 960x540
  Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 848x480
  Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 640x480
  Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 640x360
  Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 1280x720
  Interval: Discrete 0.100s (10.000 fps)
Size: Discrete 960x540
  Interval: Discrete 0.067s (15.000 fps)
Size: Discrete 848x480
  Interval: Discrete 0.067s (15.000 fps)
Size: Discrete 640x480
  Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 640x360
  Interval: Discrete 0.033s (30.000 fps)

Thanks for reply, but it didn’t help me, now it throws me a mistake:
cv2.error: OpenCV(4.6.0post deleted by author) /io/opencv/modules/highgui/src/window.cpp:967: error: (-215:Assertion failed) size.width>0 && size.height>0 in function ‘imshow’

Also this is what I’m using:

ioctl: VIDIOC_ENUM_FMT
Type: Video Capture

Size: Discrete 1280x720
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 960x540
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 848x480
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 640x480
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 640x360
Interval: Discrete 0.033s (30.000 fps)
[1]: ‘YUYV’ (YUYV 4:2:2)
Size: Discrete 1280x720
Interval: Discrete 0.100s (10.000 fps)
Size: Discrete 960x540
Interval: Discrete 0.067s (15.000 fps)
Size: Discrete 848x480
Interval: Discrete 0.067s (15.000 fps)
Size: Discrete 640x480
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 640x360
Interval: Discrete 0.033s (30.000 fps)

Hi,
You may also try this command:

cap = cv2.VideoCapture("videotestsrc ! video/x-raw, width=640, height=480 ! videoconvert ! format=BGR ! appsink", cv2.CAP_GSTREAMER)

Or try the sample:
V4l2src using OpenCV Gstreamer is not working in Jetson Xavier NX - #3 by DaneLLL

Hey!
It still doesn’t want to work
I crashed my monitor, that’s why while I’m waiting for new one, I’m practicing on Ubuntu on my laptop. My laptop’s web cam supports mjpeg and yuyv formats I still can’t understand why.
When I’m putting these commands in terminal, everything is okay and it’s working, also usual cv2.VideoCapture(‘/dev/video0’) works, sudo gst-launch-1.0 v4l2src device=/dev/video0 ! jpegdec ! ‘video/x-raw, width=640, height=480’ ! xvimagesink works too, but when I try it in python, it seems that it just doesn’t receives a frame (according to a mistake)

So what is wrong?

Hi,
It seems like gstreamer is not enabled in OpenCV. If you are not using Jetson Nano now, may need to manually enable gstreamer and rebuild OpenCV on the host PC.

Thanks for your reply! But I haven’t understood, what you mean under “manually enable gstreamer”

Gstreamer support as backend for VideoCapture and VideoWriter in opencv videoio is an option that can be activated or not at opencv build time (well just before in CMake config).
You may check if the opencv lib used by your current python setup does support gstreamer with:

import cv2
print(cv2.getBuildInformation())

in displayed log, look for GSTREAMER support, if it shows NO you would have to install another opencv build supporting gstreamer for your current python install.

If GSTREAMER support is ok, you may try:

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

# If ok try from camera:
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 drop=1", cv2.CAP_GSTREAMER)

# or trying to read from camera in JPEG format:
cap = cv2.VideoCapture("v4l2src device=/dev/video0 ! image/jpeg,format=MJPG,width=640,height=480,framerate=30/1 ! nvv4l2decoder mjpeg=1 ! nvvidconv ! video/x-raw,format=BGRx ! videoconvert ! video/x-raw,format=BGR ! appsink drop=1", cv2.CAP_GSTREAMER)

Last, you may also follow kernel messages with:

sudo dmesg --follow

and check if running your python code triggers some messages (from USB stack ?)

1 Like

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