This project is a complete real-time drowsiness and yawn detection system built using OpenCV, MediaPipe, Tkinter GUI, and Telegram Bot for alert notifications. It includes:
- Eye Aspect Ratio (EAR) and Mouth Aspect Ratio (MAR) calculations
- TTS voice alerts
- Pre- and post-alert video recording
- Telegram alerts with 10s video
- CUDA GPU acceleration (if available)
β Real-time face mesh detection using MediaPipe β EAR/MAR calculation for eye closure and yawning β Auto video recording (5s before + 5s after alert) β Telegram bot alert with video proof β Tkinter-based GUI with live feed β Text-to-speech (TTS) alerts β GPU acceleration using OpenCV CUDA
.
βββ drowsy_detector.py # Main source code (uploaded by user)
βββ README.md # This file
βββ /drowsy_video/ # Auto-saved alert videos
Install all Python dependencies using:
pip install opencv-python mediapipe pyttsx3 python-telegram-bot Pillowβ Optional for GUI support:
pip install tkinterβ Optional for GPU acceleration:
pip install opencv-contrib-pythonOpen the drowsy_detector.py file and update the following:
TELEGRAM_TOKEN = '<YOUR_BOT_TOKEN>'
CHAT_ID = '<YOUR_TELEGRAM_CHAT_ID>'If not using Telegram alerts, just leave TELEGRAM_TOKEN = ''.
β Ensure the following directory exists:
mkdir -p /home/pi/drowsy_videopython drowsy_detector.pyYouβll see a GUI window with:
- EAR/MAR values
- Start/Stop buttons
- Live video feed
| Feature | Description |
|---|---|
| π’ Drowsy Detection | Alerts if eyes closed for EYE_FRAMES |
| π‘ Yawn Detection | Alerts if mouth open for MAR_FRAMES |
| π₯ Video Recording | 5 seconds before & after alert |
| π£οΈ TTS | Audible alert |
| π€ Telegram Bot | Sends video to your chat |
EYE_THRESH = 0.28 # Eye Aspect Ratio
EYE_FRAMES = 51 # 1.7 seconds at 30 FPS
MAR_THRESH = 0.6 # Mouth Aspect Ratio
MAR_FRAMES = 60 # 2 seconds at 30 FPSIf you have an NVIDIA GPU:
- Uses
cv2.cudato process frames - Set CUDA device using
cv2.cuda.setDevice(0) - Automatically falls back to CPU if CUDA not found
- Create bot using @BotFather
- Note down the
BOT TOKEN - Get your
chat_idusing this method
-
Clicking
Exitor closing the GUI will:- Release camera
- Stop TTS
- Finalize video
- Exit gracefully
Created by Deventhira Kesavaraj BE.EEE, KPRIET Project: AI Drowsy Detection System with Telegram Alert Support
π ALERT: DROWSINESS DETECTED!
Capturing 10-second video...
π Then sends the video via Telegram!