Hi everyone,
I try to run this object detection with web streaming on flask script :
import numpy as np
import cv2
import threading
from picamera2 import Picamera2
from picamera2.devices import Hailo
from flask import Flask, Response
from libcamera import controls
import libcamera
app = Flask(__name__)
output_frame = None
lock = threading.Lock()
def extract_detections(hailo_output, w, h, class_names, threshold=0.1):
results = []
for class_id, detections in enumerate(hailo_output):
for detection in detections:
score = detection[4]
if score >= threshold:
y0, x0, y1, x1 = detection[:4]
bbox = (int(x0 * w), int(y0 * h), int(x1 * w), int(y1 * h))
results.append([class_names[class_id], bbox, score])
return results
def draw_objects(frame, detections, w, h):
for class_name, bbox, score in detections:
x0, y0, x1, y1 = bbox
label = f"{class_name}: {score:.2f}"
cv2.rectangle(frame, (x0, y0), (x1, y1), (0, 255, 0), 2)
cv2.putText(frame, label, (x0, y0 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
return frame
def detection_thread():
global output_frame
with Hailo("yolov10n.hef") as hailo_model:
print('hailo model : ', hailo_model)
print("Configured infer model:", getattr(hailo_model, "configured_infer_model", None))
print("Infer model:", getattr(hailo_model, "infer_model", None))
model_h, model_w, _ = hailo_model.get_input_shape()
video_w, video_h = model_w, model_h
with open("coco.txt", 'r', encoding="utf-8") as f:
class_names = f.read().splitlines()
with Picamera2() as picam2:
main = {'size': (video_w, video_h), 'format': 'XRGB8888'}
lores = {'size': (model_w, model_h), 'format': 'RGB888'}
config = picam2.create_preview_configuration(main, lores=lores)
config["transform"] = libcamera.Transform(vflip=1, hflip=1)
picam2.configure(config)
picam2.start()
while True:
lores_frame = picam2.capture_array('lores')
print("Captured frame shape:", lores_frame.shape)
print("Model input shape:", model_h, model_w)
results = .run(lores_frame)
print("Hailo output keys:", results.keys())
print("Hailo output shape for 'yolov10n/conv19':", results.get('yolov10n/conv19', None))
detections = extract_detections(results['yolov10n/conv19'], model_w, model_h, class_names, 0.1)
lores_frame = draw_objects(lores_frame, detections, model_w, model_h)
with lock:
output_frame = lores_frame.copy()
def generate_frame():
global output_frame
while True:
with lock:
if output_frame is None:
continue
ret, jpeg = cv2.imencode('.jpg', output_frame)
frame = jpeg.tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
@app.route('/video_feed')
def video_feed():
return Response(generate_frame(), mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == "__main__":
threading.Thread(target=detection_thread, daemon=True).start()
app.run(host='0.0.0.0', port=2000)
But I get this error : ‘Hailo’ object has no attribute ‘configured_infer_model’
As you can see below
line 59, in detection_thread
results = hailo_model.run(lores_frame)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/picamera2/devices/hailo/hailo.py", line 167, in run
future = self.run_async(input_data)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/picamera2/devices/hailo/hailo.py", line 149, in run_async
bindings = self._create_bindings()
^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/picamera2/devices/hailo/hailo.py", line 179, in _create_bindings
return self.configured_infer_model.create_bindings(output_buffers=output_buffers)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'Hailo' object has no attribute 'configured_infer_model'
Does anyone have an idea of why i get this error please ?