Hello,
I created this code to perform inference on frames from two Arducam cameras. I’m running inference in batches of two images—one from each camera. The code works well, but before running inference, I’m getting 54 FPS. After performing inference on the two images, the frame rate drops to 28 FPS.
My model is a custom YOLOv8n with two classes, and I’m using a Raspberry Pi CM4. What do you suggest I can do to increase the inference rate?
Thanks!
from picamera2 import Picamera2
from picamera2.devices import Hailo
import numpy as np
import time
import cv2
def extract_detections(hailo_output, w, h, class_names, cam_id, threshold=0.5):
"""Extract detections from the HailoRT-postprocess output."""
results = []
for class_id, detections in enumerate(hailo_output):
for detection in detections:
detection_array = np.array(detection)
score = detection_array[4]
if score >= threshold:
y0, x0, y1, x1 = detection_array[:4]
bbox = (int(x0 * w), int(y0 * h), int(x1 * w), int(y1 * h))
results.append([class_names[class_id], bbox, score])
print(
f"Detection(s) found for class '{class_names[class_id]}', Score: {score:.2f}, Camera: {cam_id}"
)
return results
if __name__ == "__main__":
video_w, video_h = 1920, 1080
model = "yolov8n_D4.hef"
labels = "labels.txt"
score_thresh = 0.5
hailo = Hailo(model, 2)
model_h, model_w, _ = hailo.get_input_shape()
try:
with open(labels, "r", encoding="utf-8") as f:
class_names = f.read().splitlines()
except FileNotFoundError as file_error:
print(f"Error: The labels file '{labels}' was not found.")
raise file_error
except IOError as io_error:
print(f"Error: There was an issue reading the labels file '{labels}'")
raise io_error
except Exception as e:
print(f"Unexpected error while loading labels from '{labels}'")
raise e
detections = None
picam2a = Picamera2(0)
picam2b = Picamera2(1)
# Configure camera A
main_a = {'size': (video_w, video_h), 'format': 'XRGB8888'}
lores_a = {'size': (model_w, model_h), 'format': 'YUV420'}
controls_a = {
'FrameRate': 60,
"AfMode": 0, # Manual AF mode
"NoiseReductionMode": 0, # Noise reduction off
"LensPosition": 6.0,
}
config_a = picam2a.create_preview_configuration(
main_a, lores=lores_a, controls=controls_a)
picam2a.configure(config_a)
# Configure camera B
main_b = {'size': (video_w, video_h), 'format': 'XRGB8888'}
lores_b = {'size': (model_w, model_h), 'format': 'YUV420'}
controls_b = {
'FrameRate': 60,
"AfMode": 0,
"NoiseReductionMode": 0,
"LensPosition": 6.0,
}
config_b = picam2b.create_preview_configuration(
main_b, lores=lores_b, controls=controls_b)
picam2b.configure(config_b)
picam2a.start()
picam2b.start()
input_data = np.empty((2, model_h, model_w, 3), dtype=np.uint8)
start_time = time.time()
while True:
frame_a = picam2a.capture_array('lores')
frame_b = picam2b.capture_array('lores')
rgb_a = cv2.cvtColor(frame_a, cv2.COLOR_YUV420p2RGB)
rgb_b = cv2.cvtColor(frame_b, cv2.COLOR_YUV420p2RGB)
input_data[0] = rgb_a
input_data[1] = rgb_b
results = hailo.run(input_data)
for cam_id, result in enumerate(results):
detections = extract_detections(
result, video_w, video_h, class_names, cam_id, score_thresh
)