Ask Your Question
0

Help geeting OpenCV to output a videostream using gstreamer. [closed]

asked 2020-07-08 01:30:50 -0600

updated 2020-07-08 14:46:55 -0600

supra56 gravatar image

List, I am having a problem running gstreamer pipeline from Python 3 via OpenCv. I do not know gstreamer very well although I am somewhat comfortable with opencv. My issues stem from my ignorance not actual issue with Jetson TX2. I am working with Jetpack 4.4, running OpenCV 4.1.1 with gstreamer (unchanged build from Jetpack), and the Python version I am using is Python 3.6.9. On the hardware side I am using a devkit with Jetson TX2. The camera I am using is an ELP USB camera. The dev kit is headless. With this setup I am able to take pictures, record video, and stream video from the Jetson using gstreamer directly. My problem arises when I attempted to use it in python. Right now I am using a very simple python script to open my camera and stream video to another computer on the same network. My goal is to latter use a neural network I made to do detection, add bounding boxes, and pipe the post processed video via g streamer to another computer. I have tested a number of pipelines on command line and in general they work fine. When I add the pipeline to Python add attempt to use the same pipeline it errors out. For example I am using these pipelines for testing the USB web cam which is running off of a Jetson TX2.

gst-launch-1.0 v4l2src device=/dev/video0 ! ‘video/x-raw, format=YUY2’ ! nvvidconv ! ‘video/x-raw(memory:NVMM), width=640, height=480’ ! omxh264enc ! h264parse ! rtph264pay config-interval=1 ! udpsink host=192.168.86.26 port=5000

gst-launch-1.0 udpsrc port=5000 ! ‘application/x-rtp, encoding-name=H264, payload=96’ ! rtph264depay ! h264parse ! avdec_h264 ! xvimagesink

My goal is to get the output from my YOLO4 implementation to stream as HD264. Just trying some simple toy code I have been able to get video to write to file, albeit really bad. When I try streaming like the code below it seems to do nothing with the exception of this warning.

[ WARN:0] global /home/nvidia/host/build_opencv/nv_opencv/modules/videoio/src/cap_gstreamer.cpp (933) open OpenCV | GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1

Here is the toy code.

import time
import cv2

fps = 30
frame_width = 640
frame_height = 480
cap = cv2.VideoCapture(0)

cap.set(cv2.CAP_PROP_FRAME_WIDTH, frame_width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, frame_height)
cap.set(cv2.CAP_PROP_FPS, fps)

gst_str_rtp = "appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=500 speed-preset=superfast ! rtph264pay !             
udpsink host=192.168.86.26 port=5000"

if cap.isOpened() is not True:
    print("Cannot open camera. Exiting.")
    quit()
out = cv2.VideoWriter(gst_str_rtp, 0, fps, (frame_width, frame_height), True)
while True:
    ret, frame = cap.read()
    if ret is True:

        frame = cv2.flip(frame, 1)
        out.write(frame)
    else:
        print("Camera error.")
        time.sleep(10)

cap.release()

I would appreciate if someone can help me understand what I need to do to open up a stream that I can pass frames to.

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by supra56
close date 2020-10-04 15:10:46.579568

1 answer

Sort by » oldest newest most voted
1

answered 2020-09-03 05:13:47 -0600

makolele12 gravatar image

updated 2020-10-04 15:09:39 -0600

supra56 gravatar image

I was facing the same problem as you. It worked for me by adding the video properties as follows:

 "appsrc ! videoconvert ! videoscale ! video/x-raw,format=I420,width=1080,height=240,framerate=5/1 !  videoconvert ! x264enc tune=zerolatency bitrate=500 speed-preset=superfast ! rtph264pay !             
udpsink host=192.168.86.26 port=5000"

Try it out by using your own video properties.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-07-08 01:30:50 -0600

Seen: 15,774 times

Last updated: Oct 04 '20