python-3.xopencvgstreamernvidia-jetson

Unable to show video stream using Gstreamer and OpenCV in Jetson-Nano


I'm trying to open a video stream using OpenCV and G stream from an IP camera. The pipeline reads each frame, but no window is being shown with imshow. It works fine when I run the pipeline with gst-launch-1.0 and nvoverlaysink on bash. The following is the python code that I have been using:

import cv2 as cv

gst = "rtspsrc location=rtsp://usser:pass@ip:port/url latency=0 ! rtph264depay ! h264parse ! avdec_h264 ! videoconvert ! appsink"
capture = cv.VideoCapture(gst, cv.CAP_GSTREAMER)

print("Is pipeline open: ", capture.isopened())
while True:
     ret, frame = capture.read()
     print("Is receiving frames: ", ret)
     cv.imshow("Stream",frame)

capture.release()

When I run the script, I can see that the pipeline is open and OpenCV is reading frames. I only have the following warning:

[ WAN:0] global /tmp/pip-install-r_pt66np/opencv-python/opencv/modules/videoto/
src/cap_gstreamer.cpp (935) open Opencv | GStreamer warning: Cannot query video
position: status=1, value=0, duration=-1 

Although the script runs and consumes frames, imshow doesn't generate a window.


Solution

  • The problem wasn't the pipeline. I had so many errors with the pipeline that I suppose that the error was there. imshow needs a wait key to work. Here it's the working code:

    import cv2 as cv
    
    gst = "rtspsrc location=rtsp://usser:pass@ip:port/url latency=0 ! rtph264depay ! h264parse ! avdec_h264 ! videoconvert ! appsink"
    capture = cv.VideoCapture(gst, cv.CAP_GSTREAMER)
    
    print("Is pipeline open: ", capture.isopened())
    while capture.isOpened() :
         ret, frame = capture.read()
         print("Is receiving frames: ", ret)
         cv.imshow("Stream", frame)
         
         if cv.waitKey(25) & 0xFF == ord("q"):
              break
    
    capture.release()
    cv.destroyAllWindows()