Skip to content

KPandya1903/Vehicle-Matching-System

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš— Automated Vehicle Re-Identification & Temporal Matching System

Python PyTorch OpenCV YOLOv8 License

A production-ready computer vision pipeline for matching vehicle entry/exit events using multi-engine OCR, fuzzy temporal matching, and optimized image enhancement strategies.

πŸ“‹ Table of Contents

🎯 Overview

This system automatically identifies and matches vehicle entry/exit events from a dataset of 2,000+ images by:

  1. Extracting metadata and bounding boxes from cloud storage headers
  2. Detecting and cropping license plates using computer vision
  3. Running multi-strategy OCR with Fast-ALPR and EasyOCR fallback
  4. Applying fuzzy string matching with temporal logic for entry/exit pairing

Challenge Solved: Given randomized vehicle images with no explicit pairing information, the system achieves 98.15% OCR accuracy and 60.31% vehicle pairing rate through intelligent enhancement strategies and temporal filtering.

πŸ—οΈ System Architecture

graph LR
    A[Image URLs] --> B[Metadata Extractor]
    B --> C[SQLite Database]
    C --> D[Plate Detection YOLOv8]
    D --> E[Plate Cropping]
    E --> F[Multi-Engine OCR]
    F --> G[Image Enhancement]
    G --> H[Fast-ALPR Primary]
    G --> I[EasyOCR Fallback]
    H --> J[Text Normalization]
    I --> J
    J --> K[Fuzzy Matcher]
    K --> L[Temporal Filter]
    L --> M[Greedy Pairing]
    M --> N[Submission Output]

    style F fill:#f9f,stroke:#333,stroke-width:2px
    style K fill:#bbf,stroke:#333,stroke-width:2px
    style M fill:#bfb,stroke:#333,stroke-width:2px
Loading

Pipeline Stages

  1. Metadata Parsing πŸ”

    • Async HTTP HEAD requests (30 concurrent)
    • Extracts Last-Modified timestamps for temporal logic
    • Parses GCS metadata headers for bbox coordinates
    • 99.85% bbox extraction success rate
  2. License Plate Detection πŸ“Έ

    • YOLOv8 object detection for plates
    • Metadata bbox fallback (99.85% coverage)
    • ~115 images/second processing speed
  3. Multi-Engine OCR πŸ”€

    • Fast-ALPR (Primary): Specialized ALPR with ONNX runtime
    • EasyOCR (Fallback): General-purpose OCR engine
    • 6-Strategy Enhancement: CLAHE, sharpening, bilateral filtering, adaptive thresholding, morphological operations
    • Character normalization (0/O, 1/I, 8/B, 5/S)
  4. Fuzzy Temporal Matching 🎯

    • Two-phase greedy algorithm:
      • Phase 1: Exact matches (100% similarity)
      • Phase 2: Fuzzy matches (75-99% similarity)
    • Temporal filtering (<72 hours between entry/exit)
    • Levenshtein distance with time proximity weighting

✨ Key Features

🎨 Multi-Strategy Image Enhancement

Applies 6 different enhancement techniques and selects the best OCR result:

  • CLAHE: Contrast-limited adaptive histogram equalization
  • Sharpening: Kernel-based edge enhancement
  • Bilateral: Noise reduction while preserving edges
  • Adaptive Threshold: Binarization for varying lighting
  • Morphological: Closing operations for character connectivity

🧠 Intelligent Character Normalization

Handles common OCR ambiguities:

character_mapping = {
    "0" ↔ "O",  # Zero and letter O
    "1" ↔ "I",  # One and letter I
    "8" ↔ "B",  # Eight and letter B
    "5" ↔ "S",  # Five and letter S
}

⏱️ Temporal Logic

  • Timestamp-based filtering prevents incorrect matches
  • Time proximity weighting: closer timestamps = higher confidence
  • Configurable max time difference (default: 72 hours)

πŸ“Š Production-Grade Error Handling

  • Async retry logic for network failures
  • Database transaction rollbacks
  • Comprehensive logging with levels (DEBUG, INFO, WARNING, ERROR)
  • Graceful degradation (EasyOCR fallback if Fast-ALPR fails)

πŸ› οΈ Technical Stack

