Neighborhood Traffic

I currently live on a corner lot that is near the primary entrance to our neighborhood. As such, I see the occasional speeder and some odd patterns of traffic. Not being able to sit and count cars I sought out the next best thing - an automated traffic counter and speed camera. What is my intent? Simply to satiate curiosity and get some real data to validate/invalidate any forming opinions. Plus, it is a great excuse to buy some hardware and solve a problem.

This project started out with the selection of hardware. Probably a little backwards from normal, but I wanted to pick something cheap and have not had an opportunity to work with the Raspberry Pi 3 yet. I ended up buying the CanaKit from Amazon, since it came with the power supply and a case. The only thing lacking was a camera, which was also available on Amazon. Out the door this cost less than $100.

The first thing I did was get it booting. Unfortunately (or fortunately), the preprogrammed SD requires you to select which OS you want installed. This requires plugging in a keyboard, mouse, and monitor. That means you need something with an HDMI port and a spare USB keyboard and mouse lying around. If you don't, there is probably a way to provision it headlessly by flashing the µSD card. After it was booting, I gave it a static IP on my home router and was able to SSH in. It's certainly nothing pretty, but it is setup in my office like below.

IMG_20180218_080634.jpg

Before I set out on creating any new code I like to take a quick look to see what currently exists. Nobody has time to reinvent the wheel - and if I can leverage existing work I stand to potentially further the overall state of the art. In my search I ran across some Python code and installed that on the Pi3. I calibrated the system with my Jeep, averaging the pixels in each direction. The end result is the local collection of images of vehicles in transit.

speed-20180204-160459.jpg

The above image shows a detected vehicle and the approximate speed. Not being satisfied with having the images locally, I spun up an Amazon Web Services EC2 OwnCloud server to store the images and view them remotely. This presented its own set of problems. First, I installed a quick script to scp the files to the owncloud server. Next step was to have owncloud rescan the directories for files. The script running on the Pi3 remotely calls the rescan script after uploading. I set up a cron job to execute the script every 10 minutes.

Now that I've collected some data I can start on some data analytics. One of the more interesting data points I was looking for was checking when the traffic into and out of the neighborhood was heaviest. The speed-camera software outputs a CSV file with *some* of the information I was looking for. More on the planned improvements later. I used a simple COUNTIF function on the hours of the day to massage the data into buckets and generate the bar chart below.

chart.png

What information can be gleaned from the above? You can see the exodus from the neighborhood between 0700 and 1000. I found it interesting the large number that leave between 0900 and 1000 - a little bit of a late start. There is also a steady flow of traffic into and out of the neighborhood throughout the day.

Where to next?

  • Investigate thermal camera for more consistent imaging. I found a few FLIR options that can interface with the Pi3. The calibration will be interesting though, since I'm unsure of the temperature profile of cars and how it will register with OpenCV.
  • Find a better locale for the camera. The tree branches trigger the motion detection in high winds. Also, once that tree has leaves again, it will have no visibility.
  • Update script output to include time of day in standard format. Right now the CSV has a date string, hour string, and minute string. My preferred output is using the ISO 8601 standard.
  • Migrate away from owncloud. It seems that using a S3 bucket with a different frontend could be more cost effective.
  • Upload entries to this site. Instead of having to use an ec2 instance see about uploading the raw entry (not the picture) to this site in some manner for data analytics.
  • Calibrate the speed better. Some of the readings seem off. I think I need to move the camera further away from the street.
  • Separate R2L and L2R traffic. Analyze when people are coming and going.

That's all for now! More to come as I work on this project.

Adam Markham