License Plate Edge Detection with YOLOv8 & KerasCV

Hugging Face Model License: MIT Framework: Keras 3 Medium Blog GitHub

Blog Thumbnail

An end-to-end pipeline for training, optimizing, and deploying an edge-ready YOLOv8 License Plate Detection model using YOLOv8-XS and KerasCV. Built specifically for real-time edge-device deployment without requiring proprietary licenses (no Ultralytics license required).

๐Ÿ“– Read the full tutorial on Medium: [Medium blog]

Overview

This repository contains a streamlined Jupyter Notebook (license_plate_detection.ipynb) tailored for training an extra-small YOLOv8 object detection model. Built for scalability using a custom Roboflow Pascal VOC dataset, the workflow ensures that the final configuration is fully optimized for edge-device deployment. The final outputs include highly optimized exports for both TFLite (for Android/Edge TPU, featuring INT8 quantization) and CoreML (.mlpackage for iOS/macOS integration).

Key Features

  • Scalable Data Pipeline: Custom XML Pascal VOC parsing paired with a highly concurrent tf.data.Dataset preparation layer.
  • Model Efficiency: Utilizes KerasCV's yolo_v8_xs_backbone, which is deliberately shallow and heavily optimized for mobile processing constraints.
  • Static Edge Graphing: Directly addresses dynamic computation graph crashes (a well-known CoreML/TFLite export issue) by mapping static tensor shapes.
  • Advanced Post-Processing: Hardcodes strict multi-class Non-Max Suppression (NMS) to effectively merge overlapping bounding boxes right at the model's output layer.
  • Multi-Platform Deployment Builds:
    • TFLite (.tflite): Utilizes a representative data generator to perform precise INT8 weight quantization.
    • CoreML (.mlpackage): Integrates cleanly into Swift/Xcode, actively utilizing ComputeUnit.ALL for Apple Neural Engine support.

Detection Results

Before Detection After Detection
Before Detection After Detection

Pre-Trained Models

The repository provides fully-trained, edge-optimized weights across multiple formats:

Format Filename Size Target Hardware / Platform Description
Keras 3 best_edge_detector.keras ~41.9 MB Python, Servers, GPUs Raw Keras weights. Use for resuming training or running Python inference.
TFLite (INT8) edge_detector_quantized.tflite ~3.8 MB Raspberry Pi, Android, Coral Edge TPU INT8-quantized payload ready to drop into edge devices.
Apple CoreML EdgeDetector.mlpackage.zip ~6.3 MB iOS, iPadOS, macOS Extracted modern CoreML package for Apple's Neural Engine via Swift/Xcode.

Quickstart & Inference

1. Keras Inference (Python)

Install dependencies:

pip install tensorflow keras-cv huggingface_hub opencv-python matplotlib

Load the model from Hugging Face Hub (or locally) and run detection:

import cv2
import keras
import keras_cv
import tensorflow as tf
from huggingface_hub import hf_hub_download

# Download weights from Hugging Face Hub (or use local path)
model_path = hf_hub_download(
    repo_id="Mithil-AI/yolov8-license-plate-detector",
    filename="best_edge_detector.keras"
)

# Load model
inference_model = keras.models.load_model(model_path, compile=False)

# Configure strict NMS filtering (merging overlapping predictions)
inference_model.prediction_decoder = keras_cv.layers.MultiClassNonMaxSuppression(
    bounding_box_format="xyxy",
    from_logits=False,
    max_detections=50,
    iou_threshold=0.30,
    confidence_threshold=0.50,
)

def load_and_preprocess(image_path):
    img = cv2.imread(image_path)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    orig_h, orig_w = img.shape[:2]
    img_resized = cv2.resize(img, (416, 416))
    img_tensor = tf.expand_dims(tf.cast(img_resized, tf.float32), axis=0)
    return img, img_tensor, orig_w, orig_h

# Run inference
img, img_tensor, orig_w, orig_h = load_and_preprocess("images/before detection.jpg")
predictions = inference_model.predict(img_tensor, verbose=0)

boxes = predictions["boxes"][0]
classes = predictions["classes"][0]
confidences = predictions["confidence"][0]

2. TFLite INT8 Inference (Raspberry Pi / Android)

import numpy as np
import tensorflow as tf
from huggingface_hub import hf_hub_download

tflite_path = hf_hub_download(
    repo_id="Mithil-AI/yolov8-license-plate-detector",
    filename="edge_detector_quantized.tflite"
)

interpreter = tf.lite.Interpreter(model_path=tflite_path)
interpreter.allocate_tensors()

input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

input_data = np.zeros((1, 416, 416, 3), dtype=np.float32)
interpreter.set_tensor(input_details[0]["index"], input_data)
interpreter.invoke()

outputs = [interpreter.get_tensor(o["index"]) for o in output_details]

3. Apple CoreML (iOS / macOS)

# Download and unzip CoreML package
hf download Mithil-AI/yolov8-license-plate-detector EdgeDetector.mlpackage.zip --local-dir .
unzip EdgeDetector.mlpackage.zip

Integrate EdgeDetector.mlpackage into Xcode and target Apple Neural Engine via ComputeUnit.ALL.

Training Architecture & Specs

  • Model: YOLOv8-XS (yolo_v8_xs_backbone via KerasCV)
  • Input Resolution: 416 x 416 x 3 (Static tensor graph)
  • Feature Pyramid: Shallow FPN (fpn_depth=1)
  • Dataset: Roboflow Pascal VOC License Plate Detection dataset
  • Classes: 1 class (LicensePlate)
  • Loss: Binary Crossentropy (classes) + CIoU (bounding boxes)
  • Optimizer: Adam (1e-4 learning rate)

Sanity Check Visualization

Data Visualization

Notebook Structure

The execution notebook (license_plate_detection.ipynb) contains 10 distinct sections:

  1. Setup: Bootstraps the environment and downloads dataset.
  2. Imports: Binds TensorFlow and KerasCV.
  3. Data Parsing: Safely extracts coordinates from VOC XML annotations.
  4. Dataset Building: Projects ragged dimensions into dense tensor tuples for pure XLA acceleration.
  5. Sanity Checking: Verifies bounding box fidelity before deep training.
  6. Detector Assembly: Patches the YOLOv8 layers together enforcing edge static resolutions.
  7. Training Loops: Executes Adam-guided learning complete with Model Checkpoint & Early Stopping.
  8. Inference: Applies NMS and renders real-time graphical results.
  9. TFLite Exporting: Compiles to edge_detector_quantized.tflite.
  10. Apple CoreML Exporting: Compiles to EdgeDetector.mlpackage.

License

MIT License

Downloads last month
-
Inference Providers NEW
This model isn't deployed by any Inference Provider. ๐Ÿ™‹ Ask for provider support