# ANPR with OpenALPR / Ultralytics
import cv2
from picamera2 import Picamera2
from ultralytics import YOLO
import easyocr
# Load models
plate_detector = YOLO('yolov8n-plate.pt') # Custom trained
ocr = easyocr.Reader(['en'])
picam2 = Picamera2()
picam2.configure(picam2.create_preview_configuration(
main={"size": (1920, 1080)}
))
picam2.start()
def process_frame(frame):
results = plate_detector(frame)
for box in results[0].boxes:
x1, y1, x2, y2 = map(int, box.xyxy[0])
plate_img = frame[y1:y2, x1:x2]
# OCR
text = ocr.readtext(plate_img, detail=0)
plate_number = ''.join(text).upper()
if plate_number:
print(f"Detected: {plate_number}")
# Send to DENDAT API
send_detection(plate_number, frame)
return results
while True:
frame = picam2.capture_array()
process_frame(frame)
tips_and_updates Build notes
Tips for ANPR:
1. Use 850nm IR LEDs (invisible to eye)
2. Camera angle: 15-30° to avoid reflection
3. Exposure: 1/500s to freeze motion
4. Train YOLO on local plate formats
5. Use hardware-accelerated encoding