Raspberry Pi Trick-or-Treat Counter with OpenCV

Avatar
Lisa Ernst · 25.10.2025 · Technology · 4 min

A Trick-or-Treat counter records visitors at the door and stores every 'entry' as an event with a timestamp. With a Raspberry Pi this can be solved cleanly, either via camera (OpenCV/YOLO, crossing a line) or simply via a door sensor and a time series in the dashboard. This explainer piece shows step by step which variant fits when, how it works, and where to find reliable tutorials.

Introduction & Overview

For a Trick-or-Treat counter there are two main approaches: camera-based counting and sensor-based counting. Both use a Raspberry Pi as the central control unit. The camera variant identifies people in the video frame, tracks them across frames and registers crossing a virtual line as a counting event. This is referred to as line/zone counting. The sensor variant uses a door sensor that records every opening and closing operation. The collected data can be visualized in a dashboard, for example with Grafana.

Camera-based Counting

Camera-based counting uses computer vision to detect and count people. OpenCV OpenCV is a library for image processing and tracking. YOLO (You Only Look Once) YOLO is a deep-learning detector that detects objects in real time. Ultralytics documents direct line/zone counting for YOLO. In this process, people in the video frame are detected, tracked across frames, and crossing a virtual line is booked as '+1'. This prevents double counting by tracking backends. As early as 2018 a practical people counter with OpenCV was demonstrated. PyImageSearch a practical people counter with OpenCV, Tracking and 'in/out' logic over a line. Since 2024/2025 there are well-maintained guides on YOLO-Counting (Lines/Zones) including tracking backends. For the hardware there exists a offizieller Quick-Start von Ultralytics für den Raspberry Pi (incl. Pi 5, Bookworm, camera setup and NCNN export for more performance). OpenCV can be installed on the Pi via package source or build script; Q-engineering pflegt aktuelle, Pi-5-taugliche Anleitungen. Camera counting provides context, but requires lighting and computing power. YOLO is robust against changing backgrounds, OpenCV-only is lighter and sufficient with little movement. On the Pi 5 Ultralytics NCNN models are recommended, as they run more efficiently on ARM than pure PyTorch inference, which reduces latency and power consumption.

OpenCV enables precise detection and counting of people in real time.

Quelle: youtube.com

OpenCV enables precise detection and counting of people in real time.

Quelle: YouTube

Sensor-based Counting

As an alternative to camera-based counting, a door sensor, such as a magnetic reed switch, can record every opening and closing of the door. This method is robust and often sufficient. Adafruit bietet Anleitungen zur Bewegungserfassung mit dem Raspberry Pi. Also on Raspberry Pi Stack Exchange finden sich Informationen zur Anbindung von 2-Draht-Türsensoren. Door sensors are incredibly simple, but they only count 'door events'. In crowds at Halloween this can undercount or overcount the real number of people. They are, however, data-efficient and require less computing power than camera systems.

Infrared sensors provide a simple and effective method for counting visitors.

Quelle: donskytech.com

Infrared sensors provide a simple and effective method for counting visitors.

Data Visualization & Storage

For visualizing the count data, suitable are InfluxDB (Zeitreihen-Datenbank) and Grafana. Without a dedicated database, can Grafana Live/MQTT be used. The Grafana MQTT-Datasource-Plugin prioritizes real-time data. For dashboards, InfluxDB 2 and Grafana are established on the Pi, including official setup guides. Those who want to start without a database can use streaming via Grafana Live and the MQTT plugin, where the plugin prioritizes real-time over history. Grafana MQTT streaming works, but is designed for real-time; history requires a database. Community voices report occasionally unstable behavior or limitations with MQTT plugins, which makes a DB-pipeline plausible for productive setups.

Practical Implementation & Tips

For fast results choose the path with the highest probability of success for your environment. For the camera variant with YOLO: install Ultralytics, choose a lightweight model (e.g., 'n'), define a line at the doorway threshold, publish the counting callback via MQTT. The offizielle Raspberry-Pi-How-to deckt Setup und Beschleunigung per NCNN ab. For OpenCV-Only: background subtraction/tracking, virtual line, counting without network — especially good with a static background. For the door sensor route: reed switch on GPIO, Python script for open/close, MQTT publish. Verkabelung und Pull-ups sind dokumentiert. For dashboards: Either InfluxDB 2 + Grafana nach Anleitung installieren or Live/MQTT testen, wenn Historie unwichtig ist. Trick-or-Treat examples from the maker community show how motion triggers, counters and displays come together, e.g., the „Pumpkin Pi Trick-or-Treat Tracker“. The video zeigt Counting mit YOLOv8, ByteTrack und Supervision; helpful to see the interaction of detection, tracking, and line events. Start small, test at the real door, and only expand what is missing in daily life – then your counter will count exactly what matters.

With a Raspberry Pi you can realize creative counting systems for Halloween visitors.

Quelle: support.initialstate.com

With a Raspberry Pi you can realize creative counting systems for Halloween visitors.

Quelle: YouTube

Teilen Sie doch unseren Beitrag!