Alright, let me tell you about this cool project I tackled recently: people counting with occupancy tracking for buildings. It was a fun dive into computer vision and IoT, and I learned a ton along the way. So, grab a coffee, and let’s get started!

The Idea:

The whole thing started when my manager asked if there was a way to track how many people were in different areas of the office in real-time. The goal was to optimize space usage, especially in meeting rooms and common areas. I thought, “Hey, I can probably do that!” So, I jumped in headfirst.

The Gear:

First things first, I needed the right tools. I ended up using:

  • A few Raspberry Pi 4s: These were going to be the brains of the operation, doing the image processing.
  • USB cameras: Cheap and cheerful, perfect for capturing video.
  • Some basic sensors: I added some temperature and humidity sensors because, why not? More data is always better, right?
  • A cloud server: For collecting and visualizing the data. I used AWS, but you could use anything.

Setting Up the Pi’s:

Getting the Raspberry Pis ready was the first hurdle. I installed the usual stuff: Raspberry Pi OS, Python, and a bunch of libraries like OpenCV for image processing, and TensorFlow Lite for the machine learning part. TensorFlow Lite is crucial because it lets you run models efficiently on lower-powered devices like the Pi.

Choosing a Model:

Next up, finding a decent object detection model. I spent ages looking around, and eventually settled on a pre-trained MobileNet SSD model. It’s not the most accurate model in the world, but it’s fast and lightweight, which is exactly what I needed for running on the Pi.

The Code:

Alright, so here’s the basic code flow:

  1. Capture a frame from the USB camera.
  2. Preprocess the image (resize it, normalize the pixel values).
  3. Run the object detection model on the image.
  4. Look for “person” detections.
  5. If a person is detected, increment a counter.
  6. Send the count and sensor data to the cloud server.

It sounds simple, but there were a lot of little tweaks involved. For example, I had to play around with the confidence threshold to avoid false positives (detecting chairs as people, that kind of thing). And I used some simple logic to track people entering and exiting a defined area.

Dealing with Occlusion and Lighting:

Of course, things weren’t always smooth sailing. Occlusion (people blocking each other) was a big problem. To try and mitigate this, I positioned the cameras strategically to get different viewpoints. Lighting was another issue. Shadows and bright light could mess with the detection, so I tried to normalize the lighting as much as possible.

Sending Data to the Cloud:

For sending the data to the cloud, I used MQTT. It’s lightweight and works well for IoT applications. I set up an MQTT broker on my AWS server and had the Raspberry Pis publish messages to it every few seconds. Each message contained the person count, timestamp, and sensor data.

Visualizing the Data:

Once the data was in the cloud, I needed a way to visualize it. I ended up using Grafana, which is a really cool open-source dashboard tool. I set up a dashboard to display the real-time person count, historical trends, and sensor data. It looked pretty slick!

The Result:

After a few weeks of tweaking and testing, I finally had a working system. It wasn’t perfect, but it was accurate enough to give a good estimate of occupancy in different areas. My manager was pretty happy with the results, and we even started using the data to make some real decisions about space allocation.

Lessons Learned:

This project taught me a ton. Here are a few key takeaways:

  • Computer vision can be surprisingly tricky in the real world.
  • Choosing the right hardware and software is crucial for performance.
  • Data visualization is key to making the data useful.
  • Always expect the unexpected – Murphy’s Law is real!

Wrapping Up:

Overall, this was a challenging but rewarding project. It gave me a chance to flex my coding muscles, learn about new technologies, and solve a real-world problem. If you’re looking for a fun and practical project to tackle, I highly recommend giving people counting a try. You might be surprised at what you can accomplish!