Python
Vehicle Plate Recognition
OpenCV
Image Processing
Computer Vision

How to extract and recognize the vehicle plate number with Python?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Vehicle plate recognition is usually a two-stage pipeline: detect the plate region first, then run OCR on that cropped plate. In Python, OpenCV is a common choice for image preprocessing and rough plate localization, while Tesseract is a common baseline OCR engine.

That combination is good for prototypes and controlled images. For noisy traffic scenes or difficult camera angles, a learned detector and a stronger OCR model are often necessary.

Preprocess the Vehicle Image

OCR works better when the image has good contrast and reduced noise. A basic preprocessing pipeline might look like this:

python
1import cv2
2
3def preprocess(image_path: str):
4    image = cv2.imread(image_path)
5    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
6    blurred = cv2.bilateralFilter(gray, 11, 17, 17)
7    edges = cv2.Canny(blurred, 30, 200)
8    return image, gray, edges
9
10image, gray, edges = preprocess("car.jpg")

The bilateral filter helps reduce noise while preserving edges, which improves contour-based detection in many simple examples.

Find a Candidate Plate Region

One basic strategy is to look for rectangular contours:

python
1import cv2
2
3def detect_plate_region(image, edges):
4    contours, _ = cv2.findContours(edges.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
5    contours = sorted(contours, key=cv2.contourArea, reverse=True)[:10]
6
7    for contour in contours:
8        perimeter = cv2.arcLength(contour, True)
9        approx = cv2.approxPolyDP(contour, 0.02 * perimeter, True)
10
11        if len(approx) == 4:
12            x, y, w, h = cv2.boundingRect(approx)
13            return image[y:y + h, x:x + w]
14
15    return None
16
17plate_roi = detect_plate_region(image, edges)

This is not a production-grade plate detector, but it works reasonably well on front-facing, well-lit examples where the plate boundary is visible.

Run OCR on the Plate Crop

Once the plate region is isolated, preprocess it again and pass it to Tesseract:

python
1import cv2
2import pytesseract
3
4def read_plate_text(plate_roi):
5    gray_plate = cv2.cvtColor(plate_roi, cv2.COLOR_BGR2GRAY)
6    _, thresh = cv2.threshold(gray_plate, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
7
8    config = "--psm 7 -c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
9    text = pytesseract.image_to_string(thresh, config=config)
10    return "".join(ch for ch in text if ch.isalnum())

The --psm 7 mode tells Tesseract to treat the image as a single text line, which fits most license plates better than general page-layout modes.

Put the Pipeline Together

An end-to-end prototype can look like this:

python
1def extract_plate_number(image_path: str):
2    image, gray, edges = preprocess(image_path)
3    plate_roi = detect_plate_region(image, edges)
4
5    if plate_roi is None:
6        return None
7
8    return read_plate_text(plate_roi)
9
10plate_number = extract_plate_number("car.jpg")
11print("plate:", plate_number)

This is a solid baseline for learning, debugging, or evaluating whether your camera setup is good enough for the task.

Know the Limits of Heuristic Detection

Contour-based plate detection works best when:

  • the plate is roughly front-facing
  • motion blur is low
  • the plate occupies enough pixels
  • lighting is stable
  • the background does not resemble the plate strongly

If those assumptions fail, the contour stage may crop the wrong region or miss the plate completely. In harder environments, a trained detector such as YOLO or another object-detection model is more robust.

Common Pitfalls

  • Running OCR on the full vehicle image instead of isolating the plate first.
  • Using contour detection on heavily rotated or low-contrast images and expecting reliable results.
  • Skipping OCR-specific preprocessing on the cropped plate region.
  • Forgetting to restrict Tesseract to likely plate characters.
  • Treating a simple OpenCV plus Tesseract prototype as production-ready for difficult traffic footage.

Summary

  • Plate recognition is usually detection first, OCR second.
  • OpenCV preprocessing helps both contour detection and OCR quality.
  • Tesseract works better on a clean, cropped, thresholded plate image.
  • A contour-based detector is fine for prototypes and controlled conditions.
  • For harder scenes, move from handcrafted heuristics to trained detection and recognition models.

Course illustration
Course illustration

All Rights Reserved.