Recognizing Car License Plate is a very important task for a camera surveillance-based security system. We can extract the license plate from an image using some computer vision techniques and then we can use Optical Character Recognition to recognize the license number. Here I will guide you through the whole procedure of this task.
- Find all the contours in the image.
- Find the bounding rectangle of every contour.
- Compare and validate the sides ratio and area of every bounding rectangle with an average license plate.
- Apply image segmentation in the image inside validated contour to find characters in it.
- Recognize characters using an OCR.
- To reduce the noise we need to blur the input Image with Gaussian Blur then convert the it to grayscale.
- Find vertical edges in the image.
- To reveal the plate we have to binarize the image. For this apply Otsu’s Thresholding on the vertical edge image. In other thresholding methods we have to choose a threshold value to binarize the image but Otsu’s Thresholding determines the value automatically.
- Apply Closing Morphological Transformation on thresholded image. Closing is useful to fill small black regions between white regions in a thresholded image. It reveals the rectangular white box of license plate.
- To detect the plate we need to find contours in the image. It is important to binarize and morph the image before finding contours so that it can find more relevant and less number of contours in the image. If you draw all the extracted contours on original image, it would look like this:
- Now find the minimum area rectangle enclosed by each of the contour and validate their side ratios and area. We have defined the minimum and maximum area of the plate as 4500 and 30000 respectively.
- Now find the contours in the validated region and validate the side ratios and area of the bounding rectangle of the largest contour in that region. After validating you will get a perfect contour of a license plate. Now extract that contour from the original image. You will get the image of plate:
Code: This step is performed by clean_plate and ratioCheck method of class PlateFinder.
_, contours, _
# index of the largest contour in the
# areas array
x, y, w, h
, [x, y, w, h]
, area, width, height):
(ratio < ratioMin
ratio > ratioMax):
- To recognize the characters on license plate precisely, we have to apply image segmentation. For that first step is to extract the value channel from the HSV format of the plate’s image. It would look like-
- Now apply adaptive thresholding on the plate’s value channel image to binarize it and reveal the characters. The image of plate can have different lightning conditions in different areas, in that case adaptive thresholding can be more suitable to binarize because it uses different threshold values for different regions based on the brightness of the pixels in the region around it.
- After binarizing apply bitwise not operation on the image to find the connected components in the image so that we can extract character candidates.
- Construct a mask to display all the character components and then find contours in mask. After extracting the contours take the largest one, find its bounding rectangle and validate side ratios.
- After validating the side ratios find the convex hull of the contour ad draw it on the character candidate mask. The mask would look like-
- Now find all the contours in the character candidate mask and extract those contour areas from the plate’s value thresholded image, you will get all the characters separately.
Steps 8 to 13 are performed by segment_chars function that you can find below in the full source code. The driver code for the functions used in steps 6 to 13 is written in the method check_plate of class PlateFinder.
- Now use OCR to recognize the character one by one.
Above 4 steps are performed by preprocess method of class PlateFinder
This step is performed by extract_contours method of class PlateFinder
Code: Methods validating the area and side ratios of minimum area rectangle are validateRatio and
preRatioCheck of class PlateFinder:
Full Source Code with its working: First, create a PlateFinder class that finds the license plates and validates its size ratio and area.
Here is the explanation of each and every method of PlateFinder class.
In preprocess method, following step has been done:
- Blur the Image
- Convert to Grayscale
- Find vertical edges
- Threshold the vertical edged image.
- Close Morph the Threshold image.
Method extract_contours returns all external contours from the preprocessed image.
Method find_possible_plates precprocess the image with preprocess method then extracts contours by extract_contours method then it checks side ratios and area of all extracted contours and cleans the image inside the contour with check_plate and clean_plate methods. After cleaning the contour image with clean_plate method, it finds all characters on the plate with find_characters_on_plate method.
find_characters_on_plate method uses segment_chars function to find the characters. It finds characters by computing the convex hull of the contours of a thresholded value image and drawing it on the characters to reveal them.
Code: Make another class to initialize Neural Network to predict the characters on the extracted license plate.
It loads the pretrained OCR model and its label file in load_graph and load_label functions. label_image_list method transforms the image to tensor with convert_tensor method and then predicts the label of tensor with label_image_list function and returns the license number.
Code: Create a main function to perform the whole task in a sequence.
You can download the source code with OCR model and testing video from my GitHub .
How to improve the model?
- You can set a particular small region in the frame to find the plates inside it.(make sure all vehicle must pass through that region).
- You can train your own machine learning model to recognize characters because the given model doesn’t recognize all the alphabets.
Automatic Number Plate Recognition System (ANPR): A Survey by Chirag Indravadanbhai Patel.
Image preprocessing techniques in OpenCV documentation.
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.
- License Plate Recognition with OpenCV and Tesseract OCR
- Detect Cat Faces in Real-Time using Python-OpenCV
- Python - Displaying real time FPS at which webcam/video file is processed using OpenCV
- Pafy - Checking CC License for each item of Playlist
- Train a Support Vector Machine to recognize facial features in C++
- OpenCV and Keras | Traffic Sign Classification for Self-Driving Car
- Car driving using hand detection in Python
- Overview of Kalman Filter for Self-Driving Car
- MoviePy – Changing Image and Time at same time of Video Clip
- Real-Time Edge Detection using OpenCV in Python | Canny edge detection method
- Python | Get the real time currency exchange rate
- Python | Real time weather detection using Tkinter
- OpenCV | Real Time Road Lane Detection
- YOLO : You Only Look Once - Real Time Object Detection
- Get Bit Coin price in real time using Python
- Multiple Color Detection in Real-Time using Python-OpenCV
- Python | Real time currency convertor using Tkinter
- Pafy - Getting Video ID of the given Video
- Pafy - Getting Best Video Stream of the given Video
- Extracting the real and imaginary parts of an NumPy array of complex numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.