This website is made possible by displaying online advertisements to our visitors.
Please consider supporting us by disabling your ad blocker.


Number Plate Detection with OpenCV and Python

Feb. 14 2022 Yacine Rouizi
OpenCV Computer Vision Object Detection
Number Plate Detection with OpenCV and Python

In this tutorial, we will see how to use OpenCV Haar Cascades to detect license/number plates.

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.

Now to train this classifier, we need to provide training data consisting of positive samples (images containing the object) and negative samples (images not containing the object).

But we don't need to train our own Haar cascade classifier because OpenCV can perform number plate detection using a pre-trained Haar cascade.  


Number Plate Detection

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.

Then we load our Haar cascade number plate detector.

Next, let's detect the number plates on 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.

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)

The image below shows the result of our Haar cascade number plate detector:

Number plate detection #1

Here, the algorithm successfully detected the car's license plate.

Let's try another image:

Number plate detection #2

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:

Number plate detection false positive

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.



In this tutorial, you learned how to use Haar cascade to detect number plates in images.

You can use other pre-trained Haar cascades to detect other objects like faces, cats, eyes, etc.

If you want to learn more about computer vision and image processing then check out my course Computer Vision with OpenCV and Python.

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.

Previous Article
Learn OpenCV by Building a Document Scanner

Learn OpenCV by Building a Document Scanner

Next Article
Number Plate Recognition with OpenCV and EasyOCR

Number Plate Recognition with OpenCV and EasyOCR


Join the mailing list to be notified about new posts and updates.

Comments 4
Avatar Richard said
C'est trés cool.

Feb. 19, 2022, 2:09 p.m.

Avatar Yacine said
Merci Richard

Feb. 19, 2022, 9:51 p.m.

Avatar NONE said
here cv2.waitkey should be 1 instead of 0.

June 18, 2022, 5:09 a.m.

Avatar Yacine said
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.

Leave a comment

(Your email address will not be published)