Python OpenCV Detect and Recognize Car License Plate

FREE Online Courses: Your Passport to Excellence - Start Now

Car plate detection and recognition using OpenCV is a crucial task in applications like traffic monitoring, toll collection systems, parking management, and law enforcement. OpenCV provides many functions and algorithms for computer vision tasks. This aims to use OpenCV to detect car plates in images or videos and then recognize the alphanumeric characters on those plates.

By combining techniques like image processing, character segmentation, and optical character recognition (OCR), we can create a reliable system that improves efficiency, enhances security, and simplifies processes in different real-world situations.

What is the role of Harcascade?

The haarcascade_russian_plate_number.xml file is a trained Haar cascade classifier designed to detect Russian license plate patterns. It is used in car plate detection using OpenCV. The classifier analyzes predefined features and distinguishes between positive and negative instances of Russian license plates. By loading and applying this classifier to images or video frames, the system can identify potential car plate regions called ROI (Region of Interest), which are then validated for accurate detection. This enables efficient and accurate car plate recognition in applications like traffic monitoring and law enforcement.

How is OCR utilized for the purpose of text recognition?

OCR (Optical Character Recognition) is essential for extracting text from car number plates. After detecting a car plate, OCR techniques are used to recognize and extract the alphanumeric characters. The process involves preprocessing the plate image, segmenting the characters, and applying pattern recognition algorithms. The recognized characters are combined to form the text on the plate. OCR greatly enhances efficiency in applications like automated toll collection, parking management, and law enforcement by eliminating manual data entry and enabling accurate information extraction from car plates.

How does the code detect the edges and contours of the car plate image?

It utilizes grayscale conversion, Gaussian blurring, binary thresholding, morphological opening, and contour detection techniques to find the edges and contours of the car plate image. By enhancing the image quality, highlighting the edges, refining the edges through a morphological opening, and extracting contour boundaries, the code accurately isolates the car plate region. These edge and contour findings facilitate further analysis and operations specific to the license plate, enabling effective processing and identification.

Code to find it

