AutoPlate Vision: AI-Powered Car Number Plate Detection


anamoly detection

AI Generated image: AutoPlate Vision: AI-Powered Car Number Plate Detection



This project explores the integration of computer vision, OCR, and deep learning to detect and recognize vehicle license plates using YOLOv11 and PaddleOCR, showcasing an end-to-end automated number plate recognition system.





Introduction

The Car Number Identification System is an advanced AI-powered solution designed to automatically detect and recognize vehicle license plates in real-time from street surveillance footage. This system is essential for traffic management, law enforcement, toll collection, and parking automation.

Using cutting-edge computer vision and deep learning methodologies, the system processes video feeds and extracts vehicle registration numbers with high accuracy. The following tools and frameworks will be used:

  • YOLO (You Only Look Once) – A state-of-the-art object detection model to identify vehicles and license plates in real-time.
  • Ultralytics YOLOv11 – An optimized version of YOLO for higher precision in number plate detection.
  • PaddleOCR & PaddlePaddle – Optical Character Recognition (OCR) frameworks to read text from license plates.
  • OpenCV with Python – Image processing techniques for refining and segmenting the license plate area.
  • MySQL Database Connector – For storing and managing detected plate numbers.
  • PyTorch (.pt file format) – For deep learning model deployment.
  • MP4 Video Processing – Demonstrations using real-world video footage to show system performance.

This system provides a fast, efficient, and scalable approach to vehicle identification for various industries.



1. What is YOLO Methodology?

YOLO (You Only Look Once) is an advanced real-time object detection algorithm designed for speed and accuracy. Unlike traditional methods, YOLO processes an entire image in a single pass, making it highly efficient for vehicle detection.

1.1 Why YOLO for Car Number Plate Detection?

  • Processes real-time video frames—ideal for highway monitoring.
  • Detects multiple objects in a single pass, including vehicles and number plates.
  • Ensures high accuracy in varying environments, such as low light or fast motion.

1.2 How YOLO Works?

k-means clustering

Image 1: oblassification, localization and detection.

Image classification assigns images to categories by identifying the main object, while object localization determines both the object's presence and location. Object detection goes further by identifying multiple objects in an image and marking them with bounding boxes. Real-time object detection, like in self-driving cars, detects vehicles, pedestrians, and traffic signs to enhance safety. 🚗📸


YOLO divides an image into a grid, predicting bounding boxes and class probabilities for each section.

k-means clustering

Image 2: Convolutional layer process.

The convolutional layer in a CNN applies a filter (kernel) to an input image, sliding across regions (receptive fields) with a defined stride. Each filter, matching the input depth, performs element-wise multiplications, summing the values into a feature map. This process, similar to cross-correlation, helps detect edges, blurs, or sharpens images. Larger filters capture broader patterns, while smaller ones focus on local details. Multiple layers work together to recognize complex features like a cat’s nose and ears. 🐱📸


1.2.1 Advancements in YOLO Versions

k-means clustering

Image 3: Yolo versions

  • YOLOv3 & YOLOv4: Improved feature extraction for better accuracy.
  • YOLOv5 & YOLOv6: Lightweight and optimized for deployment.
  • YOLOv7 & YOLOv8: High-resolution image detection.
  • YOLOv11: Latest version with advanced deep learning enhancements.
k-means clustering

Image 4: Efficiencies of Yolov11 comparing with other versions

1.2.2 Implementation in This Project


We use YOLOv11 with Ultralytics and OpenCV for real-time detection:


  • Training YOLO on a custom dataset of vehicle plates.
  • Using PaddleOCR for alphanumeric text recognition.
  • Integrating OpenCV & MySQL for data storage.


2. Use of YOLOv11 for Object Detection


Object Detection Examples


Serial No Description Example
1 Self-Driving Cars 🚗: Detects pedestrians, traffic lights, road signs, and other vehicles for safe navigation. Self Driving Car
Image: Self driving car Courtesy: caradas.com
2 Retail Checkout 🛒: Object detection helps in automated billing by recognizing products at self-checkout stations. Retail Checkout
Image: AI generated image, Self checkout counter
3 Medical Imaging 🏥: Detects tumors, fractures, and abnormalities in X-ray and MRI scans. Medical Imaging
Image: AI generated image, Medical imaging detecting tumor
4 Surveillance Systems 🎥: Identifies suspicious activities and unauthorized access in security footage. Surveillance
Image: AI generated surveillance and secuity
5 Wildlife Monitoring 🦌: Tracks animals in forests for conservation efforts and research. Wildlife Monitoring
Image: AI Generated wild life
6 Face Detection and Recognition 🧑: Unlocks smartphones, authenticates users, and detects emotions. Face Recognition
Image: Face recognition. Drawn by M. A. Hoque
7 Industrial Inspection 🏭: Detects manufacturing defects in products on assembly lines. Industrial Inspection
Image: Deep learning detects defects in the food industry. Courtesy mvtec.com
8 Agriculture 🌱: Identifies plant diseases, pests, and assesses crop health. Agriculture
Image control crop diseases with smart Agriculture Courtesy: https://geopard.tech/
9 Autonomous Drones 🚁: Used for mapping, delivery, and monitoring remote areas. Autonomous Drone
Dreama AI generated image mapping, delivery, and monitoring remote areas
10 Traffic Monitoring 🚦: Tracks congestion, violations, and number plate recognition. Traffic Monitoring
Image: Automatic number plate recognition (ANPR) Courtesy: allgovision.com

