Hailo Object Detection: Read from and Write to RTSP Server

The following edits extend the object detection examples in ’hailo-rpi5-examples’ to optionally read from a RTSP stream topic and to write the pipeline detection stream to an RTSP Server in parallel to the output on the rpi5 screen.

Therefore edit the file hailo_rpi_common.py file and add the following lines:

  1. read from RTSP stream:
def get_source_type(input_source):
    # This function will return the source type based on the input source
    # return values can be "file", "rpi", "rtsp" or "usb"
    if input_source.startswith("/dev/video"):
        return 'usb'
    else:
        if input_source.startswith("rpi"):
            return 'rpi'
        elif input_source.startswith("rtsp"): #check for streaming from a rtsp source stream
            return 'rtsp'
        else:
            return 'file'

To read from a RTSP Stream, choose option:

--input rtsp://<myRTSP-Server address>:8554/my_stream

  1. Change the following lines to write detection pipeline results to an RTSP Server:
    # Construct the display pipeline string
    display_pipeline = (
        f'{QUEUE(name=f"{name}_hailooverlay_q")} ! '
        f'hailooverlay name={name}_hailooverlay ! '
        f'tee name=rtspstream ! '  # Add a tee after hailooverlay
        f'{QUEUE(name=f"{name}_videoconvert_q")} ! '
        f'videoconvert name={name}_videoconvert n-threads=2 qos=false ! '
        f'{QUEUE(name=f"{name}_q")} ! '
        f'fpsdisplaysink name={name} video-sink={video_sink} sync={sync} text-overlay={show_fps} signal-fps-measurements=true '
        f'rtspstream. ! '  # Branch for RTSP stream
        f'videoconvert ! video/x-raw,format=I420 ! '
        f'x264enc tune=zerolatency bitrate=500 speed-preset=superfast ! '
        f'video/x-h264,profile=baseline ! '
        f'rtspclientsink location=rtsp://localhost:8554/mystream '
    )
    return display_pipeline

You also need to extend the Source Pipeline definition with the RTSP Streaming option by adding the following lines:

def SOURCE_PIPELINE(video_source, video_format='RGB', 
<.....>

    source_type = get_source_type(video_source)

    if source_type == 'rpi':
        source_element = (
            f'libcamerasrc name={name} ! '
            f'video/x-raw, format={video_format}, width=1536, height=864 ! '
        )
    elif source_type == 'usb':
        source_element = (
            f'v4l2src device={video_source} name={name} ! '
            'video/x-raw, width=640, height=480 ! '
        )
    # here we add the definition for the RTSP location pipeline:
    elif source_type == "rtsp":
        source_element = (
            f'rtspsrc location={video_source} name=src_0  message-forward=true ! '
            f'rtph264depay ! '
            f'queue name=hailo_preprocess_q_0 leaky=no max-size-buffers=5 max-size-bytes=0 max-size-time=0 ! '
            f'decodebin ! queue leaky=downstream max-size-buffers=5 max-size-bytes=0 max-size-time=0 ! '
            f'video/x-raw, format=I420 ! '
        )
    else:
        source_element = (
            f'filesrc location="{video_source}" name={name} ! '
            f'{QUEUE(name=f"{name}_queue_dec264")} ! '
            'qtdemux ! h264parse ! avdec_h264 max-threads=2 ! '
        )

Thank you.
I’d be fiddling around looking at some older posts that seemed to refer to a previous version of the example python.
Your suggestions worked first time.
Much appreciated.

im using siyi a8 ip camera ,
SOURCE_PIPELINE AND get_source_type functions are working well and i can test perfectly without streaming processed data on rtsp but when i change display function im getting error after first frame like this:
Frame count: 1

Error: gst-resource-error-quark: Resource not found. (3), ../gst/rtsp-sink/gstrtspclientsink.c(3106): gst_rtsp_client_sink_send (): /GstPipeline:pipeline0/GstRTSPClientSink:rtspclientsink0:
Not Found
Shutting down… Hit Ctrl-C again to force quit.
Frame count: 2

Frame count: 3

Frame count: 4

is there any other step which i dont know?

I have unfortunately no idea what the problem is. Please ensure that you have all gst plugins installed and that your rtsp server is up and running.

probably im starting wrong my rtsp server ,i tried 2 different way to start but nothing change ,is there any chance to explain how your starting rtsp server ? libraries or code used extra