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:
A DetectionResults class that safely stores and manages detection data
Modified app_callback that captures and stores results
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!