From 66f959174434b0aab5051e5f3e8b8acf683c2f1a Mon Sep 17 00:00:00 2001 From: David Plowman Date: Thu, 24 Oct 2024 13:28:34 +0100 Subject: [PATCH] Basic support for multiple Hailo models This allows multiple models to be loaded and used through separate Hailo objects. They should be used in serial in the same thread, however, as there is no other locking applied. Signed-off-by: David Plowman --- picamera2/devices/hailo/hailo.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/picamera2/devices/hailo/hailo.py b/picamera2/devices/hailo/hailo.py index bbbd0aaa..dbc3d437 100644 --- a/picamera2/devices/hailo/hailo.py +++ b/picamera2/devices/hailo/hailo.py @@ -6,6 +6,9 @@ class Hailo: + TARGET = None + TARGET_REF_COUNT = 0 + def __init__(self, hef_path, batch_size=None, output_type='FLOAT32'): """ Initialize the HailoAsyncInference class with the provided HEF model file path. @@ -20,7 +23,10 @@ def __init__(self, hef_path, batch_size=None, output_type='FLOAT32'): self.batch_size = batch_size self.hef = HEF(hef_path) - self.target = VDevice(params) + if Hailo.TARGET is None: + Hailo.TARGET = VDevice(params) + Hailo.TARGET_REF_COUNT += 1 + self.target = Hailo.TARGET self.infer_model = self.target.create_infer_model(hef_path) self.infer_model.set_batch_size(1 if batch_size is None else batch_size) self._set_input_output(output_type) @@ -175,4 +181,6 @@ def _create_bindings(self): def close(self): """Release the Hailo device.""" del self.configured_infer_model - self.target.release() + Hailo.TARGET_REF_COUNT -= 1 + if Hailo.TARGET_REF_COUNT == 0: + self.target.release()