License Number Plate Recognition & Extraction with OpenCV

FREE Online Courses: Dive into Knowledge for Free. Learn More!

In today’s world, Automatic number plate recognition is one of the most common computer vision tasks. The application of automatic number plate recognition varies depending on the area of use.

Some use cases are:

  • Toll ticket collection.
  • Stolen car detection.
  • Traffic monitoring system.
  • Smart parking.

In this tutorial, we’re going to make a basic automatic number plate recognizer using OpenCV and python. And after recognizing the number plate we’ll use EasyOCR to extract the vehicle number from the number plate.

What is OpenCV?

OpenCV is a Computer Vision library built on C/C++. But OpenCV is available for python also. It is widely used for image processing and real-time computer vision applications. We can perform almost every image processing task using OpenCV because it has more than 2500 image processing algorithms inbuilt.

What is EasyOCR?

EasyOCR is an OCR package for python. OCR stands for Optical Character Recognition. OCR is a technique to extract/detect text from various sources of fields, such as image, pdf, etc. Using the EasyOCR package we can perform text extraction very easily with python.

license recognition ocr

For our project, we’ll use it to extract the vehicle number from the detected number plate.

How are we going to detect the number plate?

We’ll use a Haar Cascade classifier to detect the number plate. Haar Cascade is a feature-based object detection technique. It matches approx 16000 features in an image to identify a target object.

Object detection is identifying an object located in an image.

haar features

Initially, it needs lots of positive images (Number plate images) and negative images (images that don’t contain a number plate) to train the algorithm.

We’ll use a pre-trained number plate recognizer haar cascade to identify a number plate in a given image.

Prerequisites:

  • Python 3.x (we used 3.7.10 for this project)
  • EasyOCR – 1.4
  • OpenCV – 4.5

Install all the required packages by using pip (pip install package-name).

Download License Number Plate Recognition Project Code

Please download the source code of license number plate recognition with opencv: License Number Plate Recognition Project Code

Steps to solve the Number Plate Recognition project:

1. Import necessary packages and initialize the classifier.
2. Detect Number plate from an image.
3. Extract text from the detected number plate.

Step 1 – Import necessary packages and initialize the classifier:

# ProjectGurukul Number plate recognition
# import necessary packages

import cv2
import easyocr
  • In this step, we’ve imported the OpenCV module as cv2 and EasyOCR.
# initialize cascade classifier
numberPlate_cascade = "numberplate_haarcade.xml"
detector = cv2.CascadeClassifier(numberPlate_cascade)
  • Using cv2.CascadeClassifier we load the classifier model.

Step 2 – Detect Number plate from an image:

# read image
img = cv2.imread('image.jpg')
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  • First, using the cv2.imread() function we read an image from a local path.
  • Haar cascade works with Grayscale images, so using the cv2.cvtColor() function we convert the image to a grayscale image.
#-- Detect Number plates
plates = detector.detectMultiScale(
    	img_gray,scaleFactor=1.05, minNeighbors=7,
    	minSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE)
print(plates)
  • detector.detectMultiScale() takes an image as input and returns a list containing the location of detected objects in the input image.

Output:
[[226 324 185 62]]

We can see that one number plate is detected. So, let’s draw the bounding box over the detected number plate.

# iterate through each detected number plates
for (x,y,w,h) in plates:
    
    # draw bounding box
    cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)

    # Crop the numberplate
    plateROI = img_gray[y:y+h,x:x+w]
    cv2.imshow("Numberplate", plateROI)
    
# Show the final output
cv2.imshow('Output', img)

# wait until  any key is pressed
cv2.waitKey(0)
  • cv2.rectangle draws a rectangle in an image.
  • Then we crop the number plate region because we’ll need it to extract the text from the number plate.

Output:

license number plate output

We’ve successfully detected the number plate, now the last and final step,

Step 3 – Extract text from the detected number plate:

# initialize the easyocr Reader object
reader = easyocr.Reader(['en'])

# detect text
text = reader.readtext(plateROI)

    if len(text) == 0:
    		continue
    print(text)
    print(text[0][1])

    # draw text in the frame
    cv2.putText(img, text[0][1], (x, y-5), cv2.FONT_HERSHEY_COMPLEX, 0.8, 
(0, 255, 0), 2)
# Show the final output
cv2.imshow('Output', img)

# wait until  any key is pressed
cv2.waitKey(0)
  • We defined an easyocr Reader object. We used easyocr.Reader([‘en’]) to detect English language.
  • reader.readtext() reads texts from the input image.
  • Using cv2.putText() we can draw text on an image.

OpenCV Number Plate Recognition Output

KL54 A.2670

opencv number plate recognition output

Summary:

In this project, we’ve built an automatic license plate recognition system using OpenCV – python, and EasyOCR. Through this project, we’ve learned about Haar cascade object detection, Character recognition, and some basic image processing techniques.

Your opinion matters
Please write your valuable feedback about ProjectGurukul on Google | Facebook

1 Response

  1. Bryan Felton says:

    Hello I need some help yesterday I have accident and the other party left hit and run . I do have dash cam that I got the video of the accident . But I was snowing and can’t read the plate to give to the cop . Can u help extract it please help

Leave a Reply

Your email address will not be published. Required fields are marked *