Car Number Plate Detection System Using Flask and OpenCV

Aditya Pande
5 min readAug 12, 2021

--

Task Description 📄

👉Create a model that will detect a car in a live stream or video and recognize characters on number plate of the car .

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

👉Create a Web portal where all this information will be displayed (using Html, Css, and Javascript)

This was a team task assigned to us by Vimal Daga Sir.

In this task, we have created one Web UI in which you have to select and upload one of image of your car’s number plate and then our webpage will display all the details about that car like owner name, date of registration, insurance, etc. For doing this task we have used following tools and technologies:-

i) Python (Flask framework, OpenCV module, boto3, re, json, etc)

ii) Web Technologies (HTML, CSS, JavaScript)

iii) AWS Cloud Services( AWS S3, AWS Textract)

iv) Haarcascade Russian number plate model for number plate detection.

v) RTO Car Registration API

Flask framework:-

Flask is a popular Python web framework, meaning it is a third-party Python library used for developing web applications.

In this task we have used flask to create a web portal which is asking user to upload the image of car and displays all the information of the car like owner name, insurance, car company name, etc.

We have created two routes one is home and second is upload. When we search for the website on link generated by flask run command i.e http://127.0.0.1:5000 then we are on the home page and then after uploading the image it we will be automatically landed to the upload page.

OpenCV Module:-

Computer vision works much the same as human vision human except humans have a head start. Human sight has the advantage of lifetimes of context to train how to tell objects apart, how far away they are, whether they are moving and whether there is something wrong in an image.

OpenCV (Open Source Computer Vision Library) is an open source computer vision and machine learning software library. It mainly focuses on image processing, video capture and analysis including features like face detection and object detection, and it helps to provide a common infrastructure for computer vision applications. For implementing CV in python, we use OpenCV module that we can also download using command:-

pip install opencv-python

Then import cv2 module into your python code.

Boto3 is also one of python library that we can install using pip command which is used to interact and launch AWS services from our python code.

Similarly re is also one of python library used for regular expressions(Regex) in python and json library for writing code in JSON format.

Web Technologies (HTML, CSS, JavaScript):-

We have used HTML for displaying webpage, CSS for designing webpage and Javascript for making webpage more interactive.

AWS Cloud Services( AWS S3, AWS Textract):-
In our task, we have used AWS S3 service for temporarily storing image uploaded by user so that we can download this image and then analyze and extract text present in that image using AWS Textract OCR(Optical Character Recognition) service.

Haar Cascade XML File:-

Besides the installation of the OpenCV library, another important thing to retrieve is the Haar Cascade XML file.

Let’s first talk a bit about the theory behind Haar Cascades, since it is an important concept. In 2001, Paul Viola and Michael Jones came up with the object detection technique using Haar feature-based cascade classifiers. It is a machine learning based approach (involving AdaBoost) where a cascade function is trained from many positive and negative images. It extracts numerical values for features (e.g. edges, lines) efficiently with the concept of integral image (or summed area table), which trumps the default computationally-heavy way of subtracting sums of pixels across multiple regions of an entire image.

In addition, it uses the ‘Cascade of Classifiers’. This means that instead of applying hundreds of classifiers for the many features within the image at one go (which is very inefficient), the classifiers are applied one-by-one.

Take for example an image of a human face. If the first classifier for the ‘eyes’ feature has failed (i.e. fail to detect human eyes in the image), the algorithm does not bother applying the subsequent classifiers (e.g. for nose, for mouth etc.). Instead, it stops and declares that no face is detected.

On the other hand, if this first ‘eyes’ feature is detected, the algorithm will then apply the second stage of feature classification and continue with the classification process. In the end, if the image passes all classification stages, it can be declared that a face region is present.

Visual representation of Haar Cascade process

OpenCV actually comes with pre-trained XML files of various Haar Cascades, where each XML file contains the feature set. We will be using the Haar Cascade XML file containing the features for Russian car plates, and here’s how you can download the Haar Cascade XML file:

  1. Visit the OpenCV GitHub page containing the Russian car plate Haar Cascade by clicking here.
  2. Right click on the screen (which should be displaying a wall of text with the top line being <?xml version=”1.0"?>), and click ‘Save as..’
  3. In the Save option pop-up, you should see the default file name of ‘haarcascade_russian_plate_number’ and file type ‘XML document’. Leave them as the default, and save this XML file in the path where your Jupyter notebook is located.

To explore other XML files available for experimentation, check out the OpenCV Haar Cascades GitHub resource here.

We have used Haarcascade Russian number plate model for number plate detection from the image so that we can get cropped section of number plate and then use AWS Textract to extract text i.e number out of image.

We have used one RTO Car registration API so that from the number we extracted from number plate, we will get all details about that car.

You first have to create one account on this API by visiting below link:-

Flow of the Task

Let’s understand the flow of this task.

1.First the code of flask will get run(app.py)

2.When user upload the image it get saved in S3 bucket.

3.We will first download this image with the help of curl command.

4.Then we have called the function detected number plate and pass that image to that function. This function is available in the Car_Number_Plate_Detection_Code.py

This file has been imported as a module in the flask code (app.py)

This function returns the number plate.

5.Now we have called RTO function available in that same module. We pass that detected number plate into this function and get the information as an output.

You can also see the below video in which we have demonstrated this task.

f

Github URL:-

Thanks for reading!!

--

--