The following code works well without cv2.CAP_GSTREAMER in cv2.VideoCapture. When I add cv2.GSTREAMER however (in app_stream_in), it gets hung, or fails.
Any idea why?
import cv2
gstreamer_appsink = "rtsp://user:pass@192.168.0.153:554/cam/realmonitor?channel=1&subtype=0 latency=0 ! queue ! rtph265depay ! queue ! h265parse ! omxh265dec ! nvvidconv ! video/x-raw,width=1920,height=1080,format=BGRx,framerate=(fraction)30/1 ! videoconvert ! video/x-raw,format=BGR ! appsink "
gstreamer_udpsink = "appsrc ! video/x-raw, format=(string)BGR ! videoconvert ! video/x-raw, format=(string)I420, framerate=(fraction)30/1 ! nvvidconv ! omxh265enc control-rate=2 bitrate=4000000 ! rtph265pay mtu=1400 config-interval=3 pt=96 ! udpsink host=10.0.0.176 port=5000"
print(gstreamer_appsink)
print(gstreamer_udpsink)
app_stream_in = cv2.VideoCapture(gstreamer_appsink)
app_stream_out = cv2.VideoWriter(gstreamer_udpsink, cv2.CAP_GSTREAMER, 0, 30, (1920,1080), True)
print(app_stream_in.isOpened())
print(app_stream_out.isOpened())
if not app_stream_in.isOpened() or not app_stream_out.isOpened():
print('VideoCapture or VideoWriter not opened')
exit(0)
n = 0
while True:
# print("frame", n)
ret, frame = app_stream_in.read()
# print(frame.shape)
# frame = frame.reshape(1920,1080,3)
if not ret:
print('empty frame')
break
app_stream_out.write(frame)
n += 1
The above code works, so as the one I shared for VideoCapture. Somehow though it only works without the cv2.CAP_GSTREAMER command. The one you shared above works with and without cv2.CAP_GSTREAMER with my IP camera.
But when I transcode that with H265 and set up a UDP stream, the UDP stream continuously lags, and after 5 minutes or so the lag becomes very very large and the stream is unusable (about 1 minute). Is this something to do with Python? Is it slow because I’m using OpenCV (Python) as the middle man?
Also, do you have an example for the following case in C or C++ (if Python is too slow I can’t use it).
My general use case is,
IP Camera (RTSP or UDP feed) —> Load to Python for ML Processing (Currently handled through Gstreamer and OpenCV) —> Create a RTSP or UDP stream of the processed feed and make the stream accessible to the local network