License Plate Detection with OpenCV — Python

Kerem Kargın
6 min readMay 24, 2021

In this blog post, we’ll do a small project with OpenCV. In this project, I’ll try to explain how we can detect the vehicle's license plate from a vehicle image. The content of the blog post will be as follows:

  • Why is it necessary to detect the license plate?
  • Code Section about License Plate Detection
  • Code Review and Explanation
  • Summary

You can follow me to learn and review more Computer Vision apps like this one. Let’s start.

You can follow my GitHub profile for application-related codes and much more.

Github: https://github.com/keremkargin0

Why is it necessary to detect the license plate?

Plate detection has become one of the most popular applications recently. You may have wondered why we are detecting license plates. That’s why I want to talk about where this project can be implemented.

License plate detection can be used, for example, in a parking lot, when a ticket will be issued to vehicles entering the parking lot. In this case, when the vehicles come in front of the barriers, their plates are read and a ticket belonging to that plate is created from the system. This ticket can be used to calculate a fee payment process during the time the vehicle belonging to the relevant plate is in the parking lot.

Apart from this, we may be looking for a vehicle belonging to a certain license plate within a certain region. To do this, let’s assume that we also have cameras where we monitor all the paths. By detecting the license plate of each vehicle passing through the cameras and keeping it in a list, that vehicle can be tracked directly if it matches the license plate of the searched vehicle.

I tried to make the subject more understandable by sharing a few examples that came to my mind with you. Let’s start writing the codes now. Then I will describe all the steps one by one.

Code Section about License Plate Detection

License Plate Detection

Code Review and Explanation

In this section, I’ll try to explain what it means and its usage when writing step-by-step codes.

  • First, we import the necessary libraries that we will use throughout our project.
cv2, numpy, pytesseract (to be able to read text in image), imutils
  • In our project, we read the image of the vehicle whose plate we want to detect and save it as the plate variable.
Here cv2.imread function is used.
  • Since we will do some operations on the image, we first convert the image from bgr to gray format.
We apply COLOR_BGR2GRAY using the cv2.cvtColor function.
  • We apply a filtering process to the image in gray format.
In this section, we are using the bilateralFilter. So we do the softening process to the image. You can adjust the softening rate by changing the values here.
  • We are doing the process of detecting the corners in the filtered image. Here, we applied the filtering process above to make the corners determined more accurate and bring the plate to the fore. We save the image whose corners are detected as edged.
We use the Canny function to find corners. As an argument, it takes the image whose corners will be found, and the minimum and maximum threshold values. Threshold values are as I have adjusted properly to find the corners. By changing these values, you can observe how the corner finding rate changes.
  • We need to find the contours in the visual with the corners found. Thus, in the next step, we will tailor the image to determine the area where the plate is located.
We use cv2.findContours function to find contours. It takes as argument the image whose contours will be found and in addition to it, 2 experimentally accepted arguments. One of them is cv2.RETR_TREE and the other is cv2.CHAIN_APPROX_SIMPLE. You can always use these arguments in the same way.
  • We are capturing the appropriate contours in the visual with the contours found. We will use the imutils library for this.
The imutils.grab_contours function captures the contours.
  • After finding the suitable contours, we need to list the contours we have caught. Because we’re actually looking for a rectangular box with a plate. At this point, we will try to find this rectangle by looking at the contours we have caught. As an important point when looking for a rectangle, we should know that we are looking for contours that are positioned one after another. If these successive points form a rectangle, we can say that the plate is here.
We will use the sorted function to sort the contours. It takes the captured contours as arguments, by what the contours will be sorted, and whether to sort from largest to smallest or largest to largest. If we say reverse = True, it sorts the entered values in reverse order, ie from greater to less.
  • We write a for loop to find a 4-sided closed shape within the contours we are looking for. Before writing this, we define a variable named screen. Because we will use it in a for loop soon.
We will look at every element of the cnts variable. Epsilon is an empirically proven value, so you can always use it that way. We start looking for a shape from contours with the approxPolyDP function. The argument True at the end of the function is given because it is a closed shape. We save these values in approx. If we get a situation equal to 4 in approx, congratulations we have found the plate. As a result of this situation, we can break the cycle.
  • We will apply a masking process to this plate area we detect.
We create a canvas called mask that is the same size as the image.
  • We’re going to draw a drawing on the canvas called this mask. We want to make this drawing in the area where the plate is saved in the screen variable. It keeps the coordinate information in the screen variable.
While doing this drawing, we use the function cv2.drawContours. We turn the plate area white. All remaining areas will be black.
  • We will paste the text in the plate area to this contour area.
We do this with the function cv2.bitwise_and.
  • We are doing some operations to crop the plate area.
  • As a result, using the pytesseract library, we read the plate information in the image in the plate area from the visual and print it as text.
For this, we use the image_to_string function from the pytesseract library. As an argument, it takes the visual that the text will be read and the language in which this text is to be read. I choose Turkish language to try here. If you want to try it in your own language, you may need to download it additionally. English language is automatically included in the Pytesseract library. You can also do many operations in the English language.
  • Finally, we print this plate as text on the screen. And we show the license plate image on the screen.
Detected Plate

Summary

As a result, we need to have a visual of a vehicle with a specific license plate. When this condition was met, we used the function of reading the text from the visual after performing some operations such as filtering, masking, determining the contours of this visual. As a result, we could read the license plate of the vehicle and print it on the screen in text. I hope the application was useful for you. Don’t forget to stay tuned for the next applications.

While doing these applications, I was inspired by the course in the link below.

Computer Vision Course

--

--

Kerem Kargın

BSc. Industrial Eng. | BI Developer & Machine Learning Practitioner | #BusinessIntelligence #MachineLearning #DataScience