Saving Detection Output to a Video File with Hailo RPI5 example

Hi everyone,

I’m working on the Hailo RPI5 example: GitHub Link.

I’m using an .mp4 file as input, and when I run the detection, a window appears showing the detections on the video.

But I want to save the output with the detections applied, so I can watch it later without having to rerun the script.

I noticed that there is a FILE_SINK_PIPELINE in gstreamer_helper_pipelines, so I tried to use it.

In the detection_pipeline file, inside get_pipeline_string, I added these two lines:

file_sink_pipeline = FILE_SINK_PIPELINE(output_file = 'test_output.mkv')

f'{file_sink_pipeline}'

and i commented

#display_pipeline = DISPLAY_PIPELINE(video_sink=self.video_sink, sync=self.sync, show_fps=self.show_fps)

#f'{display_pipeline}'

so my function get_pipeline_string is now :

def get_pipeline_string(self):
        source_pipeline = SOURCE_PIPELINE(self.video_source, self.video_width, self.video_height)
        detection_pipeline = INFERENCE_PIPELINE(
            hef_path=self.hef_path,
            post_process_so=self.post_process_so,
            post_function_name=self.post_function_name,
            batch_size=self.batch_size,
            config_json=self.labels_json,
            additional_params=self.thresholds_str)
        detection_pipeline_wrapper = INFERENCE_PIPELINE_WRAPPER(detection_pipeline)
        tracker_pipeline = TRACKER_PIPELINE(class_id=1)
        user_callback_pipeline = USER_CALLBACK_PIPELINE()
        #display_pipeline = DISPLAY_PIPELINE(video_sink=self.video_sink, sync=self.sync, show_fps=self.show_fps)
        file_sink_pipeline = FILE_SINK_PIPELINE(output_file = 'test_output.mkv')

        pipeline_string = (
            f'{source_pipeline} ! '
            f'{detection_pipeline_wrapper} ! '
            f'{tracker_pipeline} ! '
            f'{user_callback_pipeline} ! '
            f'{file_sink_pipeline}'
            #f'{display_pipeline}'
        )
        print(pipeline_string)
        return pipeline_string

When I run detection.py, it seems to work because the frame count keeps increasing until the end, but then I get this error:

Frame count: 350

Frame count: 351

Frame count: 352

End-of-stream
Error rewinding the video

An output file is created, but it’s just the same as the input video, without any detections applied.

Also, in the FILE_SINK_PIPELINE function, there is a recommendation to run ffmpeg after recording to fix the file header:

ffmpeg -i output.mkv -c copy fixed_output.mkv

I’m not sure if I followed the correct process.

Has anyone managed to save the output with detections using detection.py? Or does anyone know how to fix this?