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 ! '
        )
1 Like

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.

1 Like