Car Plate Recognition System with Raspberry Pi and Node-RED

In this project you’re going to learn how to build a car recognition system using a Raspberry Pi and Node-RED. For this project we’ll be using a software called OpenALPR (Automatic License Place Recognition) that has an API you can use to identify car plates and car models based on an image.

Overview

This project is divided into two parts.

  1. First, we’ll identify a car using OpenALPR and Node-RED;
  2. Then, we’ll trigger an event based on the detected car (for example, open the garage when it detects that your car arrived home);

The following image shows how the detection process will work:

In this example, we use a PIR motion sensor to detect that the car arrives home. There are other sensors that may be more suitable to detect a car, for example:

  • Hall effect sensor: senses changes in magnetic field when the car is near;
  • Ultrasonic sensor: detects distance to an object;
  • Active infrared detectors: detects the presence of an object by detecting the reflection of infrared light.

When the sensor detects motion, the Raspberry Pi camera takes a photo. After that, the Pi sends a request to OpenALPR with the car photo to be identified. Then, the OpenALPR API returns the car details like: plate number, model, color, and the confidence of the results.

After identifying a car, we’ll do some verifications, and if we found an authorized car, we’ll trigger an event (that can be open the garage, for example). The following image contains a flowchart showing the process.

Here’s what happens: after the car has been identified by OpenALPR, we’ll check if the license plate and the car model match. If they match, we’ll check if the car is in the list of authorized vehicles. If it is, we’ll trigger an event. For example: open the garage. After that, we wait a determined period of time until the car enters the garage. Then, you need to add several verifications to check if the car has already entered the garage. If yes, you can close the garage.

Prerequisites:

If you like home automation and you want to learn more about Node-RED, Raspberry Pi, ESP8266 and Arduino, we recommend that you download our course: Build a Home Automation System for $100.

Parts Required

For this project you need the following parts (click the links below to find the best price at Maker Advisor):

Note: at the moment, we don’t have an automatic garage, so we’ll use an LED to mimic the event triggering (we know it is not the same thing, but you get the idea).

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Connect the Raspberry Pi Camera Module

With the Pi shutdown, connect the camera to the Pi CSI port as shown in the following figure. Make sure the camera is connected in the right orientation with the ribbon blue letters facing up (you need to enable the camera in your Raspbian OS).

You should also have Node-RED installed in your Pi and the node-red-contrib-camerapi node Installed:

Schematics

Assemble the circuit to test this project. Follow these next schematic diagram:

After having your Raspberry Pi prepared and Node-RED software configured, you can continue with this project.

OpenALPR

OpenALPR is an open source Automatic License Plate Recognition library written in C++ with bindings in C#, Java, Node.js, Go, and Python. They also have the OpenALPR Cloud API which is a web service running in the cloud that analyzes images of vehicles and responds with license plate, model, color and much more. OpenALPR Cloud API has a free service that allows up to 2000 free recognitions per month.

Note: instead of using their Cloud API that is limited to only 2000 recognitions per month, you can install their Open Source software and use their Python integration to write Python scripts to analyse unlimited images. That way you don’t need to use their cloud service and you aren’t restricted to 2000 requests per month.

Supported countries

OpenALPR contains specialized training data for many plate styles. Currently, OpenALPR supports the following countries:

Note: Europe is not a country, but we assume that they support all countries in Europe. If your country is not listed, you may still experience high accuracy rates by using training data for a country that has plates that look similar to yours.

Creating a free account

To get started with OpenALPR Cloud API, you can create a free account. After completing your account creation, you should have access to the following page:

Open the Cloud API tab to access your Secret Key. You need it to make requests to the API.

In my case, the secret key is: sk_8081041caedd50a———

Save your secret key in a safe place, because you’ll need it in just a moment.

Testing the Camera and OpenALPR Service

First, you should start by identifying your car using the Pi Camera and the OpenALPR service. So, start with the provided sample flow that takes a photo and makes a request to the OpeALPR Cloud API to identify your car. To import the Node-RED flow provided, go to the GitHub repository or click the figure below to see the raw file, and copy the code provided.

