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:
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.

