ESP32-CAM Drowsiness Detection System
This project is a drowsiness detection system that uses an ESP32-CAM module along with computer vision algorithms to detect signs of drowsiness in drivers. It monitors for eye blinking patterns, yawning, and head movement to alert when someone might be getting sleepy.
How It Works
The system tracks several indicators to determine if someone is becoming drowsy:
- Whether their eyes are closed or open
- How often they're blinking
- If they're yawning frequently
- The position of their head (if it's tilting down)
When the system detects possible drowsiness, it triggers alerts:
- Green LED: Everything looks good
- Yellow LED: Caution - driver may be getting tired
- Red LED: Alert - potential drowsiness detected
- Buzzer sounds to get attention
What You Need
Main Components
- ESP32-CAM board with camera
- Arduino Uno (optional, for extra features)
- MicroSD card (optional, for logging data)
Lights and Sound
- Green LED for normal status
- Yellow LED for warning
- Red LED for alerts
- Two 220Ω resistors for the LEDs
- Active buzzer (for gentle alerts)
- Passive buzzer (for louder alerts)
Other Stuff
- Breadboard for connections
- Various jumper wires
- 5V power supply
- 10µF capacitor for power stability
Wiring It Up
Connect your ESP32-CAM like this: GPIO 12 to Green LED GPIO 13 to Yellow LED GPIO 15 to Red LED GPIO 2 to Active Buzzer GPIO 4 to Passive Buzzer All components need to connect to ground (GND) Power to 5V
Setting Up the Software
Option 1: Advanced Computer Vision Setup (Recommended)
This version does the most accurate detection but requires a computer running Python.
What you need:
- Python 3.8 or newer
- OpenCV and dlib libraries
- The ESP32-CAM connected to your network
To get started:
- Go to your project folder
- Install the required Python packages by running: pip install -r requirements.txt
- Download this file: shape_predictor_68_face_landmarks.dat.bz2
- Extract the .dat file and put it in your project folder
- Update the WiFi settings in the drowsiness_detection.ino file
- Upload that code to your ESP32-CAM
- Run the Python script: python drowsiness_detection.py --esp32-ip YOUR_ESP32_IP_HERE
Option 2: Simple Arduino Only Version
This version runs everything directly on the ESP32 without needing a computer.
- Open simple_drowsiness_detection.ino in Arduino IDE
- Update your WiFi settings
- Upload to your ESP32-CAM
- Access the system through your web browser using the ESP32's IP address
Option 3: Dual Controller Setup (Best Accuracy)
This combines the ESP32-CAM for video and an Arduino Uno for controlling alerts.
- Wire up both ESP32-CAM and Arduino Uno as described in the connection diagrams
- Upload arduino_uno_slave.ino to the Arduino
- Upload drowsiness_detection.ino to the ESP32-CAM
- Make sure both have a common ground connection
- Install Python dependencies: pip install -r requirements.txt
- Run the Python script: python advanced_dual_controller.py --esp32-ip YOUR_ESP32_IP
Configuring the Arduino
If you're using Arduino IDE:
- Install ESP32 board package through the Boards Manager
- Select "AI Thinker ESP32-CAM" from the board list
- Install these libraries: ArduinoJson, AsyncTCP, ESPAsyncWebServer
WiFi settings are in the code where you put your network name and password.
Using the System
For the Computer Vision Version:
- Run the Python command with your ESP32 IP
- Check the web interface at http://ESP32_IP/
- Use 'q' to quit the Python program or 'r' to reset the blink counters
For the Simple Version:
- Just access the web interface at http://ESP32_IP/
- You can capture photos, start monitoring, test warnings, or reset
How Detection Works
Blink Detection Uses a calculation called Eye Aspect Ratio (EAR). When EAR drops below 0.25, eyes are considered closed. The system tracks blink rate and triggers an alert if eyes stay closed for more than 1-2 seconds.
Yawn Detection Looks at mouth shape using Mouth Aspect Ratio (MAR). When MAR goes above 0.7, it identifies a yawn.
Head Position Estimates head pose using facial landmarks to detect if the head is tilting beyond 20 degrees in any direction.
Troubleshooting Common Problems
Camera not working:
- Make sure power supply is stable at 5V
- Double check your pin connections
- Try different board settings in Arduino IDE
WiFi issues:
- Check your network name and password
- ESP32 only works on 2.4GHz networks, not 5GHz
- Make sure signal strength is good
Python problems: If you have dlib issues on Windows, try: pip install cmake pip install dlib --no-cache-dir Or try: conda install -c conda-forge dlib
LEDs not lighting up:
- Check that resistors are 220Ω
- Verify GPIO pins are correct
- Test with a multimeter
Buzzer not working:
- Check that polarity is correct
- Make sure power supply can deliver enough current
For Better Performance
- Make sure lighting is good for camera
- Position camera at eye level
- Keep background movements to a minimum
- Consider using an external power supply for ESP32
Safety Notice
This system is meant for educational and demonstration purposes only. It's not certified for safety-critical applications. Use it as an addition to other safety measures, not as a replacement. Test everything thoroughly before using in real situations.
Project Structure
The main files are:
- drowsiness_detection.ino - Advanced version for ESP32
- simple_drowsiness_detection.ino - Simple version for ESP32
- drowsiness_detection.py - Python computer vision code
- requirements.txt - Python dependencies
- README.md - This file
Version History
- v1.0.0 - Basic detection functionality
- v1.1.0 - Added advanced computer vision features
- v1.2.0 - Improved web interface
- v1.3.0 - Added simple Arduino-only version