Next, in the Node-RED window, at the top right corner, select the menu, and go to Import > Clipboard.

Then, paste the code provided and click Import. The next nodes should show up in your flow:

Configuring the test flow

After importing the flow, you need to make some changes to make it work for you. Open the Take Photo node:

Edit the node to have the same settings as shown in the next figure:

  • File Name: car-photo.jpeg
  • File default path: No
  • File Path: /home/pi/Pictures/

Important: sometimes the previous node might overwrite the default setting. Make sure you double-check that it has the right settings. Then, click the deploy button.

Preparing your OpenALPR Cloud API request

Double-click the cURL POST node:

Then, change the Command field to include your Secret Key and country code:

By default, it should have this command:

sudo curl -X POST "https://api.openalpr.com/v2/recognize?secret_key=YOUR_SECRET_KEY&recognize_vehicle=1&country=YOUR_COUNTRY_CODE&return_image=0&topn=10" -F image=@/home/pi/Pictures/car-photo.jpeg

Add your secret key that you’ve retrieved earlier and your country code:

sudo curl -X POST "https://api.openalpr.com/v2/recognize?secret_key=sk_8081041caedd50a---------&recognize_vehicle=1&country=eu&return_image=0&topn=10" -F image=@/home/pi/Pictures/car-photo.jpeg

Note: EU is not a country, but if you live in any country in Europe, you use the EU country code.

Copy and paste your exact command in the in the Command field of the cURL POST node and press the Deploy button in your Node-RED software for all the changes to take effect:

Testing the flow

After deploying the flow, let’s test the car identification process. Move your car to a place that you can take a photo with your Raspberry Pi:

Point the camera to the car and tap the square next to the timestamp node to trigger the flow.

That should take a photo with your Pi Camera, save it at /home/pi/Pictures/car-photo.jpeg and make a request to the cloud API to identify your car plate and model. It should print the JSON response in the Debug window.

Understanding the response

Open the debug window, and you should see an object with all the details about the JSON response. The results array should have at least one result (array[1]), otherwise it might be analyzing a photo without a car or it couldn’t identify the car (make sure the Pi Camera is pointed to the car while you take the photo).

Click the arrow next to the results: array[1] to expand the object. You should see a plate object with your car plate. Save the car plate in the format retrieved by the API. In my case it’s “61CP–“. You’ll need it later to identify your car:

Expand the vehicle object > make_model > 0: object and you should see the name of your car and how the API identified the car model. In our case it identified the car as a Toyota Yaris with 77,2% confidence. We should save the exact string “toyota_yaris” retrieved by the API, because we need it for the next flow. Save your car model in the format retrieved by the API.

Troubleshooting tips

In case your response returned invalid results, make sure you double check the following details:

  • The Take Photo node is storing the photo in the right file path: /home/pi/Pictures/
  • Make sure the camera is taking good photos (access the Raspberry Pi Pictures folder at /home/pi/Pictures/car-photo.jpeg to see if the photo taken looks good)
  • See if the photo has the car visible and the car plate, otherwise the API might not be able to identify the car properly
  • Make sure you’ve entered the right command with your own API key and country code in the cURL POST command

Car Plate Recognition System Flow

In this part, we’ll add the car detection and event triggering to our flow. When motion is detected, the Pi camera takes a photo with the Pi Camera and makes a request to OpenALPR. Then, based on the response, it will trigger an event (in this case we’ll set an output on to light up an LED).

To import the Node-RED flow provided, go to the GitHub repository or click the figure below to see the raw file, and copy the code provided.

Next, in the Node-RED window, at the top right corner, select the menu, and go to Import > Clipboard.

Then, paste the code provided and click Import. The next nodes should load in your flow:

Configuring the flow

After importing the flow, you need to make some changes to make it work for you. Open the Take Photo node:

