Skip to main content

License Plate Recognition with YOLOv4, OpenCV and Tesseract

Shenzhen, China

This project uses the YOLOv4/Anaconda Setup by The AI Guy:

Github Repository

In this repository you can find a custom function to feed Tesseract OCR the bounding box regions of license plates found by my custom YOLOv4 model in order to read and extract the license plate numbers. Thorough preprocessing is done on the license plate in order to correctly extract the license plate number from the image. The function that is in charge of doing the preprocessing and text extraction is called recognize_plate and can be found in the file core/utils.py.

Activating the Virtual Environment

conda env create -f conda-gpu.yml
conda activate yolov4-gpu

Setting up YOLOv4

Download the License Plate Trained Custom Weight:

https://drive.google.com/file/d/1EUPtbtdF0bjRtNjGv436vDY28EN5DXDH/view?usp=sharing

Copy and paste the custom.weights file into the data folder and create a custom.names in the data/classes/ folder with the following line:

license_plate

To make YOLO use this file, you need to edit the following line in core/config.py to point to your custom.names file:

__C.YOLO.CLASSES              = "./data/classes/custom.names"

We now need to convert this model into the Tensorflow format for YOLOv4:

python save_model.py --weights ./data/custom.weights --output ./checkpoints/custom-416 --input_size 416 --model yolov4 

Tesseract OCR

YOLO helps us to identify license plates in the image and gives us a boundary box that we can cut out with OpenCV. The character recognition is then done by Tesseract.

In order to run Tesseract OCR you must first download the binary files and set them up on your local machine. Please do so before proceeding or commands will not run as expected!

Run License Plate Recognition

All you need to do is add the --plate flag on top of the command to run the custom YOLOv4 model:

python detect.py --weights ./checkpoints/custom-416 --size 416 --model yolov4 --images ./data/images/HK_Taxi.jpg --plate

License Plate #: W0BZK295
License Plate #: HH07194

The command will return the recognized license plates and save the following image to the detections folder:

YOLOv4 License Plate Detection

The model did not cooperate with the local license plate format and I had to Photoshop EU license plates in to get it to work. And the resolution is important - the original image was too large which resulted in incorrect results (Resizing it to 50% resolved the issue).

Count Instances

Run detect.py (for images) or detect_video.py (for videos) with the --count flag to get the number of detected objects:

python detect_video.py --weights ./checkpoints/custom-416 --size 416 --model yolov4 --video ./data/video/license_plate.mp4 --count

YOLOv4 License Plate Detection

Crop Instances

The Crop function within the file core/functions.py can be applied to any detect.py or detect_video.py commands in order to crop the YOLOv4 detections and save them each as their own new image. To crop detections all you need to do is add the --crop flag to any command:

python detect_video.py --weights ./checkpoints/custom-416 --size 416 --model yolov4 --video ./data/video/cars.mp4 --crop

Set the frame interval in detect_video.py:

crop_rate = 150 # capture images every so many frames (ex. crop photos every 150 frames)

YOLOv4 License Plate Detection

The resulting cropped images will be saved within the detections/crop/ folder:

YOLOv4 License Plate Detection

YOLOv4 License Plate Detection