License Plate Recognition using OpenCV Python

Updated: Sep 9

Hello readers, I am back with another computer vision project. In this project, we are going to learn about how we can do license plate recognition with Python.

Requirements of the project

  • CV2 (OpenCV)

  • OCR (Pytesserect)

  • Numpy

  • Russian Haar cascade

Now, we need to divide this work into steps.

  • The first task is to read Image with cv2.

  • Find the number plate using haar cascade from the image

  • The crop that detected number plate.

  • Now we need to do image processing on the cropped number plate.

  • Feed that processed Image to OCR engine, it will give the reading of plate.

  • We need to display state according to the reading (If the first 2 chars are GJ, then it is from Gujarat)

So let's start the code.


We need to initialize the needed libraries first. We need to give the Pytesseract path where it is installed in the system.

import cv2
import pytesseract
import numpy as np

pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'

Now, we need to read the cascade file, and we need to create a dictionary according to the state-wise.


cascade= cv2.CascadeClassifier("haarcascade_russian_plate_number.xml")
states={"AN":"Andaman and Nicobar","AP":"Andhra Pradesh","AR":"Arunachal Pradesh","AS":"Assam","BR":"Bihar","CH":"Chandigarh","DN":"Dadra and Nagar Haveli","DD":"Daman and Diu","DL":"Delhi","GA":"Goa","GJ":"Gujarat",
"HR":"Haryana","HP":"Himachal Pradesh","JK":"Jammu and Kashmir","KA":"Karnataka","KL":"Kerala","LD":"Lakshadweep","MP":"Madhya Pradesh","MH":"Maharashtra","MN":"Manipur","ML":"Meghalaya","MZ":"Mizoram","NL":"Nagaland","OD":"Odissa","PY":"Pondicherry","PN":"Punjab","RJ":"Rajasthan","SK":"Sikkim","TN":"TamilNadu","TR":"Tripura","UP":"Uttar Pradesh", "WB":"West Bengal","CG":"Chhattisgarh","TS":"Telangana","JH":"Jharkhand","UK":"Uttarakhand"}

Now, it's time for the most exciting part, we need to define one function, which will take the Image as an input and it will give the result.



def extract_num(img_name):
    img = cv2.imread(img_name) ## Reading Image
    # Converting into Gray
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 
    # Detecting plate
    nplate = cascade.detectMultiScale(gray,1.1,4)
    for (x,y,w,h) in nplate:
        # Crop a portion of plate
        a,b = (int(0.02*img.shape[0]), int(0.025*img.shape[1]))
        plate = img[y+a:y+h-a, x+b:x+w-b, :]
        # make image more darker to identify the LPR
        ## iMAGE PROCESSING
        kernel = np.ones((1, 1), np.uint8)
        plate = cv2.dilate(plate, kernel, iterations=1)
        plate = cv2.erode(plate, kernel, iterations=1)
        plate_gray = cv2.cvtColor(plate,cv2.COLOR_BGR2GRAY)
        (thresh, plate) = cv2.threshold(plate_gray, 127, 255, cv2.THRESH_BINARY)
        # Feed Image to OCR engine
        read = pytesseract.image_to_string(plate)
        read = ''.join(e for e in read if e.isalnum())
        print(read)
        stat = read[0:2]
        try:
        # Fetch the State information
            print('Car Belongs to',states[stat])
        except:
            print('State not recognised!!')
        print(read)
        cv2.rectangle(img, (x,y), (x+w, y+h), (51,51,255), 2)
        cv2.rectangle(img, (x, y - 40), (x + w, y),(51,51,255) , -1)
        cv2.putText(img,read, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)
        cv2.imshow('PLate',plate)
        # Save & display result image
        cv2.imwrite('plate.jpg', plate)

    cv2.imshow("Result", img)
    cv2.imwrite('result.jpg',img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

# Let's make a function call
extract_num('./test_images/t29.jpg')

Let's see the output

Car Belongs to Delhi

And here is our cropped number plate

If you want to see a more clear explanation of this project, see the tutorial below.

Full GUI version of this project - Buy Now




Thanks, guys for reading till last, so guys see you in the next article with code & tutorial.


Donation

Here we are just asking for the donations, if you want to donate us something then my UPI id and Paypal ID are mentioned below. We are creating quality content on youtube and making blogs for the same and publishing code openly. We are doing an open-source contribution.


UPI ID - kushalbhavsar58@ybl

Paypal - https://paypal.me/spidy1820

Wise - kushalbhavsar58@gmail.com


Note - We are just asking for donations.


Thanks & Regards

Machine Learning Hub