Detect and Recognize Car License Plate from a video in real time
Recognizing a 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 the validated contour to find characters in it.
- Recognize characters using an OCR.
1. To reduce the noise we need to blur the input Image with Gaussian Blur and then convert it to grayscale.
2. Find vertical edges in the image.
3. 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.
4. Apply Closing Morphological Transformation on the 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 plates.
5. 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 a more relevant and less number of contours in the image. If you draw all the extracted contours on the original image, it would look like this:
6. Now find the minimum area rectangle enclosed by each of the contours and validate their side ratios and area. We have defined the minimum and maximum area of the plate as 4500 and 30000 respectively.
7. 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 the plate:
- Code: This step is performed by clean_plate and ratioCheck method of class PlateFinder.
8. To recognize the characters on the license plate precisely, we have to apply image segmentation. That first step is to extract the value channel from the HSV format of the plate’s image. It would look like.
9. Now apply adaptive thresholding on the plate’s value channel image to binarize it and reveal the characters. The image of the plate can have different lighting 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.
10. After binarizing apply bitwise not operation on the image to find the connected components in the image so that we can extract character candidates.
11. Construct a mask to display all the character components and then find contours in the mask. After extracting the contours take the largest one, find its bounding rectangle and validate side ratios.
12. After validating the side ratios find the convex hull of the contour ad draws it on the character candidate mask. The mask would look like-
13. 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 the 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.
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 the preprocessing method, the following step has been done:
- Blur the Image
- Convert to Grayscale
- Find vertical edges
- Threshold of the vertical-edged image.
- Close Morph the Threshold image.
Method extract_contours returns all external contours from the preprocessed image.
Method find_possible_plates preprocess 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 the 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 the 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 vehicles 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.
Please Login to comment...