V4l2 error when cv::VideoCapture release() method is called

Since updating my system “recently”, I have been seeing new error messages. I am doing something like:

cv::VideoCapture inputVideo;
inputVideo.open(0);
inputVideo.grab();
inputVideo.retrieve(image);
cv::Mat image;
inputVideo.release();

I get this error when the last line is called:

0:00:52.295323652 12723   0x55bd589070 ERROR          v4l2allocator gstv4l2allocator.c:1247:gst_v4l2_allocator_qbuf:<v4l2src0:pool:src:allocator> failed queueing buffer X: Bad file descriptor
0:00:52.295424227 12723   0x55bd589070 ERROR         v4l2bufferpool gstv4l2bufferpool.c:1112:gst_v4l2_buffer_pool_qbuf:<v4l2src0:pool:src> could not queue a buffer X

where X is usually 0-3. I only get this error if I use “inputVideo.grab();” + “inputVideo.retrieve(image);”. If I run all the other commands without actually grabbing video, I do not see this error.

The error is distracting and ultimately has no effect on me grabbing video but I would like to get rid of the error message as it is distracting. Thanks.

Hi,
You may try to run gstreamer command in cv2.VideoCapture(). To see if it works in this way of launching the v4l2 sources. Please refer to
Issue with multi-camera gstreamer capture using OpenCV - #6 by DaneLLL
V4l2src using OpenCV Gstreamer is not working in Jetson Xavier NX - #3 by DaneLLL

1 Like

Are you saying that v4l2 does not work properly with OpenCV because that’s what it sounds like?

I tried this:

cv::VideoCapture inputVideo;
inputVideo.open("v4l2src device=/dev/video1 ! video/x-raw, width=1280, height=720 ! videoconvert ! video/x-raw,format=BGR ! appsink")

But got this error message:

OpenCV(4.5.5) /home/nano/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 device=/dev/video1 ! video/x-raw, width=1280, height=720 ! videoconvert ! video/x-raw,format=BGR ! appsink in function 'icvExtrac
tPattern'

You may specify gstreamer backend (seems it used IMAGES backend):

cv::VideoCapture inputVideo("v4l2src device=/dev/video1 ! video/x-raw, width=1280, height=720 ! videoconvert ! video/x-raw,format=BGR ! appsink drop=1", cv::CAP_GSTREAMER);
1 Like

I tried this with GST_DEBUG=2 but the open method fails:


[ WARN:0@3.941] global /home/nano/opencv/modules/videoio/src/cap_gstreamer.cpp (2402) handleMessage OpenCV | GStreamer warning: Embedded video playb
ack halted; module v4l2src0 reported: Internal data stream error.
[ WARN:0@3.941] global /home/nano/opencv/modules/videoio/src/cap_gstreamer.cpp (1356) open OpenCV | GStreamer warning: unable to start pipeline
[ WARN:0@3.941] global /home/nano/opencv/modules/videoio/src/cap_gstreamer.cpp (862) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created

You may post the exact code you tried for better advice.
Also, please post the output of:

# v4l2-ctl command is provided by apt package v4l-utils
v4l2-ctl -d1 --list-formats-ext

gst-launch-1.0 -v v4l2src device=/dev/video1 ! video/x-raw, width=1280, height=720 ! fakesink
1 Like

This is my code:

