Skip to content

Latest commit

 

History

History
372 lines (288 loc) · 8.09 KB

File metadata and controls

372 lines (288 loc) · 8.09 KB

Setup Guide for Driver Monitoring System

This guide will walk you through setting up the Driver Monitoring System from scratch.

Table of Contents

  1. System Requirements
  2. Python Installation
  3. Project Setup
  4. Model Setup
  5. Alert System Configuration
  6. Running the Application
  7. Troubleshooting

System Requirements

Minimum Requirements

  • OS: Windows 10, macOS 10.15+, or Linux (Ubuntu 18.04+)
  • RAM: 4 GB (8 GB recommended)
  • Python: 3.8 or higher
  • Webcam: Any USB or built-in camera
  • Internet: Required for WhatsApp alerts

Recommended Requirements

  • RAM: 8 GB or more
  • GPU: NVIDIA GPU with CUDA support (optional, for faster processing)
  • Python: 3.9 or 3.10

Python Installation

Windows

  1. Download Python from python.org
  2. Run the installer
  3. Important: Check "Add Python to PATH"
  4. Verify installation:
    python --version

macOS

  1. Install Homebrew (if not already installed):
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  2. Install Python:
    brew install python@3.10
  3. Verify installation:
    python3 --version

Linux (Ubuntu/Debian)

sudo apt update
sudo apt install python3 python3-pip python3-venv
python3 --version

Project Setup

Step 1: Clone the Repository

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

Step 2: Create Virtual Environment

Windows:

python -m venv venv
venv\Scripts\activate

macOS/Linux:

python3 -m venv venv
source venv/bin/activate

You should see (venv) in your terminal prompt.

Step 3: Install Dependencies

pip install --upgrade pip
pip install -r requirements.txt

This may take 5-10 minutes depending on your internet connection.

Step 4: Verify Installation

python -c "import cv2, flask, ultralytics; print('All packages installed successfully!')"

Model Setup

Option 1: Download Pre-trained Models

  1. YOLOv8 Nano (General Object Detection)

    # This will auto-download on first run
    # Or manually download from: https://github.com/ultralytics/ultralytics
  2. Face Mask Model (best.pt)

    • Train your own using train.ipynb
    • Or request from project maintainer
  3. Hand Detection Model (handdsa.pt)

    • Train your own or use a pre-trained hand detection model
  4. Smoking Detection Model (detection_module.pt)

    • Optional - train if needed

Option 2: Train Your Own Models

  1. Open train.ipynb in Jupyter:

    pip install jupyter
    jupyter notebook train.ipynb
  2. Follow the notebook instructions to:

    • Prepare your dataset
    • Configure training parameters
    • Train the model
    • Export the .pt file
  3. Place trained models in the models/ directory

Model Directory Structure

models/
├── best.pt              # Mask detection
├── yolov8n.pt          # General detection
├── handdsa.pt          # Hand detection
└── detection_module.pt # Smoking detection (optional)

Alert System Configuration

Step 1: Create Cloudinary Account

  1. Go to cloudinary.com
  2. Sign up for a free account
  3. Navigate to Dashboard
  4. Note your:
    • Cloud Name
    • API Key
    • API Secret

Step 2: Create Twilio Account

  1. Go to twilio.com
  2. Sign up for a free trial account
  3. Navigate to Console Dashboard
  4. Note your:
    • Account SID
    • Auth Token

Step 3: Enable WhatsApp Sandbox

  1. In Twilio Console, go to MessagingTry it outSend a WhatsApp message
  2. Follow instructions to join the sandbox (send a code to the Twilio number)
  3. Note the WhatsApp Sandbox Number: whatsapp:+14155238886

