License Plate Detector — Code, Build and Deploy

Apoorva Dave
DataDrivenInvestor

--

This article talks about the detection of license plates in images and videos using machine learning and python. Also, it discusses about how can we deploy our machine learning models using Flask as an API. The scope of the project is limited to the following main points:

  1. It detects the license plate out of images and videos with a single car. Multiple images of the car in the same picture are not handled.
  2. For the purpose of simplicity, it is assumed that there is less disturbance in the input images/videos.
  3. The same DetectPlate.py file is used for detection of license plates in videos as well as images. Accordingly, the code has to be commented and used.

Approach

The method that I followed was:

  1. Detect License Plate using CCA (Connected Component Analysis)
  2. Perform segmentation of characters (applying CCA on the plate)
  3. Train a ML model using images of characters (numbers and alphabets)
  4. Prediction of characters in License Plate
  5. Deployment of this model using Flask as an API.

Detect License Plate

The approach used to segment the images is Connected Component Analysis. Connected regions imply that all the connected pixels belong to the same object. A pixel is said to be connected to another if they both have the same value and are adjacent to each other.

A brief of what I did in my detectplate.py file is depicted below:

Car Image -> Grayscale Image -> Binary Image -> Applying CCA to get connected regions -> Detect license plate out of all connected regions

The input car image is converted to grayscale and then to binary image. CCA is applied on this binary image of car using measure and regionprops module of python. measure.label method labels all the connected regions of binary car image and returns a labelled image. Assumptions are made that width of the license plate region to the full image ranges between 15% and 40% and height of the license plate region to the full image is between 8% and 20%. Regionprops method is applied on labelled image which returns a list of regions along with their properties like area, bounding box etc. The bounding box coordinates are obtained and are then compared with the maximum and minimum dimensions of plate (according to assumption). Once the labelled region satisfies the dimension constraints, it is pushed into a list called plate_like_objects. This list will hold the objects which are classified by our code as similar to license plate. (I have observed for a few images, more than 1 object is returned which looks like a license plate. This needs to be filtered before proceeding to next step).

Input Image (Left) — License Plate Detected (Right) (The first and last 2 characters have been blacked for the purpose of privacy :p )

Segmentation of Characters

The output of above step is a car image with a bounding box in red around the license plate. CCA is again applied on the license plate to segment the characters. Here similar assumptions are made for the size of characters. Once characters are segmented, they are resized to a size of 20px X 20px and are pushed into a list.

Segmented characters of License Plate

Training of Model

Model is trained using SVC (4 cross-fold validation) on dataset of images of characters with size 20px X 20px.

Once the characters of the plate are obtained and the model is trained, the model is loaded in order to predict each character.

To handle videos as input, I am capturing different images of the car in the video unless until the user presses a key to quit. The last video frame is then used for detection. It is assumed that the video file contains only 1 car.

The complete code along with comments and description is available on my GitHub repository:

Deployment of the model

I always wondered how can we efficiently deploy our models into production. Flask is an efficient web application framework which enables us to deploy our models as an API. You can work differently to setup your project using Flask. I have created a single API for the sole purpose of demonstration. It can be written better in a more modular and structured way :) Details for this are given in repo:

This is my first attempt to share my work in the field of image processing and is an extension to the series of articles where I was talking about classification and regression. If you people liked it, please do show some ❤

As I read and explore more, I will continue writing and sharing of what I have learned :)

Stay tuned for more articles! Till then happy learning :)

--

--