Summary


YOLO has transformed real-time object detection, making it an ideal choice for **automatic vehicle number recognition** in dynamic environments.



3. OpenCV Python for Image Processing

OpenCV (Open Source Computer Vision Library) is a powerful open-source library for computer vision and image processing. It is widely used in various domains such as robotics, artificial intelligence, medical imaging, and object detection.

3.1 Key Features of OpenCV for Image Processing:

  • Image Manipulation: Resize, crop, rotate, and filter images.
  • Edge Detection: Identify object boundaries using techniques like Canny edge detection.
  • Color Space Conversion: Convert between color models like RGB, HSV, and Grayscale.
  • Feature Detection: Detect key points using algorithms like SIFT, ORB, and FAST.
  • Object Detection: Use pre-trained models for face detection, pedestrian detection, etc.
  • Video Processing: Read, write, and manipulate video streams in real-time.

3.2 Example: Loading and Displaying an Image using OpenCV in Python

Install openCV


Run in command prompt:


    
        pip install opencv-python

        import cv2
image = cv2.imread('car.jpg') # Load an image gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Convert to grayscale cv2.imshow('Original Image', image) # Display the images cv2.imshow('Grayscale Image', gray_image) cv2.waitKey(0) # Wait for a key press and close windows
cv2.destroyAllWindows()

3.3 Why Use OpenCV for Image Processing?

  • Fast and Efficient: Optimized for real-time applications.
  • Cross-Platform: Works on Windows, Linux, macOS, and mobile platforms.
  • Integration with AI: Can be combined with deep learning frameworks like TensorFlow and PyTorch.
  • Large Community Support: Extensive documentation and community-driven development.


4. 🚀 Ultralytics in License Plate Detection

4.1 Introduction

License Plate Detection (LPD) is a crucial technology in smart surveillance and traffic management. Using Ultralytics YOLO, an advanced object detection framework, enhances speed, accuracy, and efficiency in real-time license plate recognition.

🛠️4.2 Why Use Ultralytics for License Plate Detection?

  • Real-Time Performance – Fast detection for instant vehicle tracking.
  • High Accuracy – Detects plates in low-light or high-speed conditions.
  • Scalability – Works on edge devices, cloud, or embedded systems.
  • Pre-Trained Models – Reduces development time with fine-tuned detection models.

🔍4.3 How Ultralytics YOLO Works for License Plate Detection

  1. Image Acquisition: Captures frames from CCTV, drones, or dashcams.
  2. Preprocessing: Enhances image quality for better recognition.
  3. YOLO Model Inference: Detects and extracts plate regions.
  4. OCR Integration: Reads and interprets plate numbers.
  5. Data Logging & Analytics: Stores records for security or law enforcement.

Flow Diagram


PaddlePaddle YOLO Workflow

Image: Number plate detection flow diagram curtesy researchgate.net

Real Detection Example

Automatic number plate recognition (ANPR)

Image: Automatic number plate recognition (ANPR) Courtesy: ultralytics

🚦4.4 Applications of Ultralytics in License Plate Recognition

  • 🔹 Traffic Monitoring: Tracks vehicle movement for congestion analysis.
  • 🔹 Automated Toll Collection: Enables cashless toll payments.
  • 🔹 Parking Management: Automates entry/exit systems.
  • 🔹 Law Enforcement: Identifies stolen or suspicious vehicles.

💡4.5 Implementation: Running Ultralytics YOLO for LPD

Below is a Python implementation of Ultralytics YOLO for License Plate Detection:

    
      pip install ultralytics

      from ultralytics import YOLO
      
      # Load YOLO model pre-trained on license plates
      model = YOLO("yolov8n.pt")
      
      # Perform detection on an image
      results = model("car_with_plate.jpg", conf=0.5)
      
      # Display results
      results.show()
    
  

📂4.6 Download Pretrained Models & Datasets

To get started, download the necessary models and datasets:


Download YOLOv8 License Plate Datasets