Step 4: Configure Alert System

  1. Copy the template:

    cp alert_system_template.py alert_system.py
  2. Edit alert_system.py with your credentials:

    CLOUDINARY_NAME = "your_cloud_name"
    CLOUDINARY_KEY = "your_api_key"
    CLOUDINARY_SECRET = "your_api_secret"
    
    TWILIO_SID = "your_account_sid"
    TWILIO_TOKEN = "your_auth_token"
    TWILIO_FROM_NUMBER = "whatsapp:+14155238886"
    
    YOUR_WHATSAPP_NUMBER = "whatsapp:+1234567890"  # Your number

Step 5: Test Alert System

from alert_system import configure_alerts, send_whatsapp_alert

configure_alerts()
send_whatsapp_alert("Test message from Driver Monitoring System!")

Running the Application

Step 1: Start the Server

python app.py

You should see:

Loading YOLO models...
Models loaded successfully.
✅ Alert system configured (Cloudinary & Twilio).
 * Running on http://127.0.0.1:5000

Step 2: Access the Web Interface

  1. Open your browser
  2. Navigate to: http://localhost:5000
  3. Allow camera access when prompted

Step 3: Test Detection

Live Mode:

  1. Click "Start Camera"
  2. Position yourself in front of the camera
  3. Test different scenarios:
    • Close your eyes for 2+ seconds (drowsiness)
    • Hold a phone to your ear
    • Cover your face with your hand

Image Upload Mode:

  1. Click "Image Upload" tab
  2. Upload a test image
  3. View detection results

Troubleshooting

Common Issues

1. ModuleNotFoundError

ModuleNotFoundError: No module named 'cv2'

Solution:

pip install opencv-python-headless

2. Camera Not Working

Cannot access camera

Solutions:

  • Check camera permissions in system settings
  • Close other apps using the camera
  • Try a different browser (Chrome recommended)

3. Model Not Found

FileNotFoundError: models/best.pt not found

Solution:

  • Ensure models are in the models/ directory
  • Check file names match exactly

4. CUDA Out of Memory (GPU users)

Solution:

# In app.py, reduce batch size or use CPU
device = 'cpu'  # Instead of 'cuda'

5. Twilio/Cloudinary Errors

TwilioRestException: Unable to create record

Solutions:

  • Verify credentials are correct
  • Check Twilio trial account limits
  • Ensure you've joined the WhatsApp sandbox

6. Port Already in Use

Address already in use

Solution:

# Use a different port
python app.py --port 8080

Or kill the process using port 5000:

Windows:

netstat -ano | findstr :5000
taskkill /PID <PID> /F

macOS/Linux:

lsof -ti:5000 | xargs kill -9

Performance Issues

Slow Detection

  1. Reduce frame processing:

    DETECT_HEAVY_EVERY_N = 5  # Process every 5th frame
  2. Lower image resolution:

    imgsz = 416  # Instead of 640
  3. Use smaller model:

    • YOLOv8n (nano) is fastest
    • YOLOv8s (small) for better accuracy

High CPU Usage

  • Enable GPU if available
  • Increase DETECT_HEAVY_EVERY_N
  • Close other applications

Getting Help

If you're still stuck:

  1. Check existing issues: GitHub Issues
  2. Create new issue: Provide:
    • Error message
    • Operating system
    • Python version
    • Steps to reproduce
  3. Email: your.email@example.com

Next Steps

Once everything is working:

  1. ✅ Test all detection modules
  2. ✅ Customize thresholds in app.py
  3. ✅ Train custom models for better accuracy
  4. ✅ Set up proper environment variables for production
  5. ✅ Consider deploying to cloud (AWS, GCP, Azure)

Production Deployment

Security Checklist

  • Move credentials to environment variables
  • Enable HTTPS
  • Set up proper logging
  • Implement rate limiting
  • Add authentication
  • Use production WSGI server (gunicorn, uWSGI)

Example Production Setup

# Install gunicorn
pip install gunicorn

# Run with gunicorn
gunicorn -w 4 -b 0.0.0.0:8000 app:app

Congratulations! Your Driver Monitoring System is now set up and running! 🎉