top of page

Real-Time Vehicle Detection and License Plate Recognition System

Updated: Oct 20, 2023



Introduction

License Plate Recognition is a sophisticated technology that automates the identification and reading of license plates on vehicles. It has a wide range of applications, including law enforcement, traffic management, parking management, and access control systems. LPR systems use a combination of computer vision, image processing, and pattern recognition techniques to extract textual information from license plates accurately and efficiently.


Let's delve into its various uses:

1. Traffic Monitoring and Law Enforcement:
  • Traffic Violations: The system can identify vehicles that break traffic rules, such as speeding, running red lights, or illegal parking, and then automatically issue fines based on the detected license plate.

  • Stolen Vehicle Recovery: Law enforcement agencies can use the system to detect and alert officers in real-time if a stolen vehicle (based on its license plate) is identified on roads or in parking lots.

2. Parking Management:
  • Automated Entry/Exit: In parking lots or garages, such systems can be used to automate entry and exit. Vehicles can be granted or denied access based on license plate recognition.

  • Parking Fee Calculation: For paid parking areas, the time a vehicle enters and exits can be logged based on license plate recognition, allowing for automated fee calculation and payment processing.

3. Toll Collection:
  • Automated Toll Payment: The system can be used on toll roads or bridges to automatically detect vehicles and charge tolls without requiring them to stop.

4. Security and Surveillance:
  • Restricted Area Access: In secure facilities, access can be granted or denied based on recognized license plates of authorized vehicles.

  • Monitoring and Logging: For areas where vehicle activity needs to be logged for security reasons, such as around governmental buildings, the system can continuously monitor and log all vehicle movements.

5. Commercial and Marketing Applications:
  • Customer Personalization: Businesses, like gas stations or drive-thrus, can recognize returning customers based on their vehicle's license plate and offer personalized services or promotions.

  • Market Research: Companies can use aggregated data from such systems to analyze patterns in vehicle movement, which can be valuable for market research.

6. Border Control:
  • Customs and Security: At national borders, the system can assist customs and security personnel by automatically logging vehicles and checking them against databases of interest.

7. Public Transportation:
  • Bus Lane Enforcement: In cities with dedicated bus lanes, the system can detect and fine private vehicles that wrongfully use these lanes.

8. Vehicle Management in Large Organizations:
  • Large institutions like universities, corporate campuses, or hospitals can manage vehicle access, assign parking, or monitor vehicle movements using license plate recognition.

9. Fleet Management:
  • Companies with large vehicle fleets can monitor and manage their vehicles more efficiently by tracking their real-time movements and ensuring that only authorized vehicles are in operation.

10. Insurance and Claim Verification:
  • Insurance companies can verify claims by cross-referencing vehicle movement data. For example, in the case of an accident claim at a specific location and time, the system can validate if the vehicle was indeed present there.


Implementation

class RealTimeInference:
    """
    A class encapsulating real-time inference and processing of video frames for vehicle detection and license plate recognition.

    Args:
        input_video_path (str): The path to the input video file.
        output_video_path (str): The path to save the output video file.
        results_csv_path (str): The path to save the results in a CSV file.
        coco_model_path (str): The path to the YOLO COCO model file.
        license_plate_model_path (str): The path to the license plate detection model file.
        vehicles (list of int): A list of class IDs corresponding to vehicles.

    Methods:
        draw_border(img, top_left, bottom_right, color=(0, 255, 0), thickness=10, line_length_x=200, line_length_y=200):
            Draws a border around a specified region of an image.

        write_csv(results, filename, timestamp):
            Writes data to a CSV file, including car IDs, license plate text, and timestamps.

        display_inference_realtime():
            Performs real-time inference on the input video, detects vehicles and license plates, and saves results.

        run():
            Initiates the real-time inference process by calling display_inference_realtime method.
    """
    pass
    
    def __init__(self, input_video_path, output_video_path, results_csv_path, coco_model_path,
                 license_plate_model_path, vehicles):
        # ... (constructor details)
        
    @staticmethod
    def draw_border(img, top_left, bottom_right, color=(0, 255, 0), thickness=10, line_length_x=200, line_length_y=200):
        """
        Draws a border around a specified region of an image.

        Args:
            img (numpy.ndarray): The input image.
            top_left (tuple of int): Coordinates (x1, y1) of the top-left corner of the region.
            bottom_right (tuple of int): Coordinates (x2, y2) of the bottom-right corner of the region.
            color (tuple of int, optional): The color of the border (B, G, R). Default is (0, 255, 0) for green.
            thickness (int, optional): The thickness of the border lines. Default is 10.
            line_length_x (int, optional): Length of horizontal lines in the border. Default is 200.
            line_length_y (int, optional): Length of vertical lines in the border. Default is 200.

        Returns:
            numpy.ndarray: The input image with the border drawn.
        """
        pass
        
    @staticmethod
    def write_csv(results, filename, timestamp):
        """
        Writes data to a CSV file, including car IDs, license plate text, and timestamps.

        Args:
            results (dict): A dictionary containing frame results with car IDs and license plate information.
            filename (str): The name of the CSV file to write data to.
            timestamp (datetime.datetime): The timestamp for the data entry.
        """
        pass

    def display_inference_realtime(self):
        """
        Performs real-time inference on the input video, detects vehicles and license plates, and saves results.
        """
        pass

    def run(self):
        """
        Initiates the real-time inference process by calling display_inference_realtime method.
        """
        pass