🔥4.7 Why Ultralytics Stands Out?

  • 🔹 Lightweight Yet Powerful: Works on low-power devices.
  • 🔹 Open-Source & Extensible: Customizable for different regions.
  • 🔹 Seamless Deployment: Integrates with Flask, FastAPI, or cloud services.

🌍 Summary

Ultralytics YOLO revolutionizes License Plate Detection by offering real-time, high-accuracy solutions for traffic systems, security monitoring, and automation. Whether for law enforcement, toll collection, or smart parking, it ensures precision and efficiency in plate recognition.



5. Using PaddlePaddle in YOLO

5.1 Installation of PaddlePaddle

To install PaddlePaddle on your system, use the following command:


  pip install paddlepaddle

For GPU acceleration, install the GPU version:


  pip install paddlepaddle-gpu

5.2 What is PaddlePaddle?

PaddlePaddle (Parallel Distributed Deep Learning) is an open-source deep learning framework developed by Baidu. It is used for **computer vision, natural language processing (NLP), and speech recognition**.

5.3 How PaddlePaddle Achieves Superior Speed

PaddlePaddle achieves superior benchmark speed using techniques as:

  1. Data Parallelism
  2. Model Parallelism
  3. TensorRTs
  4. Supported Models

5.3.1 Data Parallelism


Data parallelism in deep learning involves splitting the training data across multiple devices or machines, enabling parallel computation and faster training by independently processing subsets of data and aggregating gradients for updating shared model parameters.

PaddlePaddle YOLO Workflow
Image: data parallelism, three GPUs. Courtesy https://learnopencv.com/

5.3.2 Model Parallelism


Model parallelism in deep learning involves dividing a single model across multiple devices or machines, enabling parallel computation by splitting different parts of the model across devices to handle larger models or memory constraints efficiently.

PaddlePaddle YOLO Workflow
Image: Model Parallelism across two different GPUs. Courtesy https://learnopencv.com/

5.3.3 TensorRT


TensorRT is a high-performance deep learning inference optimizer and runtime library developed by NVIDIA. It is helpful for deep learning as it optimizes and accelerates inference tasks by optimizing network architectures, reducing precision, layer fusion, and leveraging GPU-specific optimizations, resulting in faster and more efficient execution of deep learning models on NVIDIA GPUs.

TensorRT
Image: TensorRT different processes involved. Courtesy https://learnopencv.com/

5.3.4 Supported Models


  • More than 400 models are available on PaddlePaddle from different fields of AI.
  • PaddlePaddle has started integrating its libraries with Huggingface; however, as of January 2023, 75 of the 400+ models have been implemented.
  • Thus let us use models available directly from PaddlePaddle

5.4 How PaddlePaddle Works with YOLO?

  • Model Training: Provides pre-trained YOLO models for custom datasets.
  • Optimized Inference: Uses Paddle Lite & Paddle Inference for deployment.
  • PaddleOCR Integration: Extracts text from license plates after YOLO detects them.
  • Flexible APIs: Supports Python & C++ APIs for real-time object detection.

5.5 Example Use Case: License Plate Recognition

YOLO detects vehicles and localizes license plates → PaddleOCR extracts text → Paddle Serving processes in real-time.

PaddlePaddle YOLO Workflow
Image: PaddlePaddle image procssing. Courtesy https://learnopencv.com/

5.6 Why Use PaddlePaddle for YOLO?

  • ✅ Optimized Training: Distributed computing for faster training
  • ✅ Real-Time Deployment: Supports **CPU, GPU, and edge AI accelerators**
  • ✅ OCR Integration: Works with **PaddleOCR** for automatic text extraction
  • ✅ Edge AI Ready: Runs on **Raspberry Pi, Jetson Nano, and cloud servers**


6. PaddleOCR for Number Recognition

🚀6.1 PaddleOCR: An Overview

6.1.1 What is PaddleOCR?


PaddleOCR is an advanced Optical Character Recognition (OCR) tool developed by PaddlePaddle, an open-source deep learning framework by Baidu. It is widely used for extracting text from images and scanned documents, supporting multiple languages, handwritten text, and complex layouts.

6.2 Why is PaddleOCR Important?

  • High Accuracy: Uses deep learning models for text detection and recognition.
  • Multilingual Support: Recognizes text in over 80+ languages.
  • Fast & Lightweight: Optimized for CPU and GPU performance.
  • Flexible & Scalable: Ideal for license plate recognition, document scanning, ID verification, etc.
  • End-to-End Pipeline: Handles text detection, recognition, and post-processing.

6.3 How PaddleOCR Works for Number Recognition?

  1. Image Preprocessing: Convert image to grayscale, enhance contrast, and remove noise.
  2. Text Detection: Uses DBNet or EAST to detect text regions.
  3. Character Recognition: Extracts and recognizes numbers using CRNN (Convolutional Recurrent Neural Network).
  4. Post-Processing: Corrects errors and structures extracted data.

