Deep Learning CNN Model to Auto-Detect Vehicle’s Number Plate Using Python and Flask API

In this article, I am going to show you how you can create CNN Model or Deep Learning Model for Vehicle’s Number Plate Detection System that will get the owner’s information using Python and Flask API.

In this task :
👉Create a model that will detect a car in a live stream or video and recognize characters on the number plate of the car.
👉Secondly, it will use the characters and fetch the owner’s information using RTO APIs.
👉Create a Web portal where all this information will be displayed (using HTML, CSS, and JS).

Prerequisite: →

  1. Python and Deep Learning Libraries Installed OR (You can use Google Colab)
  2. Flask Installed
  3. Video or Image in which we want to detect the car’s plate and retrieve info
  4. Basic knowledge of Image Processing
  5. RTO’s API Key (You can use this API by visiting and creating an account on it)

Code Part-1

(Vehicle’s Number Plate Detection)

The following tasks will be performed in the above code:

  1. First of all, the important libraries are imported like NumPy, cv2, and matplotlib.
  2. Then the CascadeClassifier is used for detecting the vehicle’s number plate region. Cascading classifiers are used to detect a particular feature or a region inside an image. The feature here is the Number Plate of a Vehicle.
  3. Cascading classifiers are trained with several hundred “positive” sample views of a particular object and arbitrary “negative” images of the same size. After the classifier is trained it can be applied to a region of an image and detect the object.
  4. Then there is a function called plate_detect() to detect the vehicle’s number plate and mark it with a green rectangle on it then crop that image’s plate region and return it to another function.
  5. This function will be called from another function called display_img().

Code Part-2

(Displaying the Image)

The following tasks will be performed in the above code:

  1. The above code is used to display the image.
  2. There is a function called display_img() which will take an image as a parameter and convert it from BGR color code to RGB Color code then will display it on screen using matplotlib.
  3. Then we will read an image called car.jpg and call the function plate_detect() and then display the car image and the cropped plate image.

Code Part-3

(Preprocessing Image)

Code Part-4

( Segmentation of Image’s Characters)

Now this part is the Preprocessing of the Image i.e (Number Plate of Vehicle)

Code Part-5

(Image Augmentation and Calculate Accuracy)

Now it’s time to get the training dataset and validation dataset and create a model. We have generated our data that is the preprocessed images dataset from previous steps.

Code Part-6

(Creating and Training the Model)

Code Part-7

Code Part-8

(Getting Vehicle’s Owner Information)

Now the last part is to test the model using the API to get the vehicle’s information.

Flask Web App

Now we will convert the above Deep Learning code into a Flask WebApp.

Code for Getting Vehicle’s Owner Info (

Now this is the main file for Flask

The functionality of this code is to:

  1. Get the HTML file index.html and render it.
  2. Check for the extension of the file that is uploaded (to check for image or mp4 files)
  3. At last, it will get the carDetails.html file which uses the jinja2 template, and Pass the vehicle information to it.

Index.html code

This is a simple HTML file with a background image with some heading and a submit button where you can submit your image or video to test the model.

CarDetails.html code

This HTML file will get the output given by the API and place it within the placeholders and output the details as shown below.

CarDetails.html output

Thanks for Reading !!

Keep Learning !! Keep Sharing !!

You can contact me on:

LinkedIn emailme