import cv2
harcascade = "haarcascade_russian_plate_number.xml"
plate_detector = cv2.CascadeClassifier(harcascade)
cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    gray_blurred = cv2.GaussianBlur(gray, (5, 5), 0)
    _, threshold = cv2.threshold(gray_blurred, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
    morph_opening = cv2.morphologyEx(threshold, cv2.MORPH_OPEN, kernel)
    contours, _ = cv2.findContours(morph_opening, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    for contour in contours:
        area = cv2.contourArea(contour)
        if area > 500:
            cv2.drawContours(frame, [contour], 0, (0, 255, 0), 2)
            x, y, w, h = cv2.boundingRect(contour)
            cv2.putText(frame, "Contour", (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
            cv2.putText(frame, "Edge", (x + w - 50, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
    cv2.imshow("Edges and Contours", frame)
    if cv2.waitKey(1) == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

Note:- The explanation is accompanied by the complete program in the following description

Output 

python opencv detect and recognize car license plate output

Prerequisites For Detect and Recognize Car License Plates Using Python OpenCV

It is important to have a solid understanding of the Python programming language and the OpenCV library. Apart from this, you should have the following system requirements.

1. Python 3.7  and  above
2. Any Python editor (VS code, Pycharm)
3. Tesseract Setup

Download Python OpenCV Detect and Recognize Car License Plate Project

Please download the source code of Python OpenCV Detect and Recognize Car License Plate Project: Python OpenCV Detect and Recognize Car License Plate Project Code.

Tesseract Setup

To set up Tesseract OCR for text extraction using OpenCV, you can follow these steps:

1. Install Tesseract OCR: You can download and install Tesseract OCR

2. To install Pytesseract, open cmd as administrator, and type the following cmd.

pip install pytesseract

3. Set Path: After installing Tesseract, copy the path of the folder where you install Tesseract and add this to your system environment variable.

Installation

Open Windows cmd as administrator

1. To install the opencv library, run the command from the cmd.

pip install opencv-python

Let’s Implement it.

To implement it, follow the below step.

1. First of all, we are importing all the necessary libraries that are required during implementation.

import cv2
import pytesseract

2. Give the path of the .xml file.

harcascade = "haarcascade_russian_plate_number.xml"

3. It opens the camera.

cap = cv2.VideoCapture(0)

4. The variable min_area is used as a threshold to discard small detected regions, filtering out regions that are below this area value.

min_area = 500

5. It sets the path to the Tesseract OCR executable file in order to configure the pytesseract library.

pytesseract.pytesseract.tesseract_cmd = r"C:/Program Files/Tesseract-OCR/tesseract.exe"

6. Start the while loop.

while True:

7. This captures images or frames from a video camera in Python using the opencv library. cap.read() function, reads the next frame available from the camera and returns two values, ret, image. ‘ret’ is a boolean that returns whether the frame from the camera is successfully read or not. The captured image is stored in the second variable.

ret, frame = cap.read()

8. It assigns the cascade classifier object for license plate detection, while grey stores the grayscale version of the frame image obtained by converting it from the BGR color space.

plate_detector = cv2.CascadeClassifier(harcascade)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

9. It performs license plate detection using the plate_detector cascade classifier on the grayscale image.

plates = plate_detector.detectMultiScale(gray, 1.1, 4)

10. It iterates over the detected license plate regions stored in the plates variable.

for (x, y, w, h) in plates:

11. It calculates the area of a detected license plate region based on its width, w, and height, h.

area = w * h

12. It checks if the area is greater than the threshold area or not.

if area > min_area:

13. It draws a rectangle around a detected license plate region on the frame image.

cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
plate_img = frame[y:y + h, x:x + w]

opencv detect and recognize car license plate output

14. The code processes the captured license plate region. It converts the color image to grayscale, blurs it to reduce noise, and then applies a threshold to obtain a binary representation of the region. These operations are performed to improve the quality of the region and make it easier to extract text or perform further analysis.

gray_plate_img = cv2.cvtColor(plate_img, cv2.COLOR_BGR2GRAY)
gray_plate_img = cv2.GaussianBlur(gray_plate_img, (5, 5), 0)
_, plate_img_thresh = cv2.threshold(gray_plate_img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

Gray Image

opencv project detect and recognize car license plate output

Blur Image

opencv detect and recognize car license plate project output

Binary Image

opencv detect and recognize car license plate binary image output

15. It creates a kernel and uses it to perform morphological opening on the binary image. Morphological opening helps eliminate small noise regions and smoothens object boundaries.

kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
plate_img_thresh = cv2.morphologyEx(plate_img_thresh, cv2.MORPH_OPEN, kernel)

Output of this step

opencv detect and recognize car license plate output morphological

16. It uses pytesseract and its image_to_string() function to extract text from the binary image plate_img_thresh. The configuration parameter –psm 7 is specified to help Tesseract recognize a single line of text. The extracted text is then processed to remove non-alphanumeric characters.

plate_text = pytesseract.image_to_string(plate_img_thresh, config='--psm 7')
plate_text = "".join(c for c in plate_text if c.isalnum())

17. It resizes the license plate image to (400, 100) pixels using cv2.resize() and displays it in a window titled “ROI” using cv2.imshow().

roi_resized = cv2.resize(plate_img, (400, 100))
cv2.imshow("ROI", roi_resized)

18. It displays the extracted car plate number on the screen.

cv2.putText(frame, "Extracted Text: " + plate_text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 0, 0), 2)

Note:- steps 13-18 must be written under the if block of step 12th.

19. It displays the captured frames in the PythonGeeks windows.

cv2.imshow("PythonGeeks", frame)

20. It will wait for user input. Once the user presses the key ‘q’, the program will stop executing.

if cv2.waitKey(1) == ord('q'):
        break

Note:- steps 7-20 must be written under the 6th step while looping.

21. It is used to close all the resources occupied by a program.

cap.release()
cv2.destroyAllWindows()

Output

python opencv detect and recognize car license plate project output

Conclusion

Car number plate detection and recognition systems utilize computer vision techniques to locate and extract license plate regions. They apply cascade classifiers and image preprocessing to enhance the regions, followed by morphological operations to refine boundaries. Using OCR, the systems extract alphanumeric characters and process the text for analysis. Visual feedback allows real-time monitoring. These systems have diverse applications, including traffic management, law enforcement, and parking systems, automating the extraction and analysis of license plate information.

Your opinion matters
Please write your valuable feedback about PythonGeeks on Google | Facebook


1 Response

  1. cyril says:

    Hello, can you make the full code?

    Thinks

Leave a Reply

Your email address will not be published. Required fields are marked *