This project performs real-time person detection using TFLite or ONNX models on a Raspberry Pi. The scripts are optimized for models created with OneAI. When a person is detected, it triggers a GPIO pin (e.g., for a light or relay) and plays an alarm sound.
We created the ai model using OneAI. Here you can find the setup for this demo.
- Raspberry Pi (Pi 4 or 5 recommended)
- Camera: USB Webcam or Raspberry Pi Camera Module.
- Speaker/Audio Output: For the alarm sound (
alarm.mp3). - GPIO:
- Pin 14 (BCM): Connected to your alert device (LED, Relay, etc.).
- GND: Connect ground.
It is recommended to use a virtual environment with access to system packages (important for libraries like RPi.GPIO or if you want to use the picam).
# Create venv with system-site-packages
python3 -m venv --system-site-packages venv
# Activate the venv
source venv/bin/activateInstall the required Python libraries.
# Ensure you are in the project directory
pip install -r requirements.txt
# You also need mpg123 for sound playback
sudo apt-get install mpg123If you use the Picamera, you may run into issues with TensorFlow dependencies in the system site-packages.
Fix for "No module named 'imp'" (Python 3.13):
This error is caused by an old version of flatbuffers.
pip uninstall -y flatbuffers
pip install "flatbuffers>=23.0"This should solve the error.
To find the correct index for your USB camera:
Method 1: List Devices
ls -l /dev/video*Usually, /dev/video0 is index 0, /dev/video2 is index 2, etc. Note that many USB cameras create two devices (e.g., video0 and video1). Try the first one (even number) first.
Method 2: using v4l2-ctl (Recommended)
sudo apt-get install v4l-utils
v4l2-ctl --list-devicesThis will show which camera maps to which /dev/videoX path.
The main script is meeting_alert.py, which handles both TFLite and ONNX models.
python meeting_alert.py --model models/your_model.tflite --cam 0| Parameter | Type | Default | Description |
|---|---|---|---|
--model |
path | Required | Path to the .tflite or .onnx model file. |
--cam |
int | None |
USB Camera Index (e.g., 0 for /dev/video0). |
--gui |
flag | False |
Add this flag to open a window showing the camera feed and detection boxes. |
--conf |
float | 0.5 |
Confidence threshold (0.0 - 1.0) for triggering the alarm. |
Run with a TFLite model on USB Camera 0 and show the GUI:
python meeting_alert.py --model models/person_model.tflite --cam 0 --gui --conf 0.6Run headless (no GUI):
python meeting_alert.py --model models/detection_post.tflite --cam 0Run using a Raspberry Pi Camera (Picamera2):
If no --cam index is provided, the script attempts to use Picamera2 automatically.
python meeting_alert.py --model models/person_model.tflite- Detected: GPIO 14 set to HIGH,
alarm.mp3plays in a loop. - Not Detected: GPIO 14 set to LOW, alarm stops.