Actively determine speed of objects of any size (be it a vehicle, person, or matchbox car) via a live feed. Then use the object's speed to control GPIO!

Transcript

Hey gang, Tim here at Core Electronics and today we're going to accurately measure the speed of an object using a Raspberry Pi, a camera and machine learning. Cameras in combination with artificial intelligence create arguably the most powerful sensor you can ever put on a Raspberry Pi. By the time I'm done here, we will be able to use the video data coming into the Raspberry Pi to accurately determine the speed of moving objects.

OpenCV, which is a huge resource that helps solve real-time computer vision and imaging processing problems, will be used here to determine what is a moving object. The open source software will then compare two photos and from some trigonometry and known distances infer the speed of that object. Then it's going to take that second compared image, provide it with a timestamp, slapping the file location and speed of the object onto it, save it and upload it to a local network website. These photos can then be accessed through a website from any machine on your local network and stored indefinitely on the Raspberry Pi.

Then having all that under your belt, I'm going to take it a step further and use the recorded speed of the object to control physical hardware. On the table is all the components you're going to need for this system to get it up and running real fast. A Raspberry Pi high quality camera and camera lens, you can also use the Raspberry Pi official camera module version 2, microSD card flashed with Raspberry Pi OS, a power supply, naturally you're also going to want a HDMI cord, mouse, keyboard and monitor to connect to. I am using a Raspberry Pi 4 model B 4 gigabytes for this setup as having the extra computing power that this Pi provides is super helpful for this task. So with the microSD card flashed, insert it into the Raspberry Pi. Make sure to have the Raspberry Pi connected to a monitor.

With the peripheries and Raspberry Pi camera installed, you can power up the Raspberry Pi and see the desktop booted up. From here, you can connect it to the internet. Open up the Raspberry Pi configuration menu found by using the top left menu and scrolling over to the preferences. Make sure that the camera found under the interfaces tab is enabled. If you had to enable this setting, go ahead and reset the Raspberry Pi so that the changes can take effect.

Having done this, it's time to install OpenCV to Raspberry Pi. This is a series of terminal commands that will take some time to complete. Every terminal command to do this is found on the written article, so all you need to do is copy and paste each line. With that complete, you'll have installed OpenCV onto your fresh version of the Raspberry Pi OS.

Next, open up a new terminal using the black button on the top left of the screen. Type and enter the following lines into it to install Docker. The first line is a quick method to download and update packages from all configured sources. Sudo means that it's being run with root privileges. The next line is a client url command line that pulls data from an online server to install the Docker application and in this case we're going to be naming it get-docker.sh. The features that Docker brings is going to be used by the Speed Camera software. The last line is an admin command to install the Docker package to our Raspberry Pi.

Now we're going to install the Speed Camera to our Raspberry Pi system. Open up another terminal command and then type and enter the following into it. The very first line I'm typing is a change directory command.

We are going to use a website get command to download the Speed Camera installation files from a web server. The next line is a two-parter with the first section cmod used to change the access permissions of the file so that anybody can alter these files not just the admin and then the plus x is going to access these files and folders within this directory. Then the last command line is used to run the Speed Installer installation.

The best way to run this software is by typing and entering into a new terminal the following two lines. The first line is a change directory command so we can focus on that freshly installed Speed Camera folder and its contents. The next line is going to run the menu box system which is going to open up this blue menu. Navigate through the options using the arrow keys on your keyboard, press enter to option A and enter on option B to run both the Speed Camera script and the web server script. Now whenever a fast action occurs in front of this camera inside its red capture box it's going to capture the moment and upload it to a web server.

So this web server has the IP address of your Raspberry Pi followed by a semicolon then 8080 which when typed into any local network browser will let you access this particular information. Type and enter into the terminal ifconfig to find your IP address for your Raspberry Pi. At this stage the speed that it stamps to the bottom may not be accurate so let's fix that. There can be a couple of changes that we can do to this particular software so you can get both accurate results and see what is actually occurring in the software if you want to troubleshoot or just google at the system in real time. We're going to access

We are going to navigate with the arrow keys and press enter on option C to open up a new terminal page. In this page, we can navigate around using the arrow keys. To save and lock in changes, press ctrl x y and then the enter key on your keyboard. To enable the live stream video, we need to change the line search underscore gui underscore on to equal true. This will slow down the FPS a little bit, but it will allow us to see the actual speed of the code.

The second step is to adjust the code so it can accurately judge the speed. We need to calibrate the Raspberry Pi for particular distances, so that objects at different depths can get accurate speed measurements. To do this, we need to change the code by going down to calibrate and making it equal true. When saved, all the captured images will look like a camera snapshot with a grid of red lines across it. This will also supply the uncalibrated speed result of the image.

We need to measure the pixel length of the moving object and the distance between the moving object and the camera. In my case, the Matchbox car is 110 pixels long and the distance between the moving object and the camera is about 85 millimetres in real life.

This Matchbox car is about 85 millimetres long. Grab both these values and insert them into the calibration setting. The first one is the pixel length of the moving object when it goes from left to right of the camera and the second one is the object in millimetres for left to right. This is the distance between the moving object and the camera. For my simple setup, I'm just going to use the same values which are the 110 pixels and 85 millimetres long. Control X, Y, Enter, Save. With this saved and the code restarted, the speed values will be significantly more accurate for your situation.

Also in this setting, you can flip the camera to the other side and change the speed values to whatever you need. Another option worth talking about is increasing or decreasing the resolution being captured by this camera. This will enable you to take excellent looking photos but the software is going to be running at a lower fps. Depending on how long your object is staying in the frame, which depends on how fast it's going, this can make for less reliable data. On the other hand, if the resolution is too small then you're going to be losing dimensional accuracy of your fast speed values. Changing the capturing resolution of your camera can be done by accessing the option D setting in the menu box settings.

Let's say a certain object goes past in front of your camera over a certain threshold of speed. When this occurs, I want to be able to control some physical hardware. In this case, I want my Raspberry Pi to turn on an LED. So let's dive into the code, make some simple adjustments and get this running. I'm going to locate and open up the speed cam python file by right-clicking the file location.

I recently set up a speed camera using a Raspberry Pi and Thonny. To do this, I added five lines of code to the top section of the code and two lines further down inside the code in the define speed_camera section. This code initializes the GPIO pin 21 on my Raspberry Pi and sends power through that pin whenever a recorded speed of an object is faster than 0.5 kilometers per hour. I also brought to the table an LED and resistor wired up in series and attached to the ground pin and the GPIO pin 21 of the Raspberry Pi.

When everything was set up, I was able to get the LED to turn on. This LED is just a placeholder for any manner of technology, such as sensors, actuators, or controllers. Huge thanks goes to Claude Pajot for writing this amazing fully-fledged program, which can be found on his Github.

I hope this has shown how you can take a fully-fledged speed camera used for monitoring cars and retrofit it to all kinds of speed monitoring tasks. I hope this has ignited your creative spark and until next time, stay cozy.

Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.