Skip to content

ABHAY627/RealTime-AI-Driver-Monitoring-System

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

1 Commit
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿš— AI Driver Monitoring System

A real-time AI-powered driver monitoring system built with Flask, OpenCV, and YOLO that detects various driver behaviors and safety violations to enhance road safety.

Python Flask YOLO License

๐Ÿ“‹ Table of Contents

โœจ Features

Real-Time Detection Capabilities

  • ๐Ÿ‘๏ธ Blink Detection - Monitors eye aspect ratio (EAR) to detect blinking and drowsiness
  • ๐Ÿ˜ท Face Mask Detection - Detects whether the driver is wearing a mask
  • ๐Ÿ“ฑ Phone Usage Detection - Identifies when the driver is using a mobile phone
  • ๐Ÿšฌ Smoking Detection - Detects smoking behavior while driving
  • โœ‹ Hand-on-Mouth Detection - Identifies potential distraction from eating/drinking

Smart Alert System

  • Real-time WhatsApp Alerts - Sends instant notifications via Twilio
  • Cloud Image Upload - Captures and uploads violation snapshots to Cloudinary
  • Intelligent Cooldown - Prevents alert spam with configurable cooldown periods
  • Multi-Behavior Tracking - Monitors multiple safety violations simultaneously

Web Interface

  • Live Camera Feed - Real-time video analysis through webcam
  • Image Upload - Analyze static images for violations
  • Interactive Dashboard - Visual feedback with bounding boxes and confidence scores
  • Responsive Design - Bootstrap-based UI that works on all devices

๐ŸŽฏ Demo

The system provides two modes of operation:

  1. Live Analysis Mode: Real-time monitoring using your webcam
  2. Image Upload Mode: Analyze pre-recorded images or screenshots

Detection Examples

  • Drowsiness alerts when eyes remain closed for extended periods
  • Phone detection with hand-phone overlap verification
  • Mask compliance monitoring
  • Smoking behavior identification

๐Ÿ—๏ธ Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Web Browser   โ”‚
โ”‚  (HTML/JS UI)   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚
         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Flask Server   โ”‚
โ”‚   (app.py)      โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚
    โ”Œโ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ–ผ         โ–ผ         โ–ผ          โ–ผ          โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Blink โ”‚ โ”‚ Mask  โ”‚ โ”‚ Phone โ”‚ โ”‚ Smoke  โ”‚ โ”‚  Hand  โ”‚
โ”‚Module โ”‚ โ”‚Module โ”‚ โ”‚Module โ”‚ โ”‚ Module โ”‚ โ”‚ Module โ”‚
โ””โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”˜
    โ”‚         โ”‚         โ”‚          โ”‚          โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                       โ”‚
                       โ–ผ
              โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
              โ”‚  Alert System  โ”‚
              โ”‚  - Cloudinary  โ”‚
              โ”‚  - Twilio      โ”‚
              โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ”ง Installation

Prerequisites

  • Python 3.8 or higher
  • Webcam (for live detection)
  • Internet connection (for alerts)

Step 1: Clone the Repository

git clone https://github.com/yourusername/Driver_Monitoring_System.git
cd Driver_Monitoring_System

Step 2: Create Virtual Environment (Recommended)

# Windows
python -m venv venv
venv\Scripts\activate

# macOS/Linux
python3 -m venv venv
source venv/bin/activate

Step 3: Install Dependencies

pip install -r requirements.txt

Step 4: Download Model Files

Place the following model files in the models/ directory:

  • best.pt - Face mask detection model
  • yolov8n.pt - YOLO nano model for general object detection
  • handdsa.pt - Hand detection model
  • detection_module.pt - Smoking detection model (optional)

Step 5: Configure Alert System

Edit alert_system.py and add your credentials:

# Cloudinary Configuration
CLOUDINARY_NAME = "your_cloud_name"
CLOUDINARY_KEY = "your_api_key"
CLOUDINARY_SECRET = "your_api_secret"

# Twilio Configuration
TWILIO_SID = "your_account_sid"
TWILIO_TOKEN = "your_auth_token"
TWILIO_FROM_NUMBER = "whatsapp:+14155238886"

# Your WhatsApp Number
YOUR_WHATSAPP_NUMBER = "whatsapp:+1234567890"

๐Ÿš€ Usage

Running the Application

  1. Start the Flask Server
python app.py
  1. Access the Web Interface Open your browser and navigate to:
http://localhost:5000
  1. Choose Detection Mode
    • Live Analysis: Click "Start Camera" to begin real-time monitoring
    • Image Upload: Upload an image for analysis

Command Line Options

# Run on a specific port
python app.py --port 8080

# Run in debug mode
python app.py --debug

๐Ÿ“ Project Structure