6.4 Installing PaddleOCR

To install PaddleOCR, follow these steps:

Step 1: Install PaddlePaddle

    
      pip install paddlepaddle
    
  

Step 2: Install PaddleOCR

    
      pip install paddleocr
    
  

Step 3: Verify Installation

    
      from paddleocr import PaddleOCR
      ocr = PaddleOCR()
      result = ocr.ocr('car_plate.jpg', cls=True)
      print(result)
    
  

6.5 Examples of PaddleOCR in Action

Serial No Application Example
1️⃣ License Plate Recognition 🚗 - Extracts car plate numbers from images/videos. License Plate Recognition
2️⃣ ID Card Scanning 🆔 - Recognizes names, dates, and ID numbers. ID Card Scanning
3️⃣ Bank Cheque Processing 🏦 - Extracts handwritten amounts for banking automation. Bank Cheque Processing
4️⃣ Medical Reports 🏥 - Digitizes doctor prescriptions for patient records. Medical Reports
5️⃣ Invoice Data Extraction 🧾 - Automates receipt scanning for businesses. Invoice Data Extraction

Summary

PaddleOCR is a powerful AI-based text recognition tool that enhances automation in license plate recognition, document scanning, and digital transformation. It is fast, accurate, and easy to integrate into various AI applications.



7. Connecting to MySQL Database


7.1 Connecting to MySQL for Automatic License Plate Recognition

Flow Diagram


Number Plate Detection Process Flow

Image: Number plate detection flow diagram. Drawn by Hoque

Real Detection Example

Real Image Detecting Number Plate

Image: Automatic number plate recognition (ANPR)

7.2 Overview

This system automatically detects vehicles, extracts license plate numbers, estimates speed, and saves data in a MySQL database.

7.3 Creating the MySQL Database

    
        CREATE DATABASE vehicle_db;
        USE vehicle_db;
        CREATE TABLE vehicles (
          id INT AUTO_INCREMENT PRIMARY KEY,
          date DATE NOT NULL,
          time TIME NOT NULL,
          track_id INT NOT NULL,
          plate_number VARCHAR(20) NOT NULL,
          vehicle_class VARCHAR(50),
          speed FLOAT NOT NULL,
          location VARCHAR(100),
          entry_timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
        );
    
  

7.4 Installing Dependencies

    
      pip install mysql-connector-python
    
  

7.4.1 Python Code for MySQL Connection

    
      import mysql.connector
      from datetime import datetime

      def connect_to_db():
        try:
            connection = mysql.connector.connect(
                host="localhost",
                user="root",
                password="yourpassword",
                database="vehicle_db"
            )
            return connection
        except mysql.connector.Error as e:
            print("Error connecting to MySQL:", e)
            return None
    
  

7.4.2 Saving Data to MySQL

    
      def save_to_database(date, time, track_id, plate_number, vehicle_class, speed, location):
        connection = connect_to_db()
        if connection:
            cursor = connection.cursor()
            sql_query = """
                INSERT INTO vehicles (date, time, track_id, plate_number, vehicle_class, speed, location)
                VALUES (%s, %s, %s, %s, %s, %s, %s)
            """
            values = (date, time, track_id, plate_number, vehicle_class, speed, location)
            cursor.execute(sql_query, values)
            connection.commit()
            print("Data inserted successfully")
            cursor.close()
            connection.close()
    
  

7.4.3 Example Usage

    
      from datetime import datetime
      save_to_database(
        date=datetime.today().strftime('%Y-%m-%d'),
        time=datetime.now().strftime('%H:%M:%S'),
        track_id=101,
        plate_number="ABC-1234",
        vehicle_class="Sedan",
        speed=65.2,
        location="Highway 401"
      )
    
  

7.5 Flow Diagram (Recommended)

Consider adding a flowchart to visualize the process:

  • 📷 Camera Captures Video
  • 🛑 YOLO Detects Vehicles
  • 🔠 OCR Extracts License Plate Number
  • 🚗 Speed is Estimated
  • 📡 Data Sent to MySQL Database
  • 💾 Stored for Analysis & Reporting


8. Database Structure & MySQL Tables

8.1 Database Structure & MySQL Tables

To store and manage the detected license plate information along with speed and other details, we will use a MySQL database named numberplates_speed.


8.1.2 Database Name: numberplates_speed

This database is designed to store real-time vehicle detection and tracking data.


8.1.3 Table Name: my_data

This table will store vehicle detection data, including license plate numbers, timestamps, vehicle speed, and classification.


