Developer stories to your inbox.

Subscribe to the Developer Digest, a monthly dose of all things code.

You may unsubscribe at any time using the unsubscribe link in the digest email. See our privacy policy for more information.

alwaysAI Ad

How to Build a License Plate Reader

By Eshaan Kansagara Sep 16, 2020

In this tutorial, we will cover how to create an app that will read license plates using the license plate detection model from alwasyAI and the optical recognition software EasyOCR

streamer

In order to successfully complete this tutorial, you will need:

  1. An alwaysAI account (it's free!)
  2. alwaysAI CLI downloaded on your machine (also free)
  3. Any IDE or text editor that runs Python

Please see the alwaysAI blog for additional tutorials, tips for computer vision development and more. The finished code from this tutorial is available on GitHub.

For this app, we will be modifying the License Plate Tracker app. To complete this tutorial, first clone the code associated with the license plate tracker app, which can be found here. From within the newly cloned directory, run 

aai app configure

and follow the prompts in the terminal. For this version of the app, you will need to deploy locally, as PyTorch does not work out of the box on ARM 64 architectures. Now you're ready to start!

In this app, we will be modifying two things:

  1. app.py
  2. requirements.txt

App.py

Once you have the code imported from the License Plate Tracker App (which can be found here), we will begin to modify the app.py.

  1. First we will need to import two libraries - Pillow and easyOCR. This will be added under the other imports.
import easyocr
  1. We will then need to create a variable that will load in the model into the memory. In order to do this, we will need to add piece of code to line #35:
reader = easyocr.Reader(['en'], gpu=True) 

NOTE: in case you do not have a GPU or it has low memory, you may set the gpu=False.

      2. Now we will look at adding the code that will read the license plate.
               a. First we will need to detect the license plate and create a cutout image of it (this will produce a numpy array of the image):

    if(prediction.label == "license_plate"):
     license_image = edgeiq.cutout_image(
         frame, prediction.box)

      3. Now that we have the license isolated, we can run it through the easyOCR method, which will take in a numpy array as well as the message format as parameters:

    try:
output = reader.readtext(license_image, detail=0)
print(f'{prediction.label}_{number}: {output}')
text.append('{}_{} reads: {}'.format(prediction.label, number, output))

except Exception as e:
print(f'app.py: Error processing image through OCR lib: ERROR: {e}')

Put together, the code should look like this:

if(prediction.label == "license_plate"):
license_image = edgeiq.cutout_image(
         frame, prediction.box)
try:
output = reader.readtext(license_image, detail=0)
print(f'{prediction.label}_{number}: {output}')
text.append('{}_{} reads: {}'.format(prediction.label, number, output))

except Exception as e:
print(f'app.py: Error processing image through OCR lib: ERROR: {e}')

Requirements.txt

The requirements.txt will look like this:

-f https://download.pytorch.org/whl/torch_stable.html
easyocr
torch==1.4.0
torchvision==0.5.0

Before running, make sure to uncomment the set of dependencies that match the device you are working on.

After that, we are all done!

Now all we have to do is build the app and run it. You should then see an output similar to the image at the beginning as well as console output similar to the image below:

license-plate

Sign up for an account to get started with computer vision today! For ideas on how to take your app to the next level, visit the alwaysAI blog. If you want even more control over your application, you can train your own license plate detection model to detect different types of vehicles and license plates!

Contributions to the article made by Jason Koo and Lila Mullany

Get Started Now
We are providing professional developers with a simple and easy-to-use platform to build and deploy computer vision applications on edge devices.
Get Started Now
By Eshaan Kansagara Sep 16, 2020