https://static0.makeuseofimages.com/wordpress/wp-content/uploads/2023/01/license-plate-detection-featured-image.jpegLicense plate detection and recognition technology has many applications.

It can be used in road systems, ticketless parking lots, vehicle access control residences, and more.

This starts with setting up the program’s environment.

A car with all visible contours marked green

Before you begin coding, you’re gonna wanna install some libraries in your environment.

Open any Python IDE and create a Python file.

Run each command on the terminal to plant the respective library.

A car showing the detected license plate

You should have a priorinstallation of Python PIP on your setup.

You should install it on your rig before using the pytesseract library.

Importing the Libraries

Begin by importing the libraries you installed in the environment.

Three car pictures and one cropped license plate showing the process of detecting a license plate

Importing the libraries allows you to call and use their functions in the project.

you gotta import theOpenCV-Pythonlibrary ascv2.

Import the other libraries using the same names you used to install them.

Taking the Input

Then point pytesseract to the location where the Tesseract engine is installed.

Take the car image as the input using thecv2.imreadfunction.

Replace the image name with the name of the image you are using.

Store the image in the same folder as your project to keep things easy.

you could replace the following input image with the one you would like to use.

Preprocessing the Input

Resize the image width to 500 pixels.

Then convert the image to grayscale as thecanny edge detection functiononly works with grayscale images.

Finally, call thebilateralFilterfunction to reduce the noise in the image.

Performing Edge Detection

Begin by calling thecv2.Cannyfunction which will automatically detect the edges on the preprocessed image.

It is from these edges that we will find the contours.

Finding the Contours

Call thecv2.findContoursfunction and pass a copy of theedged image.

This function will detect the contours.

Draw around the detected contours on the original image using thecv2.drawContoursfunction.

Finally, output the original image with all the visible contours drawn.

The program draws all the contours that it finds on the car image distinctively.

After finding the contours you gotta sort them to identify the best candidates.

Sorting the Contours

Sort the contours based on minimum area 30.

Ignore the ones below that as they are less likely to be the license plate contour.

Make a copy of the original image and draw thetop 30contours on the image.

Finally, display the image.

There are now fewer contours than there were at the beginning.

The only contours drawn are the ones approximated to contain the license plate.

Looping Over the Top 30 Contours

Create a for loop to loop over the contours.

Look for the contour with four corners, and determine its perimeter and coordinates.

Store the image of the contour containing the license plate.

Finally, draw the license plate contour on the original image and display it.

After looping, your program has identified the contour containing the license plate.

It draws on the license plate contour only.

Load the license plate image you previously stored and display it.

Then, call thepytesseract.image_to_stringfunction and pass the cropped license plate image.

This function converts the characters in the image to a string.

The cropped license plate is shown below.

The characters on it will be the output you will later print on the screen.

Having detected and recognized the license plate, you are ready to display the output.

Displaying the Output

This is the final step.

You print the extracted text to the screen.

This text contains the characters of the license plate.

It is challenging, so it should help you to learn more about Python.

When it comes to programming, practice is core to the mastery of a language.

To practice your skills, you should probably work on interesting projects.