DEV Community

Jeremy
Jeremy

Posted on • Edited on

OpenCV in Python for End-to-end License Plate Detection

Abstract

The following outlines a multi-step workflow to capture images of cars driving by on the highway, read license plates, and write those to a local database. Each step in the workflow has a dedicated article and how-to. Feel free to hook in wherever!

Background

This article details how I built my own license plate detection rig with the idea that I can help you build something similar. This workflow operates in real time on a raspberry pi 4b connected to a Canon DSLR, with testing and iterating on my MacBook. The camera looks down on the i5 northbound highway in our Seattle condo. I share how the pipeline works end-to-end from capture to detection to ocr and storage. Code is shared and photo/resources for reproducing on your end, as well as outlining challenges I encountered in case you have similar problems.

Labeled highway cars

Labeled license plate

It’s a weird but fun build, with no intended business application. I thought maybe it would be neat to build a query mechanism for amber alerts - get an alert, then check if/when license was last observed. But just a toy idea. And really I just wanted a hobby project to build after studying computer vision in university – a project that would be fun to enjoy tinkering around.

As of this writing, the workflow is ~90% complete. Only OCR is not working reliably. Each other step has gone through evolutions from ideation to production. For example, the camera rig originally started as a raspberry pi HQ camera. But iterations later, it was clear that a raspberry pi HQ camera could not capture high-speed cars in detail. Instead, an EOS DSLR with 300mm lens proved better. :) This is a journey! Hopefully my experience can help some other folks interested in similar concepts - and so I detail here what I have accomplished.

Step-by-step

License plate detection workflow visualized

  1. Overview: OpenCV in Python for End-to-end License Plate Detection.
  2. Camera and computer setup. Raspberry pi (RPi), Canon DSLR, f-stop and ISO.
  3. Capturing images from DSLR to RPi. Automating capture of images and transfer to RPi.
  4. Model for detecting cars. Train model from scratch using YOLO and labeled images.
  5. Crop bounding box + rotate.
  6. TBD -- Model for finding license plates. Train a second model with YOLO.
  7. TBD -- Crop bounding box.
  8. TBD -- Read license plate with OCR. Pre-process image, and extract text with Paddle OCR.

Each article is self-contained. If you’re only interested in building and training your own CV model, then you can read just article (3). If you’re interested in how to set up a camera to capture tack-sharp images of highway speed cars, then you can read just article (1). If you want to build a whole system end-to-end, then this series should help. Have fun!

Next Link: Camera and computer setup

Top comments (0)