Running Multiple Object Detection Scripts Simultaneously

Hi,

is it possible to run multiple object detection scripts simultaneously? I mean to set up, for example, API servers on an RPI for different recognition models, like one API for model1 detections, another API for model2 detections, and so on. Then, run them all in the background and manage them separately.
The reason I’m asking is that when I try to run more than one detection script at a time, I get an error about the Hailo devices being in use. Maybe I’m using the wrong terminology, but I hope you understand the situation.

Thanks!

Hi @DimaR,

For running multiple models we have something called model scheduler. You can see how it can be used with Python here.

As you can see in the example, the same VDevice(params) needs to be used for the different models. This means that the scripts for inference can’t be completely separate, they would have to have a common target.

@nina-vilela

Despite following the tutorial closely, I’m encountering a segmentation fault when the OCR inference runs after a detection is made. The script runs fine if there are no detections, but as soon as the OCR model processes its first input, it crashes with a segmentation fault.

I’ve tried several approaches, including ensuring that I’m using a single VDevice instance with the model scheduler enabled and the same group_id. I also attempted to use threading locks to synchronize access to the Hailo SDK, but the issue persists.

Here’s a simplified version of my code focusing on the Hailo SDK and threading:

import threading
from queue import Queue
import numpy as np
from functools import partial
from hailo_platform import VDevice, HailoSchedulingAlgorithm, FormatType

frame_queue = Queue(maxsize=100)
ocr_queue = Queue(maxsize=100)

def grab_frames():
    while True:
        # Simulate grabbing a frame
        frame = np.random.randint(0, 255, (640, 640, 3), dtype=np.uint8)
        frame_queue.put(frame)

def detection_thread(vdevice, detection_model_path):
    try:
        infer_model = vdevice.create_infer_model(detection_model_path)
        infer_model.set_batch_size(1)
        with infer_model.configure() as configured_infer_model:
            while True:
                frame = frame_queue.get()
                image_data = np.expand_dims(frame, axis=0)
                bindings = configured_infer_model.create_bindings()
                bindings.input().set_buffer(image_data)
                output_shape = infer_model.output().shape
                output_buffer = np.empty(output_shape, dtype=np.float32)
                bindings.output().set_buffer(output_buffer)
                configured_infer_model.wait_for_async_ready(timeout_ms=10000)
                job = configured_infer_model.run_async(
                    [bindings],
                    partial(detection_callback, bindings=bindings)
                )
                job.wait(timeout_ms=10000)
                frame_queue.task_done()
    except Exception as e:
        print(f"Exception in detection_thread: {e}")

def detection_callback(completion_info, bindings):
    if completion_info.exception:
        print(f"Inference error: {completion_info.exception}")
    else:
        # Process detection results
        # If a detection is found, put the frame into ocr_queue
        ocr_queue.put(bindings.input().get_buffer()[0])

def ocr_thread(vdevice, ocr_model_path):
    try:
        infer_model = vdevice.create_infer_model(ocr_model_path)
        infer_model.set_batch_size(1)
        with infer_model.configure() as configured_infer_model:
            while True:
                frame = ocr_queue.get()
                image_data = np.expand_dims(frame, axis=0)
                bindings = configured_infer_model.create_bindings()
                bindings.input().set_buffer(image_data)
                output_shape = infer_model.output().shape
                output_buffer = np.empty(output_shape, dtype=np.float32)
                bindings.output().set_buffer(output_buffer)
                configured_infer_model.wait_for_async_ready(timeout_ms=10000)
                job = configured_infer_model.run_async(
                    [bindings],
                    partial(ocr_callback, bindings=bindings)
                )
                job.wait(timeout_ms=10000)
                ocr_queue.task_done()
    except Exception as e:
        print(f"Exception in ocr_thread: {e}")

def ocr_callback(completion_info, bindings):
    if completion_info.exception:
        print(f"OCR Inference error: {completion_info.exception}")
    else:
        # Process OCR results
        pass

def main():
    vdevice_params = VDevice.create_params()
    vdevice_params.scheduling_algorithm = HailoSchedulingAlgorithm.ROUND_ROBIN
    vdevice_params.group_id = "SHARED"  

    with VDevice(vdevice_params) as vdevice:
        # Start the frame grabbing thread
        threading.Thread(target=grab_frames, daemon=True).start()

        # Start the detection thread
        threading.Thread(
            target=detection_thread,
            args=(vdevice, "./license_plate_model.hef"),
            daemon=True
        ).start()

        # Start the OCR thread
        threading.Thread(
            target=ocr_thread,
            args=(vdevice, "./license_plate_ocr.hef"),
            daemon=True
        ).start()

        # Keep the main thread alive
        threading.Event().wait()

if __name__ == '__main__':
    main()

Notes:

  • I’m using a single VDevice instance shared across threads.
  • The scheduling algorithm is set to ROUND_ROBIN, and the group_id is set to "SHARED".
  • The grab_frames function simulates frame grabbing and puts frames into frame_queue.
  • The detection_thread processes frames from frame_queue and, upon detecting an object, puts frames into ocr_queue.
  • The ocr_thread processes frames from ocr_queue.

Issue:

  • When the OCR thread starts processing after a detection, the script crashes with a segmentation fault (it still processes the OCR and all fine there but after processgin OCR in 30sec the error appears).
  • If there are no detections, the script runs indefinitely without crashing.
  • Adding threading locks or using multiprocessing did not resolve the issue.

Request:

Could someone please help me identify what might be causing this segmentation fault when the OCR inference runs? Is there a specific way I should manage the VDevice and models in a multithreaded environment with the Hailo SDK?

Any guidance or suggestions would be greatly appreciated!