8.1.4 Table Schema

    
      CREATE DATABASE numberplates_speed;

      USE numberplates_speed;

      CREATE TABLE my_data (
          id INT AUTO_INCREMENT PRIMARY KEY,
          date DATE NOT NULL,
          time TIME NOT NULL,
          track_id INT NOT NULL,
          class_name VARCHAR(255) NOT NULL,
          speed FLOAT NULL,
          numberplate TEXT NULL
      );

      #NOTE: Sometime, the system may not be able to retrieve registration numberplate 
      due to poor visibility, physical obstructions by other vehicles or some other reasons. Similarly 
      conditions may apply to speed also. Therefore, these fields are allowed to cantain null values.
    
  

8.1.5 Explanation of Table Columns

Column Name Data Type Description
id INT AUTO_INCREMENT PRIMARY KEY Unique identifier for each record.
date DATE Date when the vehicle was detected.
time TIME Time of detection.
track_id INT A unique ID assigned to the detected vehicle for tracking.
class_name VARCHAR(255) Type of vehicle (e.g., Car, Truck, Motorcycle, Bus).
speed FLOAT Speed of the vehicle at the time of detection.
numberplate TEXT Recognized license plate text from OCR processing.

8.2 How It Works Automatically

  • License Plate Detection: The system captures images/videos of moving vehicles using a camera. The YOLO object detection model identifies vehicles and their license plates.
  • OCR Processing: The detected license plate region is extracted and converted to text using PaddleOCR.
  • Speed Estimation: The system calculates the vehicle’s speed based on timestamps and frame analysis.
  • Data Storage in MySQL: The extracted information is automatically inserted into the my_data table in the numberplates_speed database.

8.3 Example: Inserting Data into the Table

    
      INSERT INTO my_data (date, time, track_id, class_name, speed, numberplate)
      VALUES ('2025-02-26', '14:30:00', 101, 'Car', 72.5, 'ABC-1234');
    
  

8.4 Retrieving Data from the Table

    
      SELECT * FROM my_data ORDER BY id DESC LIMIT 10;
    
  

9. Python Code Implementation

9.1 Python Implementation Code for Vehicle License Plate Detection

Step 1: Install Python libraries

Before running the program, install the necessary Python libraries using the command prompt or Anaconda prompt:

    
      pip install opencv-python
      pip install ultralytics
      pip install paddlepaddle
      pip install paddleocr
      pip install mysql-connector-python
    
  

Step 2: Organize Project Files

Store the following files in the same directory, for example, "D:\YOLOV11":


Step 3: Run the Detection Program

Execute the following command in Windows Command Prompt, Anaconda Prompt, or Shell:

    
      python main.py --model yolo11n.onnx --source car.mp4
    
  

