Hey there!
I am trying to implement the license plate detection. I found in model zoo some license plate detection model but it wasnt for hailo8l and in some thread i found compatible model .
Here is my code how I am trying to run this recognition but it cannot recognize anything at all. I am moving with license plate in all directions but nothing. What can be wrong here?
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("./tiny_yolov4_license_plates_new.hef") as hailo:
model_h, model_w, _ = hailo.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)
focus_mode = {"AeEnable": 1, "AfMode": controls.AfModeEnum.Manual, "LensPosition": 1}
picam2.set_controls(focus_mode)
picam2.start()
while True:
lores_frame = picam2.capture_array('lores')
results = hailo.run(lores_frame)
detections = extract_detections(results['tiny_yolov4_license_plates/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=5000)