Step1: Car Detection
We will detect our car using the LBPH algorithm.
Local binary patterns (LBP) is a type of visual descriptor used for classification in computer vision. LBP was first described in 1994 and has since been found to be a powerful feature for texture classification; it has further been determined that when LBP is combined with the Histogram of oriented gradients (HOG) descriptor, it improves the detection performance considerably on some datasets. A comparison of several improvements of the original LBP in the field of background subtraction was made in 2015 by Silva et al. A full survey of the different versions of LBP can be found in Bouwmans et al.
The LBP feature vector, in its simplest form, is created in the following manner:
- Divide the examined window into cells (e.g. 16x16 pixels for each cell).
- For each pixel in a cell, compare the pixel to each of its 8 neighbors (on its left-top, left-middle, left-bottom, right-top, etc.). Follow the pixels along a circle, i.e. clockwise or counterclockwise.
- Where the center pixel’s value is greater than the neighbor’s value, write “0”. Otherwise, write “1”. This gives an 8-digit binary number (which is usually converted to decimal for convenience).
- Compute the histogram, over the cell, of the frequency of each “number” occurring (i.e., each combination of which pixels are smaller and which are greater than the center). This histogram can be seen as a 256-dimensional feature vector.
- Optionally normalize the histogram.
- Concatenate (normalized) histograms of all cells. This gives a feature vector for the entire window.
The feature vector can now be processed using the Support vector machine, extreme learning machines, or some other machine learning algorithm to classify images.
LBPH is widely used in facial recognition and texture analysis for fast prediction but it can also be used for image classifiers such as an image of a car. So now we will get the data set of various cars and train our data via LBPH. The next step would be to train our LBPH model to detect the cars.
Then we would save our trained model for future use in a yml file.
Now we will write a code to detect a car using the saved model.
Step2: Number Plate Detection
Now, that the image has been extracted from the running video as soon as the car is detected we will need a model to crop the number plate and then detect the text. We can use pytesseract for this work.
Pytesseract or Python-tesseract is an Optical Character Recognition (OCR) tool for python. It will read and recognize the text in images, license plates, etc. Here, we will use the tesseract package to read the text from the given image.
Mainly, 3 simple steps are involved here as shown below:-
- Loading an Image saved from the computer or download it using a browser and then loading the same. (Any Image with Text).
- Binarizing the Image (Converting Image to Binary).
- We will then Pass the Image through the OCR system.
Below is the code to detect the car number plate text with the help of pytesseract.
As expected, our code has detected the number plate in the car image, cropped and converted it into text via OCR.
Step3: API for Vehicle Info
API is the acronym for Application Programming Interface, which is a software intermediary that allows two applications to talk to each other. Each time you use an app like Facebook, send an instant message or check the weather on your phone, you’re using an API.
This time we need an API that will go to the website to retrieve car information when the car number is passed.
So we need to create a python code that will go and retrieve data from this site a return back data in string type. First, create an id on this site and save its username.
The output will be as follows:
Web Site Creation
Now we have created all the parts of our code now we need to design a site that will provide these services for public use.
You can get the source code from the GitHub link for everything on this blog attached at the very end of the blog.
Here is a snapshot of the code and folder structure of the web app.