9.2 Code Breakdown (main.py)


      import cv2
      from time import time
      import numpy as np
      from ultralytics.solutions.solutions import BaseSolution
      from ultralytics.utils.plotting import Annotator, colors
      from datetime import datetime
      import mysql.connector
      from paddleocr import PaddleOCR


      class SpeedEstimator(BaseSolution):
        def __init__(self, **kwargs):
            super().__init__(**kwargs)
            self.initialize_region()  # Initialize speed region
            self.spd = {}  # Dictionary to store speed data
            self.trkd_ids = []  # List for already tracked and speed-estimated IDs
            self.trk_pt = {}  # Dictionary for previous timestamps
            self.trk_pp = {}  # Dictionary for previous positions
            self.logged_ids = set()  # Set to keep track of already logged IDs

            # Initialize the OCR system
            self.ocr = PaddleOCR(use_angle_cls=True, lang='en')

            # MySQL database connection
            self.db_connection = self.connect_to_db()

        def connect_to_db(self):
            """Establish connection to MySQL database and create database/table if not exists."""
            try:
                # Connect to MySQL server
                connection = mysql.connector.connect(
                    host="localhost",
                    user="root",  # Replace with your MySQL username
                    password=""   # Replace with your MySQL password
                )
                cursor = connection.cursor()

                # Create database if it doesn't exist --> DB NAME IS: numberplates_speed
                cursor.execute("CREATE DATABASE IF NOT EXISTS numberplates_speed")       
                print("Database 'numberplates_speed' checked/created.")

                # Connect to the newly created or existing database
                connection.database = "numberplates_speed"

                # Create table if it doesn't exist TABLE NAME: my_data
                create_table_query = """
                CREATE TABLE IF NOT EXISTS my_data (
                    id INT AUTO_INCREMENT PRIMARY KEY,
                    date DATE,
                    time TIME,
                    track_id INT,
                    class_name VARCHAR(255),
                    speed FLOAT,
                    numberplate TEXT
                )
                """
                cursor.execute(create_table_query)
                print("Table 'my_data' checked/created.")

                return connection
            except mysql.connector.Error as err:
                print(f"Error connecting to database: {err}")
                raise

        def perform_ocr(self, image_array):
            """Performs OCR on the given image and returns the extracted text."""
            if image_array is None:
                raise ValueError("Image is None")
            if isinstance(image_array, np.ndarray):
                results = self.ocr.ocr(image_array, rec=True)
            else:
                raise TypeError("Input image is not a valid numpy array")
            return ' '.join([result[1][0] for result in results[0]] if results[0] else "")

        def save_to_database(self, date, time, track_id, class_name, speed, numberplate):
            """Save data to the MySQL database."""
            try:
                cursor = self.db_connection.cursor()
                query = """
                    INSERT INTO my_data (date, time, track_id, class_name, speed, numberplate)
                    VALUES (%s, %s, %s, %s, %s, %s)
                """
                cursor.execute(query, (date, time, track_id, class_name, speed, numberplate))
                self.db_connection.commit()
                print(f"Data saved to database: {date}, {time}, {track_id}, {class_name}, {speed}, {numberplate}")
            except mysql.connector.Error as err:
                print(f"Error saving to database: {err}")
                raise

        def estimate_speed(self, im0):
            """Estimate speed of objects and track them."""
            self.annotator = Annotator(im0, line_width=self.line_width)  # Initialize annotator
            self.extract_tracks(im0)  # Extract tracks
            self.annotator.draw_region(
                reg_pts=self.region, color=(104, 0, 123), thickness=self.line_width
            )   #DRAW REGION. BETTER KEEP IT COMMENTED DURING RUNNING FOR BETTER READ
            # Get current date and time
            current_time = datetime.now()

            for box, track_id, cls in zip(self.boxes, self.track_ids, self.clss):
                self.store_tracking_history(track_id, box)  # Store track history

                if track_id not in self.trk_pt:
                    self.trk_pt[track_id] = 0
                if track_id not in self.trk_pp:
                    self.trk_pp[track_id] = self.track_line[-1]

                speed_label = f"{int(self.spd[track_id])} km/h" if track_id in self.spd else self.names[int(cls)]

                # Draw the bounding box and track ID on it
                label = f"ID: {track_id} {speed_label}"  # Show track ID along with speed
                self.annotator.box_label(box, label=label, color=colors(track_id, True))  # Draw bounding box

                # Speed and direction calculation
                if self.LineString([self.trk_pp[track_id], self.track_line[-1]]).intersects(self.r_s):
                    direction = "known"
                else:
                    direction = "unknown"

                # Calculate speed if the direction is known and the object is new
                if direction == "known" and track_id not in self.trkd_ids:
                    self.trkd_ids.append(track_id)
                    time_difference = time() - self.trk_pt[track_id]
                    if time_difference > 0:
                        speed = np.abs(self.track_line[-1][1].item() - self.trk_pp[track_id][1].item()) / time_difference
                        self.spd[track_id] = round(speed)

                # Update the previous tracking time and position
                self.trk_pt[track_id] = time()
                self.trk_pp[track_id] = self.track_line[-1]
                x1, y1, x2, y2 = map(int, box)  # Convert box coordinates to integers
                cropped_image = np.array(im0)[y1:y2, x1:x2]
                ocr_text = self.perform_ocr(cropped_image)

                # Get the class name and speed
                class_name = self.names[int(cls)]
                speed = self.spd.get(track_id)

                # Ensure OCR text is not empty and save OCR text with the relevant details if not already logged
                if track_id not in self.logged_ids and ocr_text.strip() and speed is not None:
                    self.save_to_database(
                        current_time.strftime("%Y-%m-%d"),
                        current_time.strftime("%H:%M:%S"),
                        track_id,
                        class_name,
                        speed if speed is not None else 0.0,
                        ocr_text
                    )
                    self.logged_ids.add(track_id)

            self.display_output(im0)  # Display output with base class function
            return im0

        #FUCTION TO GET MOUSE POINTS:
        def RGB(self, event, x, y, flags, param):
            if event ==cv2.EVENT_MOUSEMOVE: #CHECK FOR MOUSE MOVEMENT
                point = [x, y]
                point(f"Mouse moved to: {point}")
                

      # Open the video file
      cap = cv2.VideoCapture('tc.mp4')

      # Define region points for counting
      region_points = [(0, 145), (1018, 145)]

      # Initialize the object counter
      speed_obj = SpeedEstimator(
        region=region_points,
        model=r"D:\\YOLOV11\video\best.pt",  # Replace with your YOLO model file
        line_width=2
      )

      count = 0

      #SETUP MOUSE CALL BACK TO GET MOUSE POINTS
      cv2.namedWindow("RGB")
      cv2.setMouseCallback("RGB", speed_obj.RGB)

      while True:
        # Read a frame from the video
        ret, frame = cap.read()
        if not ret:
            break

        count += 1
        if count % 3 != 0:  # Skip odd frames
            continue

        frame = cv2.resize(frame, (1020, 500))

        # Process the frame with the object counter
        result = speed_obj.estimate_speed(frame)

        # Show the frame
        cv2.imshow("RGB", result)
        if cv2.waitKey(1) & 0xFF == ord("q"):  # Press 'q' to quit
            break

      # Release the video capture object and close the display window
      cap.release()
      cv2.destroyAllWindows()



