Getting Started with Tracking Foot Traffic
Alright, so I had this nagging thought for a while. I run a small space, kind of a maker workshop slash tiny retail spot, and I always wondered where people actually went when they came inside. And, you know, how many people were actually coming through the door versus just peering in. Decided it was time to stop guessing and actually build something to figure it out.
Gathering the Bits and Pieces
Didn’t want to spend a fortune, obviously. I had an old Raspberry Pi 3B+ sitting in a drawer, doing nothing useful. Perfect. Then I needed a camera. Found a cheap USB webcam online, nothing fancy, just something that worked with the Pi. That was pretty much it for the main hardware.
- Raspberry Pi 3B+
- Basic USB Webcam
- Power supply for the Pi
- An SD card for the operating system
Software-wise, I figured Python was the way to go. It’s got libraries for almost everything. I knew I’d need something for handling the camera feed and doing some image analysis. OpenCV seemed like the obvious choice everyone talks about, so I decided to wrestle with that.
Setting Up and First Hurdles
First step was getting the Pi set up with its OS (Raspberry Pi OS) and installing Python and OpenCV. Installing OpenCV on a Pi… well, let’s just say it took a while and a few attempts following different online guides. It’s not exactly a one-click affair, especially on older hardware. Lots of compiling.
Once that was finally done, I plugged in the webcam. Getting the basic video stream working in Python using OpenCV wasn’t too bad. Just a few lines of code to capture frames. Seeing my workshop appear on the screen felt like a small victory!
Then came the hard part: actually detecting people. I started with background subtraction methods in OpenCV. The idea is the camera learns what the background looks like when empty, and then anything new that moves is potentially a person. This worked… sort of. Changes in lighting, shadows moving, even a chair getting bumped could trigger false detections. Spent a good chunk of time tweaking sensitivity parameters.
Counting People Crossing a Line
For the actual foot traffic count, I decided the simplest way was a virtual line. I literally drew a line on the screen in my code. Then, I tried to track the detected ‘blobs’ (potential people). If the center of a blob crossed the line from, say, top-to-bottom, I incremented a counter.
This was tricky too. People walking close together would merge into one blob. Sometimes a person lingering near the line would be counted multiple times. I added some logic to only count a blob once and track its ID for a short period, but it wasn’t perfect. Still, it gave a rough idea.
Making the Heatmap
This was the part I was really interested in. Where do people stand around? The plan was: every few seconds, find the center point (or maybe the bottom-center point, closer to the feet) of each detected person blob. Store these (x, y) coordinates in a big list.
After collecting data for a while (like an hour, or a day), I needed to visualize it. I took a snapshot of the empty workshop floor from the camera’s perspective to use as a background image. Then, I used a Python library (I think it was Matplotlib or Seaborn, maybe?) to plot all the collected coordinate points onto this background image. Instead of just dots, I used a function that creates a heatmap – areas with lots of points become ‘hotter’ (redder), and areas with few points stay ‘cooler’ (bluer).
Getting the overlay right, matching the coordinate system of the detected points to the background image, took some fiddling with scaling and offsets. The first few attempts looked completely wrong, with heat blobs floating in weird places.
Putting It All Together and Seeing Results
So, after several evenings and a weekend tinkering, I had a basic system running. The Pi sat on a shelf overlooking the main area, plugged into the wall, dutifully watching through the webcam.
What I got:
- A running count of people entering (crossing the virtual line). It wasn’t 100% accurate, maybe missed 1 in 10 or counted some ghosts, but it gave a trend.
- A heatmap image that updated maybe once an hour. This was genuinely useful! It clearly showed the main path people took from the door and the specific spots where they tended to stop and look at things. Confirmed some of my suspicions and highlighted one area I thought was popular but actually got very little traffic.
Performance on the Pi 3B+ was okay-ish for the low frame rate I was using, but you could tell it was working hard. Generating the heatmap itself took a noticeable few seconds.
Overall, it was a fun project. Didn’t cost much, taught me a lot about OpenCV’s quirks, and actually gave me some useful insights into how people use my little space. It’s not a commercial-grade system by any means, but for a DIY job, I was pretty pleased with the outcome.