Pujan-Dev's picture
fix: correct typo in README and update requirements.txt
7acfc7b
metadata
title: OCR NUMBERPLATE YOLO
emoji: 🌖
colorFrom: green
colorTo: gray
sdk: docker
pinned: false

Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference

Number Plate OCR Pipeline

A complete pipeline for detecting and recognizing vehicle number plates, supporting both Nepali and English characters with multi-line plate support.d

Features

  • 🎯 YOLO-based plate detection - Automatically locates number plates in images
  • 📝 Multi-line plate support - Handles single and multi-line plate formats
  • 🇳🇵 Nepali character recognition - Supports Devanagari script (क, ख, ग, etc.)
  • 🔤 English character recognition - A-Z, 0-9
  • 🔢 Embossed plate support - Works with both painted and embossed plates
  • 📊 Confidence scoring - Each character comes with a confidence score

Project Structure

pipeline/
├── __init__.py          # Package initialization
├── main.py              # Main pipeline entry point
├── config/
│   ├── __init__.py
│   └── config.py        # Configuration settings
├── model/
│   ├── __init__.py
│   ├── ocr.py           # OCR model definition
│   └── plate_detector.py # YOLO plate detector
├── utils/
│   ├── __init__.py
│   └── helper.py        # Helper functions
└── final_models/
    └── ocr_model_em_np_eng.pth  # Pre-trained OCR model

Requirements

pip install torch torchvision opencv-python numpy scikit-learn matplotlib ultralytics

Usage

Command Line

# Basic usage
python pipeline/main.py image.jpg

# Skip YOLO detection (process entire image)
python pipeline/main.py plate_image.jpg --no-yolo

# Save extracted characters and results
python pipeline/main.py image.jpg --save --output results.json

# Quiet mode (no progress messages)
python pipeline/main.py image.jpg -q

Python API

from pipeline import NumberPlateOCR

# Initialize with YOLO detection
ocr = NumberPlateOCR(use_yolo=True)

# Process an image
result = ocr.process_image('car_image.jpg', show_visualization=True)

# Get the plate number
for plate in result['plates']:
    print(f"Plate: {plate['singleline_text']}")
    print(f"Confidence: {plate['confidence_stats']['mean']:.1%}")

Process pre-cropped plate image

import cv2
from pipeline import NumberPlateOCR

ocr = NumberPlateOCR(use_yolo=False)  # No need for YOLO

plate_img = cv2.imread('cropped_plate.jpg')
result = ocr.process_from_plate_image(plate_img)

print(result['singleline_text'])

Configuration

Edit config/config.py to customize:

# OCR settings
OCR_CONFIG = {
    "input_size": (128, 128),
    "num_classes": 71,
}

# YOLO settings
YOLO_CONFIG = {
    "confidence_threshold": 0.5,
    "iou_threshold": 0.45,
}

# Character detection
CONTOUR_CONFIG = {
    "min_area": 100,
    "center_threshold": 15,  # For duplicate removal
}

# Inference
INFERENCE_CONFIG = {
    "min_confidence": 0.10,  # Minimum OCR confidence
}

Supported Characters

Type Characters
Digits (English) 0-9
Digits (Nepali) ०-९
Letters (English) A-Z
Nepali Text क, को, ख, ग, च, ज, झ, ञ, डि, त, ना, प, प्र, ब, बा, भे, म, मे, य, लु, सी, सु, से, ह
Special Nepali Flag

Output Format

{
  "image_path": "car.jpg",
  "num_plates": 1,
  "plates": [
    {
      "plate_index": 0,
      "lines": ["बा २", "प ४५६७"],
      "singleline_text": "बा २ प ४५६७",
      "multiline_text": "बा २\nप ४५६७",
      "num_lines": 2,
      "total_chars": 8,
      "confidence_stats": {
        "mean": 0.85,
        "min": 0.65,
        "max": 0.98
      }
    }
  ]
}

Model Files

  • final_models/ocr_model_em_np_eng.pth - OCR model (ResNet18-based)
  • ../best/best.pt - YOLO model for plate detection

Tips

  1. Better accuracy: Ensure good lighting and clear plate images
  2. Multi-line plates: The pipeline automatically detects line breaks
  3. Low confidence: Characters below 10% confidence are filtered out
  4. Border removal: The first 2 detected contours (usually borders) are skipped

Troubleshooting

"YOLO model not found"

  • Ensure best/best.pt exists in the project root

"OCR model not found"

  • Run: cp best/ocr_model_em_np_eng.pth pipeline/final_models/

Low accuracy

  • Adjust PREPROCESS_CONFIG["binary_threshold"] in config
  • Try different CONTOUR_CONFIG["min_area"] values