Category Technology
Language Python 3.8+
Computer Vision OpenCV 4.8+, PIL
Object Detection Ultralytics YOLOv8
Deep Learning PyTorch 2.0+, TorchVision
OCR Fast-ALPR (ONNX), EasyOCR
String Matching FuzzyWuzzy, python-Levenshtein
Async I/O aiohttp, asyncio
Database SQLite3
Configuration PyYAML
Testing pytest, pytest-asyncio

πŸ“¦ Installation

Prerequisites

  • Python 3.8 or higher
  • CUDA-capable GPU (optional, for faster inference)

Setup

  1. Clone the repository
git clone https://github.com/KPandya1903/Vehicle-Matching-System.git
cd Vehicle-Matching-System
  1. Create virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
  1. Install dependencies
pip install -r requirements.txt

πŸš€ Usage

Quick Start

Run the complete pipeline:

python main.py --input vehicle_images_input.txt --output submission.txt

Step-by-Step Execution

  1. Extract Metadata Only
python main.py --phase metadata --input vehicle_images_input.txt
  1. Run OCR Extraction
python main.py --phase ocr
  1. Perform Matching
python main.py --phase matching
  1. Export Submission
python main.py --phase export --output submission.txt

Configuration

Edit configs/config.yaml to customize:

  • OCR confidence thresholds
  • Matching similarity thresholds
  • Maximum time difference
  • Image enhancement strategies
  • Logging levels

Example:

matching:
  min_similarity_threshold: 75
  max_time_difference_hours: 72

ocr:
  fast_alpr:
    confidence_threshold: 0.5

πŸ“ Project Structure

Vehicle-Matching-System/
β”œβ”€β”€ src/                          # Source code
β”‚   β”œβ”€β”€ data/                     # Data loading & metadata extraction
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   └── metadata_extractor.py
β”‚   β”œβ”€β”€ detection/                # YOLOv8 plate detection
β”‚   β”‚   └── __init__.py
β”‚   β”œβ”€β”€ ocr/                      # Multi-engine OCR
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   └── alpr_engine.py
β”‚   β”œβ”€β”€ matching/                 # Fuzzy temporal matching
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   └── vehicle_matcher.py
β”‚   └── utils/                    # Utilities
β”‚       β”œβ”€β”€ __init__.py
β”‚       β”œβ”€β”€ config_loader.py
β”‚       β”œβ”€β”€ logger.py
β”‚       └── text_normalizer.py
β”œβ”€β”€ configs/                      # Configuration files
β”‚   └── config.yaml
β”œβ”€β”€ tests/                        # Unit tests
β”‚   β”œβ”€β”€ test_text_normalizer.py
β”‚   └── test_matching.py
β”œβ”€β”€ notebooks/                    # Jupyter notebooks (EDA, visualization)
β”œβ”€β”€ assets/                       # Diagrams, screenshots
β”œβ”€β”€ main.py                       # Main entry point
β”œβ”€β”€ requirements.txt              # Python dependencies
β”œβ”€β”€ .gitignore
β”œβ”€β”€ LICENSE
└── README.md

πŸ“Š Performance Metrics

Metric Value
Total Images 2,000
Bbox Extraction 99.85% (1,997/2,000)
OCR Success Rate 98.15% (1,960/1,997)
OCR Confidence (Avg) 87.8%
Matched Pairs 591
Match Rate 60.31% of vehicles
Avg Similarity 94.28%
Avg Time Diff 6.35 hours
Processing Speed ~115 images/sec (cropping)

OCR Breakdown

  • Fast-ALPR (Optimized): 96.67% (1,896 plates)
  • EasyOCR (Fallback): 3.27% (64 plates)

Matching Breakdown

  • Exact Matches: 368 pairs (100% similarity)
  • Fuzzy Matches: 223 pairs (75-99% similarity)

πŸ§ͺ Testing

Run unit tests:

pytest tests/ -v

Run with coverage:

pytest tests/ --cov=src --cov-report=html

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ† Acknowledgments

  • Fast-ALPR: Open-source ALPR library (GitHub)
  • Ultralytics YOLOv8: State-of-the-art object detection
  • EasyOCR: Robust general-purpose OCR engine

Built with ❀️ for computer vision and intelligent systems

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages