import hailo_platform as hp
import cv2
import numpy as np
import os
HEF_FILE = “/home/ai_projects/model/yolov5s_coco–640x640_quant_hailort_multidevice_1.hef”
IMAGE_FILE = “/home/ai_projects/model/p.jpg”
MODEL_WIDTH, MODEL_HEIGHT = 640, 640
CONF_THRESHOLD = 0.3
if not os.path.exists(IMAGE_FILE):
raise FileNotFoundError(f"Image file not found: {IMAGE_FILE}")
image = cv2.imread(IMAGE_FILE)
orig_h, orig_w = image.shape[:2]
image_resized = cv2.resize(image, (MODEL_WIDTH, MODEL_HEIGHT))
input_data = np.expand_dims(image_resized.astype(np.uint8), axis=0)
-----------------------------
try:
Use InferenceModel for modern SDKs
model = hp.InferenceModel(HEF_FILE)
# Run inference using the model object
outputs = model.infer(input_data)
except Exception as e:
print(f"An error occurred: {e}")
Handle the error gracefully, for example, by exiting the script
exit()
-----------------------------
-----------------------------
detections = outputs[0]
display_image = image.copy()
for det in detections:
x1, y1, x2, y2, score, class_id = det
if score > CONF_THRESHOLD:
rescale to original image size
x1 = int(x1 * orig_w / MODEL_WIDTH)
x2 = int(x2 * orig_w / MODEL_WIDTH)
y1 = int(y1 * orig_h / MODEL_HEIGHT)
y2 = int(y2 * orig_h / MODEL_HEIGHT)
cv2.rectangle(display_image, (x1, y1), (x2, y2), (0, 255, 0), 2)
cv2.putText(display_image, str(int(class_id)), (x1, y1 - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
cv2.imshow(“Detections”, display_image)
cv2.waitKey(0)
cv2.destroyAllWindows()