Driver_Monitoring_System/
โ”‚
โ”œโ”€โ”€ app.py                      # Main Flask application
โ”œโ”€โ”€ requirements.txt            # Python dependencies
โ”œโ”€โ”€ train.ipynb                 # Model training notebook
โ”‚
โ”œโ”€โ”€ detectors/                  # Detection modules
โ”‚   โ””โ”€โ”€ __pycache__/
โ”‚
โ”œโ”€โ”€ models/                     # YOLO model files
โ”‚   โ”œโ”€โ”€ best.pt                 # Mask detection model
โ”‚   โ”œโ”€โ”€ yolov8n.pt             # General YOLO model
โ”‚   โ”œโ”€โ”€ handdsa.pt             # Hand detection model
โ”‚   โ””โ”€โ”€ detection_module.pt    # Smoking detection model
โ”‚
โ”œโ”€โ”€ templates/                  # HTML templates
โ”‚   โ””โ”€โ”€ index.html             # Main web interface
โ”‚
โ”œโ”€โ”€ alert_system.py            # WhatsApp alert integration
โ”œโ”€โ”€ blink_detection.py         # Eye blink/drowsiness detection
โ”œโ”€โ”€ hand_detection.py          # Hand detection module
โ”œโ”€โ”€ mask_detection.py          # Face mask detection
โ”œโ”€โ”€ phone_detection.py         # Mobile phone detection
โ”œโ”€โ”€ smoke_detection.py         # Smoking detection
โ””โ”€โ”€ utils.py                   # Utility functions (IoU, bbox operations)

๐Ÿ” Detection Modules

1. Blink Detection (blink_detection.py)

Uses MediaPipe Face Mesh to calculate Eye Aspect Ratio (EAR):

  • EAR Threshold: 0.25 (indicates closed eyes)
  • Drowsiness Threshold: 2.0 seconds of continuous eye closure
  • Blink Counting: Tracks total blinks per session

Algorithm:

EAR = (|p2 - p6| + |p3 - p5|) / (2 * |p1 - p4|)

2. Mask Detection (mask_detection.py)

YOLO-based face mask classification:

  • Detects: with_mask, without_mask, mask_weared_incorrect
  • Hand-on-mouth overlap detection to avoid false negatives
  • Configurable overlap threshold: 40%

3. Phone Detection (phone_detection.py)

Context-aware phone detection:

  • Requires hand-phone overlap (5% threshold)
  • Fallback to person-phone overlap (2% threshold)
  • Minimum phone size filtering to reduce false positives
  • Confidence threshold: 30%

4. Smoking Detection (smoke_detection.py)

Detects smoking behavior:

  • YOLO-based cigarette/smoking detection
  • Real-time visual feedback with bounding boxes
  • Integrated with alert system

5. Hand Detection (hand_detection.py)

Hand position tracking:

  • Used for context in phone and mask detection
  • Detects hand-on-mouth scenarios
  • Improves overall system accuracy

๐Ÿ“ข Alert System

The system uses Twilio and Cloudinary for real-time alerts:

Features:

  • Automated Snapshots: Captures frame when violation is detected
  • Cloud Storage: Uploads images to Cloudinary for permanent storage
  • WhatsApp Notifications: Sends instant alerts with image links
  • Smart Cooldown: 60-second cooldown between alerts to prevent spam

Alert Triggers:

  • ๐Ÿšจ Drowsiness detected (eyes closed > 2 seconds)
  • ๐Ÿ“ฑ Phone usage while driving
  • ๐Ÿšฌ Smoking behavior
  • ๐Ÿ˜ท Mask violations (when applicable)

Setup Requirements:

  1. Create a Cloudinary account
  2. Create a Twilio account
  3. Enable Twilio WhatsApp sandbox
  4. Configure credentials in alert_system.py

โš™๏ธ Configuration

Adjustable Parameters (app.py)

# Detection Thresholds
HAND_ON_MOUTH_THRESHOLD = 0.40      # Hand-mouth overlap ratio
EAR_BLINK_THRESHOLD = 0.25          # Eye closure threshold
DROWSY_SECONDS_THRESHOLD = 2.0      # Drowsiness time limit

# Alert Settings
ALERT_COOLDOWN_SECONDS = 60         # Cooldown between alerts
DETECT_HEAVY_EVERY_N = 3            # Process every Nth frame

# Phone Detection
PHONE_CONF = 0.30                   # Phone detection confidence
PHONE_IOU = 0.45                    # IoU threshold for NMS

๐Ÿค– Models

Required Models:

Model Purpose Size Download
best.pt Face mask detection ~6 MB Custom trained
yolov8n.pt General object detection ~6 MB Ultralytics
handdsa.pt Hand detection ~6 MB Custom trained
detection_module.pt Smoking detection Variable Custom trained

Training Your Own Models

See train.ipynb for training pipeline examples. The notebook includes:

  • Data preprocessing
  • Model training with YOLOv8
  • Validation and testing
  • Export for deployment

๐Ÿค Contributing

Contributions are welcome! Here's how you can help:

  1. Fork the repository
  2. Create a feature branch
    git checkout -b feature/AmazingFeature
  3. Commit your changes
    git commit -m 'Add some AmazingFeature'
  4. Push to the branch
    git push origin feature/AmazingFeature
  5. Open a Pull Request

Areas for Contribution:

  • ๐ŸŽฏ Improve detection accuracy
  • ๐Ÿš€ Add new detection modules
  • ๐ŸŽจ Enhance UI/UX
  • ๐Ÿ“š Improve documentation
  • ๐Ÿงช Add unit tests
  • ๐ŸŒ Add internationalization

๐Ÿ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ™ Acknowledgments

๐Ÿ”ฎ Future Enhancements

  • Multi-driver support
  • Historical data analytics dashboard
  • GPS integration for location tracking
  • Voice alerts in addition to WhatsApp
  • Mobile app for monitoring
  • Advanced fatigue detection using head pose
  • Integration with vehicle CAN bus
  • Cloud deployment (AWS/GCP/Azure)
  • Docker containerization
  • API for third-party integrations

โญ If you find this project useful, please consider giving it a star! โญ

About

No description, website, or topics provided.

Resources

License

Contributing

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors