It smells so good! Detect and recognize license plates with Python (with code)

License plate detection and recognition technology is widely used and can be used in road systems, ticketless parking lots, vehicle access control, etc. The technology combines computer vision and artificial intelligence.

This article will create a license plate detection and recognition program using Python. The program processes the input image, detects and recognizes the license plate, and finally displays the license plate characters as the output.

/ 01 /

Create a Python environment

To follow this tutorial with ease, you need to be familiar with Python basics. The program environment should be created first.

Before you can start programming, you need to install a few libraries in your environment. Open any Python IDE, create a Python file. Run the command on the terminal to install the appropriate library. You should have Python PIP pre-installed on your computer.

  • OpenCV-Python: You will use this library to preprocess input images and display individual output images.

    pipinstallOpenCV-Python

  • imutils: You will use this library to crop the original input image to the desired width.

    pipinstallimutils

  • pytesseract: You will use this library to extract license plate characters and convert them into strings.

    pipinstallpytesseract

    The pytesseract library relies on the Tesseract OCR engine for character recognition.

/ 02 /

how on your computer

Install Tesseract OCR?

Tesseract OCR is an engine that can recognize language characters. Before using the pytesseract library, you should have it installed on your computer. Proceed as follows:

1. Open any Chrome-based browser.

2. Download the Tesseract OCR installer.

3. Run the installer and install it like any other program.

After preparing the environment and installing tesseract OCR, you can write programs.

import library

First import the libraries installed in the environment. Import libraries allow you to call and use their functions in your project.

  • importcv2

  • importimutils

  • importpytesseract

You need to import the OpenCV-Python library as cv2. Import other libraries with the same names as they were installed.

get input

Then point pytesseract to where the Tesseract engine is installed. Use the cv2.imread function to take the car image as input. Replace the image name with the name of the image you are using. Store images in the same folder as your project for easy manipulation.

pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'
original_image = cv2.imread('image3.jpeg')

Swipe left and right to view the complete code

You can replace the input image below with the one you want to use.

Preprocess input

Resize the image width to 500 pixels, and then convert the image to a grayscale image, because the canny edge detection function only works on grayscale images. Finally, the bilateralFilter function is called to reduce image noise.

original_image = imutils.resize(original_image, width=500 )
gray_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2GRAY)
gray_image = cv2.bilateralFilter(gray_image, 11, 17, 17)

Swipe left and right to view the complete code

Detect license plate on input

Detecting license plates is the process of determining which part of the car has the license plate characters on it.

(1) Perform edge detection

First call the cv2.Canny function, which can automatically detect edges on the preprocessed image.

edged_image = cv2.Canny(gray_image, 30,200)

We will find the contour through these edges.

(2) Find the contour

Call the cv2.findContours function, passing a copy of the edge image. This function will detect contours. Use the cv2.drawContours function to draw the detected contours on the original image. Finally, output the original image with all visible contours drawn.

contours, new = cv2.findContours(edged_image.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
img1 = original_image.copy()
cv2.drawContours(img1, contours, -1, (0, 255, 0), 3)
cv2.imshow("img1", img1)

Swipe left and right to view the complete code

The program draws all the contours it finds on the car image.

After you find contours, you need to sift through them to identify the best contour candidates.

(3) Filter profile

Contours are filtered based on a minimum area of ​​30. Contours smaller than this area are ignored as they are unlikely to be license plate contours. Duplicate the original image, draw the first 30 contours on the image. Finally, display the image.

contours = sorted(contours, key = cv2.contourArea, reverse = True)[:30]
# stores the license plate contour
screenCnt = None
img2 = original_image.copy()

# draws top 30 contours
cv2.drawContours(img2, contours, -1, (0, 255, 0), 3)
cv2.imshow("img2", img2)

Swipe left and right to view the complete code

There are now fewer contours than at the beginning. The only contours drawn are those that approximately contain license plates.

Finally, you need to iterate through the filtered contours to determine which one is the license plate.

(4) Traverse the first 30 contours

Create a for loop that iterates over the contours. Find a contour with four corners, determine its perimeter and coordinates. An image containing the outline of the license plate is stored. Finally, the outline of the license plate is drawn on the original image and displayed.

count = 0
idx = 7

for c in contours:
    # approximate the license plate contour
    contour_perimeter = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.018 * contour_perimeter, True)

    # Look for contours with 4 corners
    if len(approx) == 4:
        screenCnt = approx

        # find the coordinates of the license plate contour
        x, y, w, h = cv2.boundingRect(c)
        new_img = original_image [ y: y + h, x: x + w]

        # stores the new image
        cv2.imwrite('./'+str(idx)+'.png',new_img)
        idx += 1
        break

# draws the license plate contour on original image
cv2.drawContours(original_image , [screenCnt], -1, (0, 255, 0), 3)
cv2.imshow("detected license plate", original_image )

Swipe left and right to view the complete code

After the loop, the program has identified the contour containing the license plate.

Recognize detected license plates

Recognizing a license plate means reading the characters on a cropped image of the license plate. Load a previously stored license plate image and display it. Then, call the pytesseract.image_to_string function, passing the cropped license plate image. This function converts the characters in the image into a string.

# filename of the cropped license plate image
cropped_License_Plate = './7.png'
cv2.imshow("cropped license plate", cv2.imread(cropped_License_Plate))

# converts the license plate characters to string
text = pytesseract.image_to_string(cropped_License_Plate, lang='eng')

Swipe left and right to view the complete code

The clipped license plate is shown below. The characters above will be what you output on the screen later.

After the license plate is detected and recognized, you can display the output.

display output

This is the last step. You output the extracted text to the screen. The text contains license plate characters.

print("License plate is:", text)
cv2.waitKey(0)
cv2.destroyAllWindows()

The expected output of the program should look like this:

The license plate text can be seen on the terminal.

/ 03 /

Sharpen your Python skills

Detecting and recognizing license plates with Python is an interesting project. It's challenging, so it should help you learn more about Python.

When it comes to programming, practical application is the key to mastering a language. In order to develop your skills, you need to develop interesting projects.

Original link:

https://www.makeuseof.com/python-car-license-plates-detect-and-recognize/

Guess you like

Origin blog.csdn.net/y1282037271/article/details/129008969