![]() |
VOOZH | about |
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.
Requirements:
opencv-python >= 3.4.x
numpy >= 1.17.2
skimage >= 0.16.2
tensorflow >= 2.x.
imutils >= 0.5.3
.
Example:
Input:
Output:
29A33185 👁 Image
Approach:
Methodology:
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:
👁 Image
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. The first step is to extract the value channel from the HSV format of the plate's image.
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 and draw it on the character candidate mask.
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.
Full Source Code with its working: First, create a PlateFinder class that finds the license plates and validates their 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:
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 the find_characters_on_plate method.
find_characters_on_plate method uses the 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.
Now use OCR to recognize the character one by one on the extracted license plate.
It loads the pre-trained OCR model and its label file in load_graph and load_label functions. label_image_list method transforms the image to a tensor with the convert_tensor method and then predicts the label of the tensor with the label_image_list function and returns the license number.
Code: Create a main function to perform the whole task in a sequence.
Now, run this main file to see the output.
This is how the output will look like:
How to improve the model?
References:
Image preprocessing techniques in OpenCV documentation.