How to extract and recognize the vehicle plate number with Python?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
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:
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:
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:
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:
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.
Related reading
- How to find and crop words into individual images with Python OpenCV?
- How to Fine tune existing Tensorflow Object Detection model to recognize additional classes?
- How to Fine tune existing Tensorflow Object Detection model to recognize additional classes?
- how to fix slow kmeans of opencv
- How to extract and save images from tensorboard event summary?
- How to extract numbers from a string in Python?
- How to freeze lf-net tensorflow model to use it with opencv dnn?
- How to generate .pbtxt file from a .pb file for dnn module in opencv?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.