VideoWriter with gstreamer

Good day.
For my project I am using jetson xavier.
My goal it to create python script that read frame from the connected camera (Boson) and push image to the gstreamer pipeline through the network

When I tried to use opencv I received following error:

import cv2
“Illegal Instruction (Core Dump)”

I downgraded opencv and numpy according to this guide

https://forums.developer.nvidia.com/t/opencv-import-fails-with-illegal-instruction-core-dump/168060

opencv-python==4.5.3.56
numpy==1.19.4

And cv2 and flirpy python libraries started work

Then I tried to create VideoWriter with gstreamer pipeline

import cv2

gst_str_rtp = “appsrc ! decodebin ! x264enc ! rtph264pay ! udpsink host=10.0.0.19 port=5000”
out = cv2.VideoWriter(gst_str_rtp, 0, 10.0, (640, 480), True)
if not out.isOpened():
* print(“Failed to open output”)*

  • exit()*

After I am starting this script - I received Failed to open output

Maybe this is because of the dimensions or else, so I tried to simplify the code

import cv2
im = cv2.read(“/home/rakia/Desktop/vimtestpy/1.jpg”)

gst_out = "appsrc ! video/x-raw, format=BGR ! queue ! videoconvert ! video/x-raw,format=BGRx ! nvvidconv ! nvv4l2h264enc ! h264parse ! matroskamux ! filesink location=/home/rakia/Desktop/vimtestpy/test.mkv "
out = cv2.VideoWriter(gst_out, cv2.CAP_GSTREAMER, 0, float(10), (int(332), int(332)))
if not out.isOpened():
print(“Failed to open output”)

  • exit()*

while True:

  • out.write(im);*

print(“successfully exit”)

out.release()

if name == ‘main’:

  • read_cam()*

The output is the same
Failed to open output

And the most important
I tested first version of code few month ago and it worked. What changed? What am I doing wrong?

Thank you

Hi,
Please execute $ v4l2-ctl --list-formats-ext and check supported formats. If the camera supports general YUV422 formats such as UYVY or YUYV, please try this sample:
V4l2src using OpenCV Gstreamer is not working in Jetson Xavier NX - #3 by DaneLLL

Here is the result

rakia@rakia-dev1:~/Desktop/vimtestpy$ python3 Server.py
[ERROR:0] global /tmp/pip-req-build-rd4x2fty/opencv/modules/videoio/src/cap.cpp (160) open VIDEOIO(CV_IMAGES): raised OpenCV exception:

OpenCV(4.5.3) /tmp/pip-req-build-rd4x2fty/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/video2 ! video/x-raw, width=640, height=480 ! videoconvert ! video/x-raw,format=BGR ! appsink in function ‘icvExtractPattern’

*$ v4l2-ctl --list-formats-ext

ioctl: VIDIOC_ENUM_FMT
Index : 0
Type : Video Capture
Pixel Format: ‘YU12’
Name : Planar YUV 4:2:0
Size: Discrete 640x512
Interval: Discrete 0.017s (60.000 fps)
Interval: Discrete 0.033s (30.000 fps)

Index       : 1
Type        : Video Capture
Pixel Format: 'Y16 '
Name        : 16-bit Greyscale
	Size: Discrete 640x512
		Interval: Discrete 0.017s (60.000 fps)
		Interval: Discrete 0.033s (30.000 fps)

Index       : 2
Type        : Video Capture
Pixel Format: 'NV12'
Name        : Y/CbCr 4:2:0
	Size: Discrete 640x512
		Interval: Discrete 0.017s (60.000 fps)
		Interval: Discrete 0.033s (30.000 fps)

Index       : 3
Type        : Video Capture
Pixel Format: ''
Name        : 3132564e-0000-0010-8000-00aa003
	Size: Discrete 640x512
		Interval: Discrete 0.017s (60.000 fps)
		Interval: Discrete 0.033s (30.000 fps)

Hi,
Please try the gst-launch-1.0 command and check if you can see video preview:

$ export DISPLAY=:0
$ gst-launch-1.0 v4l2src device=/dev/video0 ! video/x-raw,format=NV12,width=640,height=512,framerate=30/1 ! videoconvert ! xvimagesink sync=0

If above command works, please apply it to cv2.VideoCapture():

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

But my problem is to put image to pipeline. How this can help me?

Hi,
From the error it looks like you cannot read frames from the source, so we suggest check camera source first. So you are able to read frames from the source and see camera preview now?

No, it is not a problem. I can read the frame with simple opencv. My main problem - send this frame through tcp with gstreamer. Or UDP. TCP is better

Hi,
There are topics about UDP streaming, Please refer to cv2.VideoWriter() in the posts:
Stream processed video with OpenCV on Jetson TX2 - #5 by DaneLLL
OpenvCV, Gstreamer, Python camera capture/access and streaming to RTP

And give it a try.

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