This project provides a Raspberry Pi oriented driver monitoring server built with Flask, OpenCV, MediaPipe model assets, and GPIO-based alerts.
It accepts uploaded JPEG frames, detects face and attention state, and exposes both a live MJPEG stream and JSON status output. The current runtime classifies:
NO_FACENORMALDISTRACTEDDROWSY
It also supports:
- LED and buzzer alerts through Raspberry Pi GPIO
- looped melodies for sustained
DISTRACTEDandDROWSYstates - auto-download of required OpenCV cascade XML files if missing
- systemd deployment for headless startup on boot
pi_server.py: main Flask server and processing loopmodels/face_landmarker.task: local face landmark model assetcascades/: OpenCV cascade storage/download directorydriver_monitor.service: example systemd unit
- Python 3.10+
- Raspberry Pi OS or another Linux environment
- Camera client that posts JPEG frames to the server
- Optional Raspberry Pi GPIO hardware for LED/buzzer output
Install Python dependencies:
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txtStart the server:
python3 pi_server.pyDefault server address:
http://0.0.0.0:5000/
Useful endpoints:
GET /: simple monitoring pageGET /status: JSON status and last frame ageGET /stream: MJPEG video streamPOST /frame: upload a frame under multipart fieldframe
Example frame upload:
curl -X POST http://127.0.0.1:5000/frame \
-F "frame=@sample.jpg"Example status check:
curl http://127.0.0.1:5000/statusCurrent output logic:
NO_FACE: LED on, buzzer offNORMAL: LED off, buzzer offDISTRACTED: after 3 seconds, starts looping Fur EliseDROWSY: after 3 seconds, starts looping Super Mario; after 6 seconds total, LED also turns on
If GPIO initialization fails, the app falls back to a mock backend and continues running without hardware control.
An example service file is included as driver_monitor.service.
Before enabling it, update paths if your installation directory differs from /home/pi/driver-monitor.
Typical setup:
sudo cp driver_monitor.service /etc/systemd/system/driver_monitor.service
sudo systemctl daemon-reload
sudo systemctl enable driver_monitor.service
sudo systemctl start driver_monitor.service
sudo systemctl status driver_monitor.servicepi_server copy.pyis a local backup copy and is intentionally excluded from version control.- The project expects
models/face_landmarker.taskto be present locally. - Haar cascade files are downloaded automatically if OpenCV cannot find them on the system.