Code Explanation main.py:


1. Importing Required Libraries: Loads essential libraries for computer vision, OCR, and database connectivity.

2. Speed Estimation Class (SpeedEstimator): The class processes video frames, detects vehicles, extracts license plate information, and stores results in a database.



3. Opening the Video File: Loads the car.mp4 video and processes each frame for vehicle detection.


4. Defining Region Points: Specifies the area in the video where detection and OCR operations will be performed.


5. Initializing the Object Counter: Loads the pre-trained YOLO model to detect vehicles and track their movement.


6. Mouse Callback for Interactive Selection: Allows users to click on the video frame to define specific detection areas.


By following these steps, you can efficiently implement real-time vehicle speed estimation and license plate recognition using YOLO, PaddleOCR, and MySQL. 🚗💡



10. Using PyTorch (.pt files) for Deep Learning

10.1 What is PyTorch and .pt Files?

PyTorch is an open-source deep learning framework developed by Facebook AI. It is widely used for machine learning and deep learning tasks, including image recognition, NLP, and reinforcement learning.

A .pt file is a PyTorch model file that stores trained model parameters. These files are used to save and load deep learning models efficiently, enabling model deployment without retraining.

10.2 Why is PyTorch (.pt Files) Important?

  • Efficient Model Storage: `.pt` files store trained model weights, reducing computational overhead.
  • Easy Deployment: Pre-trained models can be loaded and used without re-training.
  • Flexible: Works with both CPU and GPU acceleration, making it ideal for deep learning applications.
  • Supports Transfer Learning: You can fine-tune pre-trained models for specific tasks like license plate recognition.

10.3 How PyTorch (.pt Files) Works in License Plate Recognition?

  1. Train a YOLO or CNN Model: A PyTorch-based deep learning model (YOLO, CNN, or a custom network) is trained using images of license plates.
  2. Save Model Weights as .pt File: After training, model weights are stored in a .pt file.
  3. Load Pre-trained Model: During plate recognition, the `.pt` file is loaded into PyTorch for inference.
  4. Detect and Recognize Plate Numbers: The model processes input images, detects plates, and extracts the text using OCR tools like PaddleOCR.
  5. Post-processing: The detected plate numbers are cleaned and formatted for display or database storage.

10.4 How to Install PyTorch?

10.4.1 Using pip (Recommended)

    
      pip install torch torchvision torchaudio
    
  

10.4.2 For CUDA (GPU Acceleration)

    
      pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
    
  

10.5 Example: Using PyTorch (.pt) File for License Plate Recognition

1️⃣ Load a Pre-trained Model

    
      import torch
      import cv2
      import numpy as np

      # Load the trained PyTorch model (.pt file)
      model = torch.hub.load('ultralytics/yolov5', 'custom', path='plate_detector.pt')

      # Set the model to evaluation mode
      model.eval()
    
  

2️⃣ Run Plate Detection on an Image

    
        # Load image
        image_path = 'car_plate.jpg'
        image = cv2.imread(image_path)

        # Convert image to RGB (PyTorch models usually expect RGB)
        image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

        # Perform inference
        results = model(image_rgb)

        # Show results
        results.show()
    
  

3️⃣ Extract Plate Numbers Using PaddleOCR

    
        from paddleocr import PaddleOCR
        ocr = PaddleOCR()
        plate_text = ocr.ocr(image_path)
        
        print("Detected Plate Number:", plate_text)
    
  

Summary

Using PyTorch (.pt) files enables efficient license plate recognition by allowing quick model loading and inference. Combining PyTorch with YOLO, OpenCV, and PaddleOCR makes license plate detection highly accurate and deployable in real-world applications. 🚗🔍



11. MP4 Video Demonstrations

11.1 MP4 Video Demonstrations for Automatic License Plate Recognition

In this section, we explain how to run a video demonstration where the YOLO-based system detects vehicle types, speed, and license plates automatically.


1️⃣ Running the Video and Starting Automatic Recognition

