Number Plate Detection with Python

In recent years, automatic number plate detection or license plate recognition has been one of the useful approaches for vehicle surveillance. In this article, I will introduce you to a machine learning project on Number Plate Detection with Python.

Machine Learning Project on Number Plate Detection

Traffic control and the identification of vehicle owners have become a major problem in all countries. Sometimes it becomes difficult to identify a vehicle owner who is breaking the rules of the road and driving too fast.

Also, Read – 100+ Machine Learning Projects Solved and Explained.

Therefore, it is not possible to catch and punish such kind of people because the traffic personnel might not be able to retrieve the vehicle number of the moving vehicle due to the speed of the vehicle. So to solve these types of problems we need to create a Number Plate Detection System. There are many Number Plate Recognition systems available today.

These systems are based on different methodologies, but it is always a very difficult task as some of the factors like high vehicle speed, non-uniform vehicle number plate, vehicle number language and different lighting conditions can affect a lot of overall recognition rate. Most systems operate under these limitations. In the section below, I will introduce you to a machine learning project on Number plate detection with Python.

Number Plate Detection with Python

Now, In this section, I will take you through how to create a Machine Learning model for the task of Number Plate Detection with Python. I will start this task by importing the necessary Python libraries and the dataset:

The dataset I am using here is in JSON format with multiline records. The two main elements are content and annotation. The content contains links to images and an annotation contains information about the respected image.

I wrote a simple script to download and save all images to a directory while saving their respected annotation information in a dictionary. At first, I thought all images were in JPEG format. However, a quick inspection of the uploaded images showed this assumption to be wrong. Some images are in GIF format. So before saving the images, I converted them to JPEG images with three channels (RGB) using the PIL library in Python:

Downloaded 237 car images.
df = pd.DataFrame(dataset)
df.to_csv("indian_license_plates.csv", index=False)
df = pd.read_csv("indian_license_plates.csv")
df["image_name"] = df["image_name"] + ".jpeg"
df.drop(["image_width", "image_height"], axis=1, inplace=True)

In the code below I have chosen five random records from the dataset for later visual inspection of the predictions. I deleted these records from the original dataframe to prevent the model from being trained on them:

Creating a Convolutional Neural Network

To train a machine learning model for Number Plate Detection, I’ll first create an ImageDataGenerator object from Keras to load batches of images into memory. This process is necessary because we do not have infinite memory in RAM and GPU RAM.

Then I will split the data in half with a batch size of 32 images. One for training (80% of data) and one for validation (20% of data) during training. Validation is important to see if the model overfits the training data:

Now I will create a convolutional neural network. It will have 8 convolutional layers with a maximum of 4 pool layers and a fully connected network with 2 hidden layers:

To find the minimum number of steps to cover all lots, the following equations are needed:

Train step size: 7
Validation step size: 1

Training Model for Number Plate Detection

Now, let’s train our model for the task of the Number plate detection system:

Now let’s have a look at the results given by our CNN for Number plate detection:

I hope you liked this article on how to train a Machine Learning model for Number Plate Detection with Python. Feel free to ask your valuable questions in the comments section below.

Articles: 77


  1. Hi Aman,
    Your code is simply clear and very useful for the beginner. I don’t see why these peoples are not recognized u r such hard work.

    I am enjoying a lot of learnings by reading your article.


Leave a Reply