How to open rtspsrc as VideoCapture

This works in terminal:

 ./test-launch "v4l2src device=/dev/video2 do-timestamp=1 ! video/x-raw, width=2560, height=720, framerate=60/1 ! nvvidconv ! nvv4l2h265enc insert-vui=1 insert-sps-pps=1 ! h265parse ! rtph265pay name=pay0"
gst-launch-1.0 rtspsrc location=rtsp://127.0.0.1:8554/test latency=0 ! application/x-rtp,media=video,encoding-name=H265 ! rtph265depay ! h265parse ! nvv4l2decoder ! nvvidconv ! xvimagesink

How do I create a videocapture to get the rtspsrc.

    gst_in = "rtspsrc location=rtsp://127.0.0.1:8554/test latency=0 ! application/x-rtp,media=video,encoding-name=H265 ! rtph265depay ! h265parse ! nvv4l2decoder ! videoconvert ! appsink"
    camera = cv2.VideoCapture(gst_in, cv2.CAP_GSTREAMER)

I tried the above VideoCapture object but it just says Opening in BLOCKING MODE and gets stuck.

Hi,
Please check if you can run this sample:
Doesn't work nvv4l2decoder for decoding RTSP in gstreamer + opencv - #3 by DaneLLL

The sample works however when I tested it on my rtsp url I don’t see any frames in opencv. The opencv icon does pop up however my computer gets much slower and I don’t see the video stream. The return of .read() is also true. Could it be that my frame size and framerate is too fast for opencv? I’m running at 60FPS 2560x720. Here’s the code for reference:

import sys
import cv2

def read_cam():
    cap = cv2.VideoCapture("rtspsrc location=rtsp://127.0.0.1:8554/test ! rtph265depay ! h265parse ! nvv4l2decoder ! nvvidconv ! video/x-raw, format=(string)BGRx ! videoconvert ! video/x-raw,format=BGR ! appsink ")
    if cap.isOpened():
        cv2.namedWindow("demo", cv2.WINDOW_AUTOSIZE)
        while True:
            ret_val, img = cap.read()
            print(ret_val)
            cv2.imshow('demo',img)
            cv2.waitKey(10)
    else:
     print("rtsp open failed")

    cv2.destroyAllWindows()


if __name__ == '__main__':
    read_cam()

./test-launch “v4l2src device=/dev/video2 do-timestamp=1 ! video/x-raw, width=2560, height=720, framerate=60/1 ! nvvidconv ! nvv4l2h265enc insert-vui=1 insert-sps-pps=1 ! h265parse ! rtph265pay name=pay0”

It works now with VideoWriter to an xvimagesink. I’m not sure why imshow didn’t work but I’ll just use this now:

gst_out = "appsrc ! queue ! videoconvert ! videoscale method=0 ! xvimagesink "
vw = cv2.VideoWriter(gst_out, cv2.CAP_GSTREAMER, 0, 40, (2560, 720))
1 Like

Hi,
Not sure but probably the resolution(2560x720) is special so it cannot be shown in imshow(). Using xvimagesink looks to be a good solution.

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