cv::VideoCapture inputVideo;
bool ret = inputVideo.open("v4l2src device=/dev/video1 ! video/x-raw, width=1280, height=720 ! videoconvert ! video/x-raw,format=BGR ! appsink drop=1", cv::CAP_GSTREAMER)
// ret == false :-(
nano@jetson-nano:/$ v4l2-ctl -d1 --list-formats-ext
ioctl: VIDIOC_ENUM_FMT
        Index       : 0
        Type        : Video Capture
        Pixel Format: 'YUYV'
        Name        : YUYV 4:2:2
                Size: Discrete 320x240
                        Interval: Discrete 0.005s (187.000 fps)
                        Interval: Discrete 0.007s (150.000 fps)
                        Interval: Discrete 0.007s (137.000 fps)
                        Interval: Discrete 0.008s (125.000 fps)
                        Interval: Discrete 0.010s (100.000 fps)
                        Interval: Discrete 0.013s (75.000 fps)
                        Interval: Discrete 0.017s (60.000 fps)
                        Interval: Discrete 0.020s (50.000 fps)
                        Interval: Discrete 0.027s (37.000 fps)
                        Interval: Discrete 0.033s (30.000 fps)
                Size: Discrete 640x480
                        Interval: Discrete 0.017s (60.000 fps)
                        Interval: Discrete 0.020s (50.000 fps)
                        Interval: Discrete 0.025s (40.000 fps)
                        Interval: Discrete 0.033s (30.000 fps)
                        Interval: Discrete 0.067s (15.000 fps)
nano@jetson-nano:/$ gst-launch-1.0 -v v4l2src device=/dev/video1 ! video/x-raw, width=1280, height=720 ! fakesink
Setting pipeline to PAUSED ...
Pipeline is live and does not need PREROLL ...
0:00:00.201781496 12474   0x559be02990 WARN                 basesrc gstbasesrc.c:3055:gst_base_src_loop:<v4l2src0> error: Internal data stream error.
0:00:00.201829362 12474   0x559be02990 WARN                 basesrc gstbasesrc.c:3055:gst_base_src_loop:<v4l2src0> error: streaming stopped, reason not-negotiated (-4)
ERROR: from element /GstPipeline:pipeline0/GstV4l2Src:v4l2src0: Internal data stream error.
Additional debug info:
gstbasesrc.c(3055): gst_base_src_loop (): /GstPipeline:pipeline0/GstV4l2Src:v4l2src0:
streaming stopped, reason not-negotiated (-4)
ERROR: pipeline doesn't want to preroll.
Setting pipeline to PAUSED ...
Setting pipeline to READY ...
Setting pipeline to NULL ...
Freeing pipeline ...

Does it work with one of the listed modes ?

gst-launch-1.0 -v v4l2src device=/dev/video1 ! video/x-raw, format=YUY2, width=640, height=480, framerate=30/1 ! fakesink

# Or
gst-launch-1.0 -v v4l2src device=/dev/video1 io-mode=2 ! video/x-raw, format=YUY2, width=640, height=480, framerate=30/1 ! fakesink

1 Like

Apologies to both of you - I was trying to use an incompatible resolution. SMH!

When I run both of the shell commands that you suggested it seems to not give any errors.

If I use this line in my code:

inputVideo.open("v4l2src device=/dev/video1 ! video/x-raw, width=640, height=480 ! videoconvert ! video/x-raw,format=BGR ! appsink")

The open() succeeds however I am still seeing the same error messages when release() is called:

0:00:46.705588468 12962   0x5590698240 ERROR          v4l2allocator gstv4l2allocator.c:1247:gst_v4l2_allocator_qbuf:<v4l2src0:pool:src:allocator> failed queueing buffer 0: Bad file descriptor
0:00:46.705664720 12962   0x5590698240 ERROR         v4l2bufferpool gstv4l2bufferpool.c:1112:gst_v4l2_buffer_pool_qbuf:<v4l2src0:pool:src> could not queue a buffer 0

Try adding property drop=1 to appsink as in my first post.

1 Like

I tried;

inputVideo.open("v4l2src device=/dev/video1 ! video/x-raw, width=640, height=480 ! videoconvert ! video/x-raw,format=BGR ! appsink drop=1", cv::CAP_GSTREAMER)

And the error messages still appear.

Hi,
If you don’t hit any real issue such as failing to launch the camera in next run, it should be harmless and can be ignored. You can download source code of gst-v4l2, remove the prints, and rebuild/replace libgstnvvideo4linux2.so

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