- Raspberry Pi 3 (3+ and 4 should work too) with its power adapter (ofc)
- Raspberry Pi Camera Module V2 or NoIR Camera V2 (just need to be compatible with the PiCamera library)
- Image of the SD Card (I linked every tutorial/guide that I used)
- optional: PC to control your Pi remotely
- optional: Ethernet connection for the Raspberry Pi, but it’s also possible with just wifi
The reason for all this
For a pretty long time we had a 50 km/h zone in our neighborhood. Only recently it was changed to a 30 km/h zone. Though we always had trouble that people drove way too fast on that street, it looks like many still drive 50 km/h… Some of our neighbours are families that have small kids. Those like to play near the street, like we did when we were young, because we don’t have that much space or any playground nearby.
Now that my parents and I have seen those cars speeding through the area, we only assumed that they were driving +30km/h, we never really knew. So I thought I could measure the speed. Getting a Speed Radar though really isn’t in my budget, nor do I want to stand outside and monitor how fast everyone is driving.
OpenCV and Speed Detection
What I came across on the internet was a library for image processing or for this particular project, object detection. It is called OpenCV and it’s free to use. Searching a bit further, I found multiple projects for Car Speed Detection, which is great, because I’m not really that good in programming and don’t think I would be able to write such a program. I tried multiple ones in C# but it never really was the one I really wanted or I just didn’t work at all. I found a project on the Raspberry Pi so I gave it a try.
CarSpeed-Detection by Greg Barbu
Now the project is programmed by Greg Barbu and I cannot thank him enough for sharing the code. You can find his blog post here: https://gregtinkers.wordpress.com/2016/03/25/car-speed-detector
Before using the program I had to prepare my Raspberry. Install Python and most importantly: OpenCV
I had many problems installing OpenCV, especially because there’s OpenCV4 out and many projects are written for OpenCV3. I couldn’t find a built library of OpenCV4 so I had to build it on my Raspberry itself (that took 3h!). It was a bit of a pain in the ass getting the library to work, but I was able to do so (I used this guide). I had to change only a small part of the code to make it work on OpenCV4.
The program itself is pretty cool, it detects objects passing through the area, which you can define when starting it.
After someone drives through it, an image is taken with the speed and a timestamp. All I have to do is to measure the distance to the street and set the threshold to trigger the camera (I used 0mp/h to test it). We made a few tests, it’s surprisingly accurate.
Additions to the Raspberry
I also built in a little edit in the code so it also shows the km/h. This is the end-result:
This alone is pretty cool but I also installed a few things to the Raspberry that makes transferring files etc a bit easier:
– First off, I added a shared folder which I can access to over my Windows PC. This will make going through the captures a lot easier so I won’t need to use any SSH-connection or similar for that.
– Second I added a script which moves the files captured to the shared folder every hour.
– I also added a shell-script, which can be used to start up the python-program, because it has to be run in a virtual environment and can be tricky. “bash /home/pi/CarSpeed/start_speed_detection.sh”
– When the Pi is booted up, it will automatically start a VNCServer on the Port 5901. You will need to use that VNCServer to define the area where the speed-detection should be activated.
Raspberry Pi Image with OpenCV4 and SpeedDetection
Link(4 Gb): https://mega.nz/#!cJVVDS5Z!ITGKWW6OsIsf0rG1vjsvcqhuMZ-1D6xaBOJkk81BowY
vnc pw: vnc_pi
What you have to do after downloading the Image-File
- Write that Img to your SD-Card using Win32 Disk Imager
- connect the camera module to your Raspberry Pi
- connect a keyboard and mouse (only temporary) to your Pi
- find out IP-Address or change DNS-Name for remote control of the Pi
- connect your Ethernet (recommended) or Wifi
- start the program in the terminal with “bash /home/pi/CarSpeed/start_speed_detection.sh”
Recommendation remotely controlling the Pi
I personally use MobaXterm as the SSH or VNC Client. It’s a small, free and very good all-rounder with no ads whatsoever. There’s even a portable version of it.
I printed out the case for the Raspberry Pi Camera Module, I used these files: https://www.myminifactory.com/object/3d-print-picamera-2-axis-rotating-holder-56190
In my opinion, I can generally recommend MyMiniFactory. They don’t have that many 3D-Files on the site yet but mostly every model is well made and all files are thought to be printed out. Not just to look at a 3D-Model on your PC.
As the case for the Raspberry Pi 3, I already had an official case, but you can pretty much print out any case which is open on top or has a hole for the camera ribbon cable.
I’m thinking about creating a Raspberry Pi Case which also has the PiCamera Case on it. Would be neat if everything would be in one package.