Number Plate Recognition using OpenCV Python

Posted By : Rishabh Jain | 03-May-2022

 

Steps:

The first step is to create a model for that we have 50 types of car bands and in each has 100 images. Using that data set we are going to train the model and perform the testing.

Import Python libraries:-

a) import numpy as np

b) import pandas as pd

c) import matplotlib.pyplot as plt

iv) import cv2

In the above figures, we can see that we have stored the model using the line below.

[ ].model.save()

and also we make the prediction of the model of that image using the line command below.

[ ].model.predict()

Now, our goal is to detect number plates -

The steps required are as follows-

1. License Plate Detection :

The first and primary step is to detect the License plate from the car. We will use the contour option which is present in the OpenCV to detect the rectangular objects which enable us to find the number plate. The accuracy can be improved if we know the exact size, color, and approximate location of the number plate used on the cars. Normally the detection algorithm is trained based on the position of the camera and also on the type of number plate used in that particular country, which may vary for different countries. This gets trickier if the image does not even have a car, in this case now, we will do an additional step to detect the car and then also the license plate.

2. Character Segmentation:

After the successful detection of the License Plate, we now have to crop it out and save it as a new image on our server. Again this can be done easily using the help of OpenCV module.

3. Character Recognition:

Now, the new image which we obtained in the previous step is sure to have some characters which may be Numbers or Alphabets written on it. So, we need to perform the OCR (Optical Character Recognition) on it to detect the numbers and characters.

Prerequisites-

  • Python: aka swiss army knife of coding. I have used version 3.7 here.
  • Haar cascade: It is a machine learning object detection algorithm used to identify objects in an image or video and is based on the concept of ?? features proposed by Paul  Viola and Michael Jones in their esteemed paper titled “Rapid Object Detection using a Boosted Cascade of Simple Features” in the year 2001.
  • OpenCV: OpenCV is a library of programming functions mainly aimed at real-time computer vision plus is open-source, fun to work with, and my personal favorite. For this project,  I have used version 4.1.0 of the OpenCV module.
  • Keras: Keras is easy to use and widely supported and used all over the world by python developers, Keras module makes deep learning about as simple as deep learning can be.
  • Import libraries like numpy, pandas, matplotlib.pyplot …etc

In the above screenshots, we have displayed the number plate of that car.

In above

Creating a Machine Learning model and training it for the characters and numbers

  • The dataset is all cleanly created and ready, now it’s time to create a Neural Network which will be intelligent enough to recognize the characters after the training and testing.
  • For the modeling of the model, we would be using a CNN (Convolutional Neural Network) with 3 layers architecture.
  • To keep the model simple, we’ll start our model by simply creating a sequential object.
  • The first layer will be a convolutional layer with 16 output filters, a convolution window of size (5,5), and ‘Relu’ as the activation function.
  • The next step is that we’ll be adding a max-pooling layer with a window, which would be the size of (4,4).
    Max pooling is a sample-based discretization process. Our main goal is to downsample an input representation (image, hidden layers, output matrix, etc.), reducing its dimensionality and also allowing for assumptions that need to be made about features contained in the sub-regions binned.
  • Now it’s the time to flatten the node data so we add a flatten layer for that. The flattened layer takes data from the previous layer and represents it in a single dimension.

Our final step would be to add two dense layers, one with the dimensionality of the output space as 128, Activation function= 'relu', and others, our final layer will have 36 outputs for categorizing the 10 digits (0–9) and the 26 alphabets (A-Z) and activation function used would be softmax

Training our CNN module-

The data we will be using contains images of digits (0–9) and alphabets (A-Z) of size 28x28, also the data is balanced so we won’t have to do any kind of data tuning here.

Finally, it's time to test our model, remember the binary images of extracted characters from the number plate? Let’s feed the images to our model!

It will use the characters and fetch the owner's information using RTO APIs.

The libraries imported are:-

1) import requests

2) import xmltodict

3) import JSON

Finally, we got the owner's information from that number plate.

About Author

Author Image
Rishabh Jain

He is a Devops person who loves Automation and have skills in various technologies like Docker, Linux, Kubernetes, Ansible, Terraform and Many More.

Request for Proposal

Name is required

Comment is required

Sending message..