OpenCV Videowriter to v4l2sink

I’m using opencv to create frames which I want to send to a v4l2sink via VideoWriter. This is what I have so far:

 gst_out =  "appsrc ! queue ! videoconvert ! v4l2sink device=/dev/video3"
    vw = cv2.VideoWriter(gst_out, cv2.CAP_GSTREAMER, 0, 60, (2560, 720))

while True:
        # Create sample opencv frame
        frame = np.zeros((2560,720,3), np.uint8)
        cv2.putText(frame, "room a", (500, 500), font, 1.0, (255, 255, 255), 1)

        vw.write(frame)

But when I try to view these frames via the following pipeline:

gst-launch-1.0 v4l2src device=/dev/video3 ! xvimagesink

I get ERROR: from element /GstPipeline:pipeline0/GstV4l2Src:v4l2src0: Cannot identify device '/dev/video3'.
Additionally, it doesn’t list this new video device in v4l2-ctl --list-devices

In the python script I get the following error:
handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module v4l2sink0 reported: Cannot identify device '/dev/video3'.

I believe that maybe I didn’t install v4l2loopback correctly? I just ran sudo apt-get install v4l2loopback-dkms and it installed with no errors

I had to run sudo modprobe v4l2loopback video_nr=3 and now I can see the device under list-devices. However, now I get the following error:

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)

Hi,
please refer to this topic and give it a try:
Gstreamer v4l2loopback hw mjpeg stream - #5 by Honey_Patouceul

That example works however when I try to simplify the pipeline like so I only see the image for a second then it closes and gives an error:

gst-launch-1.0 v4l2src device=/dev/video2 ! video/x-raw,format=YUY2,width=2560,height=720,framerate=60/1 ! v4l2sink device=/dev/video3 -v
gst-launch-1.0 -e v4l2src device=/dev/video3 ! xvimagesink
Setting pipeline to PAUSED ...
Pipeline is live and does not need PREROLL ...
Setting pipeline to PLAYING ...
New clock: GstSystemClock
ERROR: from element /GstPipeline:pipeline0/GstV4l2Src:v4l2src0: Failed to allocate a buffer
Additional debug info:
gstv4l2src.c(998): gst_v4l2src_create (): /GstPipeline:pipeline0/GstV4l2Src:v4l2src0
EOS on shutdown enabled -- waiting for EOS after Error
Waiting for EOS...
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 error (-5)
Got EOS from element "pipeline0".
EOS received - stopping pipeline...
Execution ended after 0:00:00.097575230
Setting pipeline to PAUSED ...
Setting pipeline to READY ...
Setting pipeline to NULL ...
Freeing pipeline ...

I also tried this as a videowriter in opencv:

gst_out = "appsrc ! queue ! videoconvert ! videoscale method=0 ! v4l2sink device=/dev/video3 -v"
vw = cv2.VideoWriter(gst_out, 0, 60, (2560, 720))

which writes the frame with no errors, but when I try to view it I get this error:

gst-launch-1.0 -e v4l2src device=/dev/video3 ! xvimagesink
Setting pipeline to PAUSED ...
Pipeline is live and does not need PREROLL ...
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 ...

This stack post helped me: Gstreamer v4l2src failed to allocate buffer when run on a Jetson Nano - Stack Overflow

I set max-buffers=2 in sudo modprobe v4l2loopback video_nr=3 max_buffers=2

and now I can run

gst-launch-1.0 v4l2src device=/dev/video2 ! video/x-raw,format=YUY2,width=2560,height=720,framerate=60/1 ! v4l2sink device=/dev/video3 -v
 gst-launch-1.0 -v v4l2src device=/dev/video3 ! xvimagesink

I still don’t know how to do this in opencv however

Hi,
You may go to OpenCV forum to get further suggestion. Since we don’t have experience of linking to v4l2sink in cv2.VideoWriter(), not able to suggest next. User in OpenCV forum can have more experience and can share suggestion/guidance.

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