To execute the program and process an MP4 video file, follow these steps:


  1. Ensure that the main.py file and the pretrained YOLO model (.onnx or .pt) are stored in the same directory as the video file.
  2. Open Windows Command Prompt or Anaconda Command Prompt and navigate to the project folder: cd D:\YOLOV11
  3. Run the following command to start processing the video and detect vehicles and their details:

python main.py --model yolo11n.onnx --source car.mp4 # use yolo11n.onnx for normal detection python main.py --model best.pt --source car.mp4 # when using best.pt for numberplate detection only python main.py # When best.pt and car.mp4 are referenced from inside main.py File

Storing data into my_data table:

After opening MySql database table, detection information are stored as:

TensorRT
Image: Records in MySql database table


2️⃣ How YOLO Recognizes Vehicles, Speed, and Number Plates


The system uses YOLO (You Only Look Once) for object detection and PaddleOCR for text recognition. The process follows these steps:

  • Frame Processing: The system extracts frames from the video and processes them for object detection.
  • Vehicle Recognition: The YOLO model detects different vehicle types such as Car, Truck, Bus, Motorcycle.
  • Speed Estimation: The system tracks vehicle movement across frames and estimates speed.
  • License Plate Detection & OCR: The license plate region is extracted and converted into readable text using PaddleOCR.
  • Database Entry: Extracted details (date, time, vehicle type, speed, number plate) are stored in a MySQL database.

3️⃣ Example of Output in Command Prompt


        
          [INFO] Loading YOLO model from yolo11n.onnx...
          [INFO] Processing video file: car.mp4
          [INFO] Detected Vehicle: Car | ID: 101 | Speed: 72.5 km/h | Plate: ABC-1234
          [INFO] Detected Vehicle: Truck | ID: 102 | Speed: 55.0 km/h | Plate: XYZ-5678
          [INFO] Data saved to MySQL database successfully.
        
      

4️⃣ Running the Program with a Live Camera


Instead of using an MP4 video file, you can process a live camera feed by running:

python main.py --model yolo11n.onnx --source 0

Note: Here, 0 represents the default webcam connected to your system.


5️⃣ Summary of the Process


  • Runs an MP4 video or live camera feed
  • Detects different vehicle types using YOLO
  • Estimates speed based on frame timestamps
  • Extracts license plate text using PaddleOCR
  • Stores data in a MySQL database

12. What is the 'best.pt' File and How is it Generated?

The best.pt file is a pre-trained model checkpoint created during the training process of a YOLO model using Ultralytics YOLO (e.g., YOLOv5, YOLOv8). It contains the optimized model weights and is automatically generated based on performance metrics.

How is best.pt Generated?


When training a YOLO model on a custom dataset, the framework automatically saves model checkpoints as follows:

  • During training, YOLO periodically saves intermediate model checkpoints as epoch.pt files.
  • After training is completed, YOLO saves two key weight files:
    1. last.pt → The final model weights from the last training epoch.
    2. best.pt → The best-performing model, determined based on validation metrics such as mean Average Precision (mAP) and loss values.

Example YOLO Training Command:

    
      yolo task=detect mode=train model=yolov8n.pt data=your_dataset.yaml epochs=50
    
  

After completing the training process, YOLO will store both best.pt and last.pt in the following directory:

📂 runs/train/exp/weights/


The best.pt file can then be used for inference to detect objects in images and videos with the highest accuracy. 🚀



13. Conclusion


This project successfully implements an Automatic License Plate Recognition (ALPR) system using YOLO v11 to detect and recognize vehicle license plates in real-time. From the initial setup to final execution, the system follows a structured pipeline:


1. Installing Dependencies

We installed essential Python libraries such as OpenCV, Ultralytics, PaddlePaddle, PaddleOCR, and MySQL Connector to facilitate image processing, YOLO-based detection, OCR, and database storage.


2. Data Processing & Model Integration

The system processes input video feeds and leverages YOLO v11 to detect vehicles and their license plates. The detected plates are then processed using PaddleOCR for text recognition.


3. Database Storage & Analytics

Recognized license plates, along with additional data such as vehicle speed and classification, are stored in a MySQL database, allowing structured retrieval and analysis.


4. Live Demonstration & Results

The system can be tested using a video feed (MP4), with command-line execution ensuring automatic recognition, tracking, and storage of results.


5. Scalability & Future Enhancements

This project provides a scalable framework that can be extended to real-time traffic monitoring, law enforcement applications, and smart city infrastructure. Future enhancements could include:


  • Deep learning improvements for higher accuracy
  • Cloud-based processing for large-scale deployment
  • AI-powered analytics for better performance

By leveraging YOLO v11 and OCR technologies, this project demonstrates an efficient and automated license plate recognition system, offering practical applications in vehicle tracking, security, and transportation management. 🚀



14. References





Contact Me

hoque@HoqueAi.com

+1 473 607 1949

Download Resume