Artificial Intelligence 7 min read

Python License Plate Detection and Recognition Using OpenCV and Tesseract

This tutorial walks through building a Python program that detects and reads vehicle license plates by preprocessing images with OpenCV, extracting contours, isolating the plate region, and applying Tesseract OCR to convert the characters into text, complete with environment setup and code examples.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Python License Plate Detection and Recognition Using OpenCV and Tesseract

The article introduces a practical computer‑vision project: detecting and recognizing vehicle license plates using Python. It explains the required libraries—OpenCV‑Python for image processing, imutils for resizing, and pytesseract (which relies on the Tesseract OCR engine) for character extraction.

First, set up the Python environment and install the dependencies with commands such as pip install opencv-python imutils pytesseract . Then download and install the Tesseract OCR engine, pointing pytesseract to its executable:

<code>pytesseract.pytesseract.tesseract_cmd = 'C:\Program Files\Tesseract-OCR\tesseract.exe'</code>

Import the libraries:

<code>import cv2
import imutils
import pytesseract</code>

Load the input image and resize it to a width of 500 px, convert it to grayscale, and apply a bilateral filter to reduce noise:

<code>original_image = imutils.resize(cv2.imread('image3.jpeg'), width=500)
gray_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2GRAY)
gray_image = cv2.bilateralFilter(gray_image, 11, 17, 17)</code>

Perform Canny edge detection and find contours:

<code>edged_image = cv2.Canny(gray_image, 30, 200)
contours, _ = cv2.findContours(edged_image.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)</code>

Sort contours by area, keep the top 30, and draw them for visual inspection. Filter these contours to locate a quadrilateral that likely represents the license plate, then extract the bounding rectangle and save the cropped plate image:

<code>for c in contours:
    peri = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.018 * peri, True)
    if len(approx) == 4:
        x, y, w, h = cv2.boundingRect(c)
        plate_img = original_image[y:y+h, x:x+w]
        cv2.imwrite('./plate.png', plate_img)
        break</code>

Finally, read the saved plate image with Tesseract and print the recognized text:

<code>text = pytesseract.image_to_string('./plate.png', lang='eng')
print('License plate is:', text)</code>

The program displays intermediate images (original with contours, the cropped plate) and outputs the detected license‑plate characters to the console, demonstrating a complete end‑to‑end solution.

computer visionPythonopencvLicense Plate RecognitionTesseract OCR
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.