The above code introduces a class RealTimeInference that encapsulates functionalities required for performing real-time inference on video streams. Specifically, it deals with vehicle detection and license plate recognition. Let's break down the provided code in detail:

Class Overview:
  • RealTimeInference: This class represents a framework for real-time detection and inference on videos.

Attributes (arguments to the class):
  • input_video_path: The file path of the input video where detection and inference are to be performed.

  • output_video_path: The file path where the output video (with detections and annotations) will be saved.

  • results_csv_path: The file path where results (such as detected license plate numbers) are saved in a CSV format.

  • coco_model_path: The file path for the YOLO COCO model. The COCO dataset is widely used for object detection tasks, and YOLO is a popular object detection algorithm.

  • license_plate_model_path: The file path for the specific model designed to detect license plates.

  • vehicles: A list of class IDs that represent vehicles. This is used to filter out detections that are not vehicles.

Methods:

draw_border:

  • Purpose: This static method is used to draw a border around a specified region in an image. The region can represent an area of interest, like a detected vehicle or its license plate.

  • Parameters:

    • img: The actual image where the border is to be drawn.

    • top_left & bottom_right: Coordinates specifying the region of interest.

    • color, thickness, line_length_x, line_length_y: These are optional parameters allowing customization of the border's appearance.

  • Returns: The image with the drawn border.


write_csv:

  • Purpose: This static method writes detected information, like vehicle IDs and license plate text, into a CSV file, along with the associated timestamp.

  • Parameters:

    • results: A dictionary containing detection results.

    • filename: The name/path of the CSV file.

    • timestamp: The exact time when the detection happened.


display_inference_realtime:

  • Purpose: This method manages real-time inference on the input video, detects vehicles and their license plates, and saves the results (both visually in the video and in the CSV file).


run:

  • Purpose: This method serves as the primary driver function to initiate the entire process of real-time inference. It calls the display_inference_realtime method to start the detection and inference on the input video.

# Parameters
input_video_path = 'input_video_3.mp4'
output_video_path = 'output_video.mp4'
results_csv_path = 'results.csv'
coco_model_path = 'yolov8n.pt'
license_plate_model_path = # trained model
vehicles = [2, 3, 5, 7]

# Create an instance of the class and run the real-time inference
real_time_inference = RealTimeInference(input_video_path, output_video_path, results_csv_path, coco_model_path,
                                         license_plate_model_path, vehicles)
real_time_inference.run()
  • real_time_inference: Here, an instance of the RealTimeInference class is created. All the parameters defined above are passed to it. This object now represents a ready-to-use vehicle detection and license plate recognition system configured as per the provided parameters.

  • real_time_inference.run(): This line initiates the whole process. When the run() method is called, it starts the real-time inference on the input video using the configurations and models provided. It will detect vehicles, recognize license plates, annotate the detections on the video, and save the results in both the output video and the CSV file.

As we can see, we have successfully detected the car and its number plate.


We have provided only the code template. For a complete implementation, contact us.

If you require assistance with the implementation of the topic mentioned above, or if you need help with related projects, please don't hesitate to reach out to us.
4 views0 comments

Recent Posts

See All
bottom of page