Popup

Wait! Don’t Go Yet! 👋

Become a Member Today and Unlock Access to All eBooks! 😍

Thousands of eBooks at your fingertips. Read, learn, and grow anytime, anywhere ✨

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 Plate Recognition) that has an API you can use to identify car plates and car models based on an image.

rts.

  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 has arrived home).

The following image shows how the detection process will work:

project overview

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

  • Hall effect sensor: senses changes in the magnetic field when the car is near;
  • Ultrasonic sensor: detects distance to an object;
  • Active infrared detectors: detect 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 find an authorized car, we’ll trigger an event (that can be opening the garage, for example). The following image contains a flowchart showing the process.

project overview

Top 6

Raspberry Pi eBooks

From Zero to Professional

Raspberry Pi Projects

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:

Parts Required

For this project, you need the following parts:

parts required

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

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

connect camera

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

camera pi take photo node

Schematics

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

identify car schematics

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, which 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:

countries supported openaplr

Note: Europe is not a country, but we assume that it supports 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:

openaplr main page

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

openaplr sk key

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.

node-red simple

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

import nodes

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

flow2

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:

flow2

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/
camera node

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:

flow2

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

node-red post

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 Command field of the cURL POST node and press the Deploy button in your Node-RED software for all the changes to take effect:

Node-RED-deploy-button

Testing the flow

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

car photo f

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

flow2

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 at the car while you take the photo).

debug

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:

debug2

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.

debug3

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 requests OpenALPR. Then, based on the response, it will trigger an event (in this case, we’ll set an output 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.

node-red final

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

import nodes

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

flow 6

Configuring the flow

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

flow_2

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/
camera node

Preparing your OpenALPR Cloud API request

Double-click the cURL POST node:

flow_3

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

node-red post

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.

flow_4

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:

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:

Node-RED-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.

project overview

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.

car photo demo

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. Other sensors 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.

Share your love

🚀 Discover the world of electronics and innovation!

✨ Create, program, and experiment with all your creative ideas with ease.

Spotpear

Leave a Reply

Your email address will not be published. Required fields are marked *

Secure Payments
Securing online payments is a shared responsibility, and everyone can contribute.
Free Shipping
You get unlimited free shipping on eligible items with Ebokify, with no minimum spend.
24/7 Support
Sales gifts are helpful tools often used to show appreciation to clients for their purchase.
Gifts & Sales
Our customer care service is offered in the form of 1st or 2nd level support.