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.
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:
pytesseract.pytesseract.tesseract_cmd = 'C:\Program Files\Tesseract-OCR\tesseract.exe'Import the libraries:
import cv2
import imutils
import pytesseractLoad the input image and resize it to a width of 500 px, convert it to grayscale, and apply a bilateral filter to reduce noise:
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)Perform Canny edge detection and find contours:
edged_image = cv2.Canny(gray_image, 30, 200)
contours, _ = cv2.findContours(edged_image.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)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:
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)
breakFinally, read the saved plate image with Tesseract and print the recognized text:
text = pytesseract.image_to_string('./plate.png', lang='eng')
print('License plate is:', text)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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
