Parking Space Detection with OpenCV and a Raspberry Pi

Eli Fatsi, Former Development Director

Article Category: #Code

Posted on


Here in the Viget Boulder office, there are anywhere from 10-16 people in and out on a given day. That plus the fact that we only have one available parking spot leads to a few small problems: who gets the spot? Is anyone in it at the moment? When will it be free if someone is in it? While parking spaces are only few blocks from the office, we took these problems as an oportunity to play around with some new technology.

Step #1 - Use Existing Technology

We all make use of Google Calendar to make our days easier and more organized, so we creating a “Room” that represents the parking spot answers the question “Who gets the spot?” most of the time. We claim no bragging rights on that solution, pretty run-of-the-mill-stuff.

Step #2 - Innovate A Bit

If you’re on the go, Google Calendar might not always be available, and it’s not uncommon for the “Room” to be inacurate about it’s occupied status. We decided that it would be for great justice if a computer could determine by itself if the spot was taken or not. Image processing seemed like the most plausible solution. Snap a picture, run it through … something, and spit out “TAKEN” or “AVAILABLE”.

So Mike Ackerman and I set out to master the basics of image processing, with the knowledge that OpenCV, an open source image processing library, would most likely be the tool we would lean on. Unfortunately the online community around this type of stuff is less prominent than the web community we’re so familiar with. Eventually we stumbled across SimpleCV, an open source python project that makes OpenCV a good bit more accessible.

We rigged up our Raspberry Pi to a webcam and fashioned it to the window overlooking the parking spot. Pretty quickly we were able to capture images of our parking space, so the next challenge was to figure out that … something to process the image.

Step #3 Pull Out The Big Guns

Canny Edge Detection is the brains behind the operation at the moment. You supply an image and it returns a new image that’s black everywhere with white lines on all the edges it detected. You can pass in different parameters that mean things we don’t quite understand. With enough fiddling we were able to drown out the noise of the image to a satisfactory level, and still highlight the object we were concerned with identifying.

Since an empty spot produces a nice big area void of white lines, we added some cropping and masking before running the edge detection. Thus we could only count the white pixels in the area where the car would be, set a threshold, and return “TAKEN” if the number of white pixels ever exceeded the threshold. It’s not 100% accurate, but it works quite well. If the spot is empty, we typically see a white pixel count from 0 to 400, and the presence of a car cranks that up in the 5,000 - 12,000 range. Performance at night and when it's snowing leaves room for improvement, but you can see the code for yourself here.

Step #4 Get It On The Internet

Back in our wheelhouse, we whipped up a quick little rails app to host the images and determined status of the parking spot. You can see it in all it’s (un-styled) glory here - A cronjob on the Raspberry Pi then runs this flow every 10 minutes:

  • take a picture, save it
  • crop it, mask it, convert to edge-detected version, save that
  • count white pixels
  • make a POST to the heroku app with the image, edge-detected image, and status (“TAKEN” or “AVAILABLE”)

Step #5 Engage The Power Of Texting

The website is up, our Raspberry Pi is fairly good at determining the availability of the parking spot, all is sweet. But what if you’re on the go and don’t have good access to the internet, or still think flip phones are the way to true happiness? Not a problem! Thanks to the awesome service Twilio provides, you can literally send the app a text, and you’ll get a response with the parking spot status in seconds. You find out that it’s taken? Rats. Simply shoot another text over with the word “Alert” in it, and the app will text you as soon as the spot frees up (give or take a few mintues).

The Future

While side projects like this are extremely fun to work on, we can’t play around with internal office projects all the time. Regardless, I hope to make some strides in the next few months on the following fronts:

  • Better camera allowing for night vision
  • Make a Haar Classifier to recognize cars
  • Integrate the app with the Google Calendar
  • Get SimpleCV working on OSX so we don’t have to experiment/develop on a Raspberry Pi

Related Articles