Example... tested on pi5, 64bit raspiOS, hailo hat, logitech c270 camera
( based on https://github.com/raspberrypi/picamera ... /detect.py )
You'll need to install Opencv sudo apt install python3-opencv -y
Don't have a csi camera connected at the same time.
( based on https://github.com/raspberrypi/picamera ... /detect.py )
You'll need to install Opencv sudo apt install python3-opencv -y
Don't have a csi camera connected at the same time.
Code:
#!/usr/bin/env python3"""Example module for Hailo Detection using USB camera."""import argparseimport cv2from picamera2.devices import Hailoimport os# find USB cameracam1 = -1x = 0while cam1 == -1 and x < 42: txt = "v4l2-ctl -d " + str(x) + " --list-ctrls > /run/shm/cam_ctrls.txt" os.system(txt) ctrls = [] with open("/run/shm/cam_ctrls.txt", "r") as file: line = file.readline() while line: ctrls.append(line) line = file.readline() if 'User Controls\n' in ctrls and ('Camera Controls\n' in ctrls): cam1 = x else: x +=1if cam1 == -1: print(" No USB camera found !!")def extract_detections(hailo_output, w, h, class_names, threshold=0.5): """Extract detections from the HailoRT-postprocess output.""" 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 resultsdef draw_objects(request): current_detections = detections if current_detections: for class_name, bbox, score in current_detections: x0, y0, x1, y1 = bbox label = f"{class_name} %{int(score * 100)}" cv2.rectangle(frame, (x0, y0), (x1, y1), (0, 255, 0, 0), 2) cv2.putText(frame, label, (x0 + 5, y0 + 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0, 0), 1, cv2.LINE_AA)if __name__ == "__main__": # Parse command-line arguments. parser = argparse.ArgumentParser(description="Detection Example") parser.add_argument("-m", "--model", help="Path for the HEF model.", default="/usr/share/hailo-models/yolov8s_h8l.hef") parser.add_argument("-l", "--labels", default="coco.txt", help="Path to a text file containing labels.") parser.add_argument("-s", "--score_thresh", type=float, default=0.5, help="Score threshold, must be a float between 0 and 1.") args = parser.parse_args() # Get the Hailo model, the input size it wants, and the size of our preview stream. with Hailo(args.model) as hailo: model_h, model_w, _ = hailo.get_input_shape() video_w, video_h = 640,640 # Load class names from the labels file with open(args.labels, 'r', encoding="utf-8") as f: class_names = f.read().splitlines() # The list of detected objects to draw. detections = None # Configure and start CV2 Videocapture. cap = cv2.VideoCapture(cam1) if not cap.isOpened(): print("Cannot open camera") exit() # Process each low resolution camera frame. while True: # Capture frame-by-frame ret, frame = cap.read() frame = cv2.resize(frame, (model_h, model_w)) # Run inference on the preprocessed frame results = hailo.run(frame) # Extract detections from the inference results detections = extract_detections(results, video_w, video_h, class_names, args.score_thresh) draw_objects(detections) cv2.imshow('frame', frame) if cv2.waitKey(1) == ord('q'): breakStatistics: Posted by gordon77 — Sun Jun 22, 2025 10:22 am