AGX Orin will not open CV2.Videowriter to send out gstreamer stream

I have been trying to get my AGX Orin to capture a webcam stream and then turn it around and sent it out as a RTSP stream. But I keep getting and error that the cv2.VideoWriter is not open. No matter what kind of configuration I use the Videowriter will not open.
I have compiled Open CV with Gstreamer on.
I know that the cv2.Videowriter works because I can write to a file.

I have looked at this forum post Which seems to be a similar problem to mine:
https://forums.developer.nvidia.com/t/rtsp-stream-with-opencv-missing-installation/194869

And I looked here where it seems that the previous poster found the solution:
https://stackoverflow.com/questions/67376753/how-to-create-x264-rtsp-server-with-opencv-python-with-gstreamer-backend

But I cant find the solution;
Here is my code:

import cv2
import os
import numpy as np
cap = cv2.VideoCapture(2)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS))
gst_out = "appsrc ! videoconvert ! x264enc noise-reduction=10000 tune=zerolatency byte-stream=true threads=4 ! h264parse ! mpegtsmux ! rtpmp2tpay ! udpsink host=rtsp://127.0.0.1 port=8554/mystream"
    
out = cv2.VideoWriter(gst_out, cv2.CAP_GSTREAMER, 0, float(30), (int(width), int(height)))
if not out.isOpened() :
    print("Writer failed")
    exit()
print('Writer opened')
if not cap.isOpened():
    print('Cannot open RTSP stream')
    exit(-1)
    
count = 0

while True:
    _, frame = cap.read()
    out.write(frame)
out.release()

You may try:

gst_out = "appsrc ! video/x-raw,format=BGR ! queue ! videoconvert ! x264enc key-int-max=30 insert-vui=1 noise-reduction=10000 tune=zerolatency byte-stream=true threads=4 ! h264parse ! mpegtsmux ! rtpmp2tpay ! udpsink host=rtsp://127.0.0.1 port=8554"
out = cv2.VideoWriter(gst_out, cv2.CAP_GSTREAMER, 0, float(fps), (int(width), int(height)))

Thanks for the response.
I gave it a try and am still getting

"Writer failed"

I also believe that the issue has to do with the “gst_out” string.
I have seen other post in this forum where they seem to get the RTSP stream to be sent out with versions of the “gst_out” string

I have tried many iterations of the “gst_out” string to no avail.
I starting to think it may have to do with my OpenCV build.

Is there a recomended OpenCV build that is for the jetson products.?
Might and give that a try

You would check if the opencv version installed in your python env has GStreamer support:

import cv2
print(cv2.getBuildInformation())

It says its gstreamer is OFF
What’s odd is When I ran that command

import cv2
print(cv2.getBuildInformation())

after compiling OpenCV with Gstreamer=ON it said gstreamer was ON .
But now its off.
I am going to try removing OPencv and reinstall.
Got any suggestions for the best route to install OpenCV on AGX Orin?

There are many scripts that would be ok I think, such as

although you may have to remove v4l2ucp at line 40 of install_opencv4.6.0_Jetson.sh if running JP5.

Be aware that, AFAIK, the opencv install step would only install python2 OR python3. So be sure about the one you want to install opencv in, and configure your opencv build accordingly. (the link above configures for python3)

In python, pip may also install dependencies that might result in other opencv versions to be installed.

Thanks a bunch
Going to have to wait till I get back home to give it a go.

The Opencv build that was on my AGX Orin was a mess.
I had to reinstall jetpack5.0 to get the AGX Orin to reinstall OpenCV .

The “install_opencv4.5.0_Jetson.sh” script from you link worked nicely.

The cv2.Videowriter works with cv2.gstreamer now.
Big thanks for the help

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