Edit the node to have the same settings as shown in the next figure:

  • File Name: car-photo.jpeg
  • File default path: No
  • File Path: /home/pi/Pictures/

Preparing your OpenALPR Cloud API request

Double-click the cURL POST node:

Then, change the Command field to include your Secret Key and country code:

By default, it should have this command:

sudo curl -X POST "https://api.openalpr.com/v2/recognize?secret_key=YOUR_SECRET_KEY&recognize_vehicle=1&country=YOUR_COUNTRY_CODE&return_image=0&topn=10" -F image=@/home/pi/Pictures/car-photo.jpeg

Add your secret key that you’ve retrieved earlier and your country code:

sudo curl -X POST "https://api.openalpr.com/v2/recognize?secret_key=sk_8081041caedd50a---------&recognize_vehicle=1&country=eu&return_image=0&topn=10" -F image=@/home/pi/Pic

Add your car details to the flow

Open the function node Identify Car to add your car’s license plate and model.

You need to replace the carPlate and carModel variables with your own car plate and model name (in the exact same format as retrieved earlier). In our case, we get:

var carPlate = "61CP--";
var carModel = "toyota_yaris";

The following figure shows the place where you should add your car plate and model on the function node:

Note: if you want to add another car plate, you need to edit the “Identify Car” function. Declare another carPlate2 and carModel2:

var carPlate2 = " ";
var carModel2= " ";

Then, after the else if statement, add the following:

else if(msg.payload.results[0].plate==carPlate2 && msg.payload.results[0].vehicle.make_model[0].name==carModel2){
  msg.payload=1;
  global.set("garageOpen","1");
}

That should allow you to verify two cars (you can add more).

Once you’ve added your car details, press Done and click the Deploy button:

Demonstration

Now, when the car arrives, the PIR sensor detects motion, the Pi Camera takes a photo, and the car is identified using the OpenALPR API.

Your Node-RED should identify your car plate and car model. Then, it will trigger an event: it turns GPIO 17 on for a few seconds and then it turns off. This simulates the open/close garage door command.

Wrapping Up

In this project we’ve shown you how to identify car plates using OpenALPR. We also show you how you can trigger an event based on a specific car plate. This way, you can automatically open your garage when you arrive with your car. You can modify this project to make it work for you and use it in your own home automation system.

In the example we’ve shown, we’ve used a PIR motion sensor to detect the car arriving. There are other sensors that might be better for this case like: ultrasonic sensor, active infrared sensor, or hall effect sensor, for example.

When we trigger the event, we wait 10 seconds until “closing the garage”. You need to add several verification processes (that we did not include in the flow) to check if the car has entered the garage or not.

We hope you’ve found this project interesting. If you like this project, you may also like:

Thanks for reading.



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Recommended Resources

Build a Home Automation System from Scratch » With Raspberry Pi, ESP8266, Arduino, and Node-RED.

Home Automation using ESP8266 eBook and video course » Build IoT and home automation projects.

Arduino Step-by-Step Projects » Build 25 Arduino projects with our course, even with no prior experience!

What to Read Next…


Enjoyed this project? Stay updated by subscribing our newsletter!

62 thoughts on “Car Plate Recognition System with Raspberry Pi and Node-RED”

  1. Hi,

    This is an amazing project, thanks for preparing such awesome informative content.
    Will try this project.

    My doubts:
    Can we use web cam for this image recognition.?

    And how can we detect any other things or… like people, for simple access gate.

    Can you provide a tutorial on that.

    Reply
    • Hi.
      Yes, you can use a standard USB cam. But our project is prepared for the Raspberry Pi Camera Module.
      You may need to make some changes to be able to take photos with a USB cam. You may find this info useful: raspberrypi.org/documentation/usage/webcams/README.md
      At the moment, we don’t have any tutorials on image recognition. For that you would need to use something like OpenCV.
      I hope this helps.
      Regards,
      Sara :=

      Reply
  2. Excellent project and thanks for sharing. I will start working on it right away. Much appreciated Rui, keep up the great work.

    Reply
  3. Hi, nice project and well explained.Few questions:
    -Raspberry should always connected at internet?
    -Can you give as the all command to install, or an imagine of your raspberry ready to use?

    Reply
    • Hi Cip! Yes, with this setup the Raspberry Pi needs an Internet connection, because it makes a request to a cloud app, but if you install the OpenAPLR directly on your Raspberry Pi and you create a custom code, you don’t need an Internet connection… github.com/openalpr/openalpr

      Unfortunately I don’t have the full command sequence to make this project, but this project page has all the links with all the information required to make this project work.

      Thanks for reading!
      Rui

      Reply
  4. Is there a way to dynamically use the license plate?

    I want to see the plates and compare them to a list, instead of having it hard-coded.

    Car A comes up, isn’t on the list, nothing happens. Car B shows up, that one is on the list, gets in. So do C, D, E, F and G, but not H, I, J, K, L, M…

    Reply
  5. hi, first of all this is very helpful. also i am going to work on this project as my final year project. i am a student of bachelor. it would be great help if i can contact you directly somewhere either facebook or something as i have still many queries about from where exactly to begin with. please.

    Reply
    • Hi Safia.
      If you have any questions regarding our projects you can post them in the comments section of our projects.
      Regards,
      Sara 🙂

      Reply
  6. This tutorial is very helpful for us. I would like to ask what language did you used in this project and what part of project still needed a code for this? In the list of authorized vehicles can we use a database for matching the processed plate number with our database of authorized vehicles?

    Reply
      • Thanks Sara, May I know the process of Cloud API of OpenALPR. I already read about it in their website but I want more for me to visualize it. If you used that Cloud API and sending the image up to that API it will return a JSON in your application right? Because I saw in their demo browser you need to put the picture manually but in the integrated api it will become automatically after capturing the vehicle and send it to api not like the demo browser?

        Reply
      • We are going to use also the python, but with another approach in capturing the image of a vehicle. Since, you said that python integration can be used for unlimited processing of a plate number. I want to know also what is their Open Source Software what was the purpose of that software? I want to gather an information because, we haven’t started yet our project because we are finalizing the camera that we are going to buy also the approach on how are we going to capture/detect the vehicle.

        Reply
          • Hi, Sara

            This is what you said.

            Note: instead of using their Cloud API that is limited to only 2000 recognitions per month, you can install their “Open Source software and use their Python integration to write Python scripts to analyse unlimited images”. That way you don’t need to use their cloud service and you aren’t restricted to 2000 requests per month.

            You said in this note that we can use “Python Integration” To make our system analyze unlimited images. And also you said that we can install the “Open Source Software” of OpenALPR, I am curious what was like their software and what was the features of it? Sorry sara, I want to learn and gather information thank you!

            Nigel

          • Hi Nigel.
            Our project uses their Cloud API, so in our example we’re limited to 2000 recognitions.
            But you can use their Python integration and write your own code. I recommend reading more about this on the following links:
            – github.com/openalpr/openalpr
            – github.com/openalpr/openalpr/wiki/Integrating-OpenALPR#python
            Regards,
            Sara

          • Hi Sara

            Thank you sara for replying and for helping me. I would like to ask what was that Open Source Software of OpenALPR? How it helps my project?

            I am confused on what to use the camera, am I going to use Ip camera with LPR or just an IP camera hoping with a software that I can connect with my IP camera to capture plate numbers. Thank you

            P.s – Can I have conversation with you? Maybe thru FB so that I can discuss my project to you and I would appreciate your help in me very much.

            Nigel

          • Hi again Nigel.
            There are two versions of the software.
            1) You can download the open-source version and install it in your Raspberry Pi, for example.
            2) You can use their online API, which is limited.

            We don’t have any more tutorials on that subject.
            I recommend reading their documentation.

            Regards,
            Sara

    • Hi Erik. One of the easiest ways is to add the license plates on the “Identify Car” function node.
      Other solution is using a data base to store multiple license plates if you have lots of them (we don’t any tutorial about that).
      Regards,
      Sara 🙂

      Reply
  7. Hallo Sara,
    I’m in advanced age, I can not program Rasbery but I would like to buy a finished set of this project even with programmed into that rasbery. Is anyone willing to build this project and as a kit to offer for sale ???
    friendly salut pavel
    thanks pavel

    Reply
    • Hi Pavel.
      I’m sorry, but we don’t sell finished products.
      However, if there’s anyone here willing to do that, just reply to this message.
      Regards,
      Sara 🙂

      Reply
  8. Hello Sara, great project! I want to deploy this project at my office. How to make RP camera take a photo (or a video) 24/7 and recognize alpr automatically without motion sensor. Thank you

    Reply
  9. “Hi Erik. One of the easiest ways is to add the license plates on the “Identify Car” function node.”

    But how.. multiple lines with var carPlate = are not working..

    Can you give me a quick example?

    Reply
  10. Hi,
    How do i get only the License plate number and not all the other information when the number plate is recognized?
    I just want to extract the license plate.
    Thank you

    Reply
    • Hi Prajwal.
      I think the API returns all those information by default.
      After you receive the information you can use only the pieces you want.
      Regards,
      Sara

      Reply
  11. Hi,Sara! what’s happend when some person use my licens plate? I think I still have to work on security..your project is very nice and like it (Y)

    Reply
    • Hi shahed.
      This project is programmed to turn an LED on when it detectes the correct car plate.
      You just have to replace the LED with the relay on the circuit.
      (The relay will probably work with inverted logic, so you need to send a LOW signal to turn it on and an HIGH signal to turn it off – you just have to change this on the node-red flow)
      Regards,
      Sara

      Reply
  12. Hi RNT’s
    Error : unexpected end of JSON

    I’m having this error in the second system flow . It’s not forwarding to “Identify car” node
    Help me with solution.

    Reply
  13. Hi Sara,
    Thank you so much for this tutorial.
    Is the number plate content specific to number plates only or does it extract numbers from any kind of number source?
    For Example: If I use a paper that has numbers on it instead of a number plate can it read the numbers and put the value in the array?

    Reply
    • Hi Mo.
      I haven’t experimented with that. So, I can’t say if it will work or not.
      You have to experiment yourself.
      Regards,
      Sara

      Reply
  14. How would i go about recording the number plate entries to a database and comparing those to another database, eg scanning plates to check for wanted vehicles?

    Reply
  15. Hello Sara.
    Thanks for this tutorial. I will start it soon, I am buying the parts.
    I wonder if it needs to be the Raspberry pi 3 B+ model or if it can be the raspberry pi 3 B model? Is a program change required to use B?
    Thanks again.

    Reply
  16. Very interesting project.
    Thank you for sharing this amazing and informative project.
    Can’t wait to try it out myself.

    Reply
  17. I Have a problem in my Raspberry pi Alpr is not Supporting Plzz Help me to solve This. And Showing This Error Traceback (most recent call last):
    File “/home/pi/anpr.py”, line 1, in
    from openalpr import Alpr
    ModuleNotFoundError: No module named ‘openalpr’

    Reply
  18. Hi,

    Just found this great article. So what do I need to do for 2 things.

    I have an ultrasonic sensor. What needs to change and how to connect it?
    How can I integrate it so it pings a url that will open my garage door (myq)

    Thank!

    Reply
  19. Hello,
    I hope you read your comment in 2022 !
    I was able to recognize my car as well as my plate
    however, in the last part, when I insert the flow, it says “unknow: rpi-gpio in” and “unknow: rpi-gpio out” can you help me please?
    i think it’s module or something in node-red…
    thx

    Reply
  20. Hello, when you have finished registering, go to the site, log in and change the URL to the one in the image of the site

    Reply
  21. As of 11/1/2024, OpenALPR doesn’t seem to be offering a free tier, but the techniques in this tutorial are still quite useful for generating ideas!

    Reply

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.