Extract results from detection RPI5 gstreamer

Hello everyone,
I tested detection.py from the following link: https://github.com/hailo-ai/hailo-rpi5-examples/blob/95d6e010a6a47109e10ac795c7d24d14d7e0a813/basic_pipelines/hailo_rpi_common.py, it works very well for me, I would like to know if it is possible to use the results of the detections


outside the function def “app_callback(pad, info, user_data):” in another function for example

Hey @mAty1901 ,

To extract detection results from the app_callback function and use them elsewhere in your code, you’ll need to implement a shared data storage solution. Here’s a practical implementation:

class DetectionResults:
    def __init__(self):
        self.detections = []
        
    def update_detections(self, detections):
        self.detections = detections
        
    def get_detections(self):
        return self.detections

# Create a shared instance
detection_results = DetectionResults()

def app_callback(pad, info, user_data):
    buffer = info.get_buffer()
    if buffer is None:
        return Gst.PadProbeReturn.OK
        
    roi = hailo.get_roi_from_buffer(buffer)
    detections = roi.get_objects_typed(hailo.HAILO_DETECTION)
    
    results = []
    for detection in detections:
        results.append({
            "label": detection.get_label(),
            "confidence": detection.get_confidence(),
            "bbox": (
                detection.get_bbox().xmin(),
                detection.get_bbox().ymin(), 
                detection.get_bbox().xmax(),
                detection.get_bbox().ymax()
            )
        })
    
    detection_results.update_detections(results)
    return Gst.PadProbeReturn.OK

# Example usage
def process_detections():
    results = detection_results.get_detections()
    for result in results:
        print(f"Detected: {result['label']} ({result['confidence']*100:.2f}%)")
        print(f"Bounding box: {result['bbox']}")

if __name__ == "__main__":
    app = GStreamerDetectionApp(app_callback, None)
    app.run()
    process_detections()

The key parts of this solution are:

  1. A DetectionResults class that safely stores and manages detection data
  2. Modified app_callback that captures and stores results
  3. Helper functions to access the stored results
  • This Code has not been Tested!

This approach lets you access detection data from anywhere in your code while keeping the implementation clean and maintainable. Let me know if you need any clarification or have questions about implementing this in your specific use case!