Alright folks, lemme tell you about this cool project I cooked up – an outdoor people counting system for malls and parks. It’s been a journey, and I learned a ton along the way. Figured I’d share the whole shebang with you.

So, the initial idea was simple: malls and parks want to know how many people are visiting, right? It helps them with staffing, security, and figuring out what’s popular. I thought, “Hey, I can build something for that!”

Phase 1: Gathering the Gear and Brainstorming

  • First things first, I needed a camera. I snagged a decent IP camera that could handle outdoor conditions. Weatherproofing was key, you know?
  • Then came the brain – a Raspberry Pi 4. Small, powerful enough, and easy to work with.
  • I also picked up an enclosure to protect the Pi from the elements. Can’t have it getting rained on!

Next up, the software. I knew I wanted to use Python ’cause it’s what I’m comfy with. I started looking into OpenCV for the image processing. It’s got all sorts of cool functions for object detection and tracking.

Phase 2: Diving into the Code

This is where things got interesting. I spent a good chunk of time learning about background subtraction techniques in OpenCV. The idea is to identify moving objects by comparing the current frame to a background model.

I messed around with a few methods: MOG2, KNN… MOG2 seemed to work best for my situation. It’s pretty robust to changes in lighting and shadows, which is crucial for outdoor environments.

Here’s the basic workflow:

  • Grab a frame from the camera feed.
  • Apply the background subtractor to get a foreground mask (the moving stuff).
  • Use morphological operations (like erosion and dilation) to clean up the mask and remove noise.
  • Find contours in the mask. Each contour represents a potential person.
  • Filter the contours based on size and aspect ratio. This helps get rid of small objects like leaves or birds.
  • Count the remaining contours! Each one is a person.

It sounds straightforward, but trust me, tweaking the parameters to get accurate results was a pain. I spent hours adjusting thresholds and kernel sizes.

Phase 3: Dealing with Real-World Challenges

The biggest problem I faced was occlusion – people blocking each other. When two people overlapped, the system would often count them as one. Bummer!

I tried a few things to fix this:

  • Using perspective transform: I mapped the camera’s view onto a “bird’s-eye” view. This made it easier to separate people who were close together.
  • Implementing tracking: Instead of just counting contours in each frame, I tracked them over time. This helped the system recognize when a single blob split into two people. I used a simple centroid tracking algorithm.

These tricks helped a lot, but they weren’t perfect. Occlusion is a tough nut to crack!

Phase 4: Putting it All Together and Data Logging

Once I was happy with the counting accuracy, I wrapped everything up into a neat Python script. I also added code to log the counts to a CSV file.

I then set up a cron job on the Raspberry Pi to run the script every minute. This way, I could get a continuous stream of data.

Finally, I wrote a simple web app (using Flask) to display the counts in real-time. I could access it from my phone or computer.

Results and Lessons Learned

The system worked surprisingly well! It wasn’t perfect, but it gave a decent estimate of the number of people in the area. I was pretty proud of myself.

Here’s what I learned:

  • Outdoor environments are tough. Lighting changes, shadows, and weather can all mess with your results.
  • Occlusion is a major challenge. Advanced tracking algorithms are needed for high accuracy.
  • Data visualization is important. It’s much easier to understand the data when it’s presented visually.
  • Python and OpenCV are awesome tools for computer vision.

Overall, this was a really fun and rewarding project. I got to learn a lot about computer vision, and I built something that could actually be useful. Maybe I’ll even sell it to a mall someday! Who knows?