License Plate Recognition System

 License Plate Recognition System 

1. Introduction

The License Plate Recognition System is a computer vision project designed to detect and read vehicle license plates from images or video feeds. This application leverages OpenCV for image processing and Tesseract OCR for text extraction.

2. Prerequisites

• Python: Install Python 3.x from the official Python website.
• Required Libraries:
  - opencv-python: Install using pip install opencv-python
  - pytesseract: Install using pip install pytesseract
  - numpy: Install using pip install numpy
• Tesseract OCR: Download and install Tesseract OCR from the official website and configure it in the system path.

3. Project Setup

1. Create a Project Directory:

- Name your project folder, e.g., `LicensePlateRecognition`.
- Inside this folder, create the Python script file (`license_plate_recognition.py`).

2. Install Required Libraries:

Ensure OpenCV, pytesseract, and numpy are installed using `pip`.

3. Configure Tesseract:

Install Tesseract and note the installation path for configuration in the script.

4. Writing the Code

Below is the Python code for the License Plate Recognition System:


import cv2
import pytesseract
import numpy as np

# Configure Tesseract path
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'

# Function to preprocess the image
def preprocess_image(image_path):
    image = cv2.imread(image_path)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    blurred = cv2.GaussianBlur(gray, (5, 5), 0)
    edged = cv2.Canny(blurred, 100, 200)
    return image, edged

# Function to detect and extract license plate
def detect_license_plate(image, edged):
    contours, _ = cv2.findContours(edged, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    for contour in contours:
        approx = cv2.approxPolyDP(contour, 0.02 * cv2.arcLength(contour, True), True)
        if len(approx) == 4:  # Look for rectangular contours
            x, y, w, h = cv2.boundingRect(approx)
            plate = image[y:y+h, x:x+w]
            return plate
    return None

# Function to read text from the license plate
def read_license_plate_text(plate_image):
    plate_text = pytesseract.image_to_string(plate_image, config='--psm 8')
    return plate_text.strip()

# Main function to process an image
def recognize_license_plate(image_path):
    image, edged = preprocess_image(image_path)
    plate = detect_license_plate(image, edged)
    if plate is not None:
        plate_text = read_license_plate_text(plate)
        print("Detected License Plate Text:", plate_text)
        cv2.imshow("License Plate", plate)
    else:
        print("No license plate detected.")
    cv2.imshow("Input Image", image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

# Example usage
if __name__ == "__main__":
    image_path = 'path_to_license_plate_image.jpg'  # Replace with your image path
    recognize_license_plate(image_path)
   

5. Key Components

• Image Preprocessing: Converts the input image to grayscale and applies edge detection for contour extraction.
• Contour Detection: Detects rectangular contours to locate potential license plates.
• Text Extraction: Uses Tesseract OCR to read text from the detected license plate.

6. Testing

1. Use a sample image containing a vehicle license plate.

2. Run the script:

   python license_plate_recognition.py

3. Verify the detected license plate text and its visualization.

7. Enhancements

• Real-Time Detection: Extend the script to process live video feeds.
• Improved OCR Accuracy: Experiment with different OCR configurations and preprocessing techniques.
• Multiple Plate Detection: Enhance the system to detect and read multiple plates in a single image.

8. Troubleshooting

• Detection Failures: Adjust edge detection parameters or contour approximation thresholds.
• OCR Errors: Ensure proper preprocessing and experiment with different Tesseract configurations.
• Performance Issues: Optimize image processing steps for real-time applications.

9. Conclusion

This project implements a basic License Plate Recognition System using OpenCV and Tesseract OCR. With additional enhancements, it can be utilized for traffic monitoring, parking systems, and toll collection.