Artificial Intelligence 8 min read

Python License Plate Detection and Recognition Using OpenCV and Tesseract

This tutorial walks through creating a Python program that preprocesses an input image, detects and extracts a vehicle license plate using OpenCV, and then reads the plate characters with Tesseract OCR, displaying the recognized text as the final output.

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

License‑plate detection and recognition is widely used in traffic systems, parking lots, and vehicle access control, combining computer‑vision techniques with artificial‑intelligence OCR.

Environment setup : Install the required Python packages with pip:

pip install OpenCV-Python imutils pytesseract

Note that pytesseract depends on the Tesseract OCR engine, which must be installed separately and its executable path set in the script.

Import libraries :

import cv2 import imutils import pytesseract

Configure the Tesseract command path and load the source image:

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

Pre‑processing : Resize the image to a width of 500 px, convert to grayscale, and apply a bilateral filter to reduce 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)

Edge detection using Canny:

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

Contour detection and drawing:

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

Contour filtering : Keep the top 30 contours by area, then draw them.

contours = sorted(contours, key=cv2.contourArea, reverse=True)[:30] cv2.drawContours(original_image.copy(), contours, -1, (0,255,0), 3) cv2.imshow('img2', original_image)

License‑plate candidate selection : Iterate over the filtered contours, approximate each contour, and select the one with exactly four vertices. Crop the bounding rectangle of that contour and save the cropped plate image.

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('./7.png', plate_img) screenCnt = approx break cv2.drawContours(original_image, [screenCnt], -1, (0,255,0), 3) cv2.imshow('detected license plate', original_image)

OCR recognition : Load the saved plate image and use pytesseract.image_to_string to convert the characters to a string.

cropped_License_Plate = './7.png' cv2.imshow('cropped license plate', cv2.imread(cropped_License_Plate)) text = pytesseract.image_to_string(cropped_License_Plate, lang='eng') print('License plate is:', text) cv2.waitKey(0) cv2.destroyAllWindows()

The program prints the recognized license‑plate text to the console, completing the detection‑and‑recognition pipeline.

computer visionimage-processingopencvLicense Plate RecognitionTesseract
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.