What is the correct way to stream frames over udp sink using gstreamer in python?
I am reading frames from a USB camera using gstreamer (v4l2) and reading it using cv2.videoCapture. It works fine. I want to dump these frames using udp sink and gi. What is the correct way to achieve it? Following is my code, it works but after few minutes I get hugh lag. Kindly let me know what mistake I am doing. Thanks!
import cv2 import gi import numpy as np import time
gi.require_version("Gst", "1.0") from gi.repository import Gst, GObject, GLib
Gst.init(None) pipeline_str = ("appsrc name=source ! tiovxdlcolorconvert ! video/x-raw,format=NV12 ! queue ! jpegenc ! multipartmux boundary=spionisto ! rndbuffersize max=65000 ! udpsink host=127.0.0.1 port=8081 sync=false") pipeline = Gst.parse_launch(pipeline_str) source = pipeline.get_by_name("source") pipeline.set_state(Gst.State.PLAYING)
gst_pipeline = 'v4l2src device=/dev/video-usb-cam0 io-mode=2 ! image/jpeg, width=1280, height=720 ! jpegdec ! tiovxdlcolorconvert ! video/x-raw, format=NV12 !
tiovxmultiscaler ! video/x-raw, format=NV12 ! tiovxdlcolorconvert ! video/x-raw, format=RGB ! videoconvert ! video/x-raw, format=BGR ! queue ! appsink'
cap = cv2.VideoCapture(gst_pipeline, cv2.CAP_GSTREAMER)
try: while True: ret, frame = cap.read() if not ret: break
t1 = time.time()
raw_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
gst_buffer = Gst.Buffer.new_wrapped(raw_frame.tobytes())
caps = Gst.caps_from_string(f"video/x-raw,format=RGB,width={frame.shape[1]},height={frame.shape[0]}")
source.set_property("caps", caps)
source.emit("push-buffer", gst_buffer)
t2 = time.time()
print('FPS : ', 1/(t2-t1))
except KeyboardInterrupt: pass finally:
pipeline.set_state(Gst.State.NULL)
cap.release()