License plate detection is the process of using computer vision techniques to automatically detect and recognize license/number plates in images or video streams.
This task is useful in many applications, such as traffic management, automatic toll payment, and parking control.
License plate detection is basically divided into two tasks:
In this tutorial, we will focus on the detection part and not the recognition aspect. We will learn how to use OpenCV to detect license/number plates in images.
There are different techniques for license plate detection, such as color-based object detection, template matching, machine learning algorithms (e.g. Haar cascades), deep learning algorithms (e.g. YOLO), etc. In our case, we will use Haar cascades, which is one of the most popular methods for license plate detection.
So, what is a Haar cascade?
Haar cascade is a type of machine-learning algorithm that is used for object detection. It was first introduced in 2001 by Paul Viola and Michael Jones in the paper Rapid Object Detection using a Boosted Cascade of Simple Features.
The algorithm uses a set of simple features to detect objects in images. It is particularly efficient for detecting objects with complex patterns and shapes, such as faces, cars, and license plates.
Training Haar Cascade Classifier
Now, to train a Haar cascade classifier, we need to provide it with training data consisting of positive and negative samples. Positive samples are images containing the object we want to detect, while negative samples are images that do not contain the object.
Training a Haar cascade classifier can be a time-consuming and challenging process, and it requires a significant amount of labeled data. However, OpenCV provides pre-trained Haar cascades for several objects, including license plates.
Basically, to detect license plates using OpenCV Haar cascades, we need to follow these steps:
Hopefully, you have a basic understanding of Haar cascades and how to use them to detect objects in images. Let's now get our hands dirty and see how to detect license plates in images using OpenCV and Python.
If you go to the repository of OpenCV, you will find that there are several pre-trained Haar cascades. The one we want to use in this tutorial is the haarcascade_russian_plate_number.xml file. Download it and put it in the project directory.
Now, create a new Python file, and let's write some code:
import cv2
width = 800
height = 400
# load the image, resize it, and convert it to grayscale
image = cv2.imread("1.jpg")
image = cv2.resize(image, (width, height))
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# load the number plate detector
n_plate_detector = cv2.CascadeClassifier("haarcascade_russian_plate_number.xml")
We set the height and width of the image, load our image, resize it, and convert it to grayscale.
The final line of code is where the most important action takes place! We load the license plate detector using cv2.CascadeClassifier(). This function loads a pre-trained Haar cascade classifier from an XML file.
Now, we're ready to detect license plates in the image:
# detect the number plates in the grayscale image
detections = n_plate_detector.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=7)
# loop over the number plate bounding boxes
for (x, y, w, h) in detections:
# draw a rectangle around the number plate
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 255), 2)
cv2.putText(image, "Number plate detected", (x - 20, y - 10),
cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 255, 255), 2)
# extract the number plate from the grayscale image
number_plate = gray[y:y + h, x:x + w]
Once our detector is loaded, we can apply it to the grayscale image to detect the number plates using the detectMultiScale function.
The function returns a list containing the bounding boxes of the detected number plates on the image.
So we loop over this list and draw a rectangle around each detection. We also write the text "Number plate detected" above the bounding box; we remove some pixels from the x, y coordinates to make sure the text is well centered.
We then extract the number plate from the grayscale image by using the coordinates of the bounding box (see: How to Crop Images with OpenCV and Python).
Finally, we can display our number plate ROI and the number plate detection on the original image:
cv2.imshow("Number plate", number_plate)
cv2.imshow("Number plate detection", image)
cv2.waitKey(0)
The image below shows the result of our Haar cascade number plate detector:
Here, the algorithm successfully detected the car's license plate.
Let's try another image:
Again, our classifier had no problem detecting the license plate in the image.
Haar cascade classifiers are known for false-positive detections (detect the object when the object is not present in the image). Let's see an example of a false-positive detection:
Here as you can see, the two license plates were detected successfully, on the other hand, we can see a third detection while there is no license plate. This is one of the disadvantages of using Haar cascade.
But if you can tolerate some false positive detection it will be advantageous to use Haar cascades because they are super fast and can easily run in real-time.
Congratulations! You have successfully learned how to use OpenCV, Python, and Haar cascades to detect license/number plates in images.
While false-positive detections can be a drawback, the advantages of Haar cascades in terms of speed and real-time performance make them a viable option for certain applications.
It is important to consider the requirements of your project and weigh the trade-offs between accuracy and speed before deciding which object detection technique to use.
Please note that license plate detection is only the first step in building an Automatic Number Plate Recognition (ANPR) system.
If you're interested in taking your knowledge of number plate detection to the next level and building an end-to-end Automatic Number Plate Recognition (ANPR) system with state-of-the-art deep learning techniques, then you might want to check out my new ebook, "Mastering YOLO: Build an Automatic Number Plate Recognition System".
In this comprehensive guide, you'll learn everything you need to know to master YOLO and build your own ANPR system. From data collection to deployment, you'll gain practical skills that can be applied to real-world projects.
While this blog post covers the basics of number plate detection using OpenCV and Python, my ebook provides even more in-depth knowledge and practical skills related to building an ANPR system with YOLO. So if you're interested in taking your skills to the next level, be sure to check it out!
I hope this tutorial has been helpful.
You can get the source code for this article by clicking this link.
If you have any questions or want to say something, please leave a comment in the section below.
Merci Richard
Feb. 19, 2022, 9:51 p.m.
here cv2.waitkey should be 1 instead of 0.
June 18, 2022, 5:09 a.m.
If we use 1 instead of 0, the window will wait 1 millisecond before closing the window and we won't have time to see the image. Using 0 instead will wait indefinitely until we press any key
June 18, 2022, 12:01 p.m.
Feb. 19, 2022, 2:09 p.m.