Number Plate Recognition using OpenCV Python

Ishita Mittal
5 min readSep 1, 2021

--

Steps:

First we are creating model for that we have 50 types of car band and in each having 100 images . Using that data set we are doing let’s continue…

Import required libraries-

i)import numpy as np

ii)import pandas as pd

iii)import matplotlib.pyplot as plt

AND

iv) import cv2

In above we have stored the model using -

[ ].model.save()

and also we predicted the model of that image using -

[ ].model.predict()

Now we are detecting number plate -

Steps required-

1. License Plate Detection:

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

2. Character Segmentation:

Once we have detected the License Plate we have to crop it out and save it as a new image. Again this can be done easily using OpenCV.

3. Character Recognition:

Now, the new image that we obtained in the previous step is sure to have some characters (Numbers/Alphabets) written on it. So, we can perform OCR (Optical Character Recognition) on it to detect the number.

Prerequisites-

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

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

In above

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

  • The data is all clean and ready, now it’s time do create a Neural Network that will be intelligent enough to recognize the characters after training.
  • For modeling, we will be using a Convolutional Neural Network with 3 layers.
  • For modeling, we will be using a Convolutional Neural Network with 3 layers.
  • To keep the model simple, we’ll start by 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 activation function.
  • Next, we’ll be adding a max-pooling layer with a window size of (4,4).
    Max pooling is a sample-based discretization process. The objective is to down-sample an input representation (image, hidden-layer output matrix, etc.), reducing its dimensionality and allowing for assumptions to be made about features contained in the sub-regions binned.
  • Now it’s time to flatten the node data so we add a flatten layer for that. The flatten layer takes data from the previous layer and represents it in a single dimension.

Finally, we will be adding 2 dense layers, one with the dimensionality of the output space as 128, activation function=’relu’ and other, our final layer with 36 outputs for categorizing the 26 alphabets (A-Z) + 10 digits (0–9) and activation function=’ softmax

👉Training our CNN module-

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

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

👉it will use the characters and fetch the owners information using RTO API’s .

import requests

import xmltodict

import json

Finally we got the owner information from that number plate.

--

--