ANPR: Automatic Number Plate (Licence plate)Recognition

Paramteraiya
5 min readDec 23, 2020

--

We are living in an era, where people are gonna compare lifestyles using the “Before Covid & after Covid” phrase. We required solutions that deduct human interaction, and here I am solving one of them.

source: http://www.omnitecgroup.com/Solutions/parking-controls/anpr-system

Authentication of vehicles in a housing society, parking, the commercial building has been a problem since we are in a country where the amount of purchase of vehicles is higher than parking facilities. Hence we struggle a lot with these problems. We might need a few or many solutions that can solve problems with entities rather than developing resources. Automatic Number Plate (Licence Plate) Recognition (ANPR) is one of them, that too with entities like vehicles. I do not need to mention that it does solve problems related to Monitoring vehicle movements, Controlling entry and exit within: parking, educational establishments, police, hospitals, and garages, logging staff arrival/exit at commercial premises, speed detection and enforcement, parking management, validating vehicle types and payments at toll stations and law enforcement. The government of Gujarat used the ANPR system for maintaining lockdown protocol (in a covid-19 pandemic) to monitor vehicle entries.

Pheww. Enough of discussing the problem statement, let’s build a solution to it. We are gonna be working on image input, after this blog you must try for the real-time video frame.

If you’re familiar with OpenCV and python you will enjoy the process more, otherwise, you will learn.

so, the first and foremost step is to take image input(RGB) and resize it to some pixel. below is the code with comments to do it.

import cv2 #importing opencv library
img = cv2.imread("file location")
resized_img = cv2.resize(img, (1366, 768))
cv2.imshow('resized',resized_img)
cv2.imwrite('resized.jpg', resized_img)
cv2.waitKey(0)cv2.destroyAllWindows()
resized input image

Now, we need to convert this RGB image to Grayscale. Because in many applications of image processing, color information doesn’t help to identify important edges or any features. Here we highly need of edge of an object out of the image we feed to it.

gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #opencv function to convert RGB to Grayscale
cv2.imshow('Grayscale',gray_img)
cv2.imwrite('gray_img.jpg', gray_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Grayscale input image

Some of you might think after looking at image input that, every input image can not be this clear(sharp) or without noise. In order to overcome the noise issue, here we are going to use a bilateral filter; the bilateral filter is a non-linear, edge-preserving, and noise reduction smoothing filter. It replaces the intensity of each pixel with a weighted average of the intensity.

You can read more from the below link:

In our case image looks good (sharp and denoise), though you can observe some difference compare to the above images.

gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
kernel = np.ones((5,5),np.float32)/25
dst = cv2.filter2D(gray_img,-1,kernel)
cv2.imwrite('biliteral.jpg', gray_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Grayscale input after applying the bilateral filter

After all of this pre-processing, we can move on to the detection part. Fundamentally we need the edges of the license plate, which can be inputted to the tesseract function. To be able to detect edges, we’ll be using the Canny edge detection algorithm (there are others too), which gives a very accurate output. learn more from the below link:

edges = cv2.Canny(resized_img,100,200)
cv2.imshow('canny edge',edges)
cv2.imwrite('edges.jpg', edges)
cv2.waitKey(0)
cv2.destroyAllWindows()
using a canny edge detection algorithm

The most important part of this entire project is to find the best possible approximate contour of the number plate. Contours can be explained simply as a curve joining all the continuous points (along the boundary), having the same color or intensity. The contours are a useful tool for shape analysis and object detection and recognition.

ret, thresh = cv2.threshold(gray_img, 127, 255, 0)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img, contours, -1, (0, 255, 0), 3)
imS = cv2.resize(img, (960, 540))
cv2.imwrite('counter.jpg', imS)
cv2.waitKey(0)
cv2.destroyAllWindows()
contour detection

contour detection looks very complicated to understand without looking deep into the documentation. here is the link:

Now, the final step is to read the license plate. We were performing all of the above steps just to get the most processed input which can undergo an image to a string. Python-tesseract is an optical character recognition (OCR) tool for python. That is, it will recognize and “read” the text embedded in images.

import pytesseract
text = pytesseract.image_to_string(img_rgb)

This was just to understand the entire working of the Automatic Number Plate Recognition system. If you’re planning to extend it to production-ready and robust you would need to write scalable code. Also In order to make the entire working system, you will require to connect to one of the databases which can store the license plate data. Making a flask based web application of this would be a really good idea. Also, there can add some features related to authentication and storing a new license plate.

It was a basic intuition to the entire system. Now the sky is yours :)

--

--

No responses yet