Skip to content

najzdev/Sleep-Alert-Car-Camera-Image-Classification-System

Repository files navigation

๐Ÿ›ก๏ธ Pro-Vision v2.0: Advanced Driver Monitoring System

Overview

Pro-Vision is a production-ready, real-time driver drowsiness detection system using edge computing and advanced computer vision. It classifies driver states into AWAKE, DROWSY, or SLEEPING with multi-level alerts.

Key Features

โœ… Real-time Detection - 30+ FPS processing
โœ… Advanced Analytics - Blink detection, yawn detection, fatigue scoring
โœ… Multi-level Alerts - Info/Warning/Critical with audio & visual
โœ… Event Logging - Persistent JSON logs with filtering
โœ… Production Architecture - Modular, configurable, scalable
โœ… Easy Deployment - Single command startup, Streamlit web UI


๐Ÿš€ Quick Start

Prerequisites

  • Python 3.9+
  • Webcam (for live mode)
  • Linux/Mac/Windows

Installation

# Clone or download the repository
cd pro-vision-v2

# Install dependencies
pip install -r requirements.txt

# Run the dashboard
streamlit run app.py

The dashboard opens at http://localhost:8501


๐Ÿ“‹ System Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚          Video Input (Webcam)           โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                   โ”‚
                   โ–ผ
    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚  detection_engine.py         โ”‚
    โ”‚  โ”œโ”€ FaceDetector            โ”‚
    โ”‚  โ”œโ”€ DrowsinessClassifier    โ”‚
    โ”‚  โ””โ”€ FrameAnnotator          โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                       โ”‚
                       โ–ผ
        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
        โ”‚  alert_system.py         โ”‚
        โ”‚  โ”œโ”€ AlertManager        โ”‚
        โ”‚  โ””โ”€ EventLogger         โ”‚
        โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                   โ”‚
                   โ–ผ
    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚  app.py (Streamlit)          โ”‚
    โ”‚  โ”œโ”€ Live Feed               โ”‚
    โ”‚  โ”œโ”€ Real-time Metrics       โ”‚
    โ”‚  โ”œโ”€ Charts & Analytics      โ”‚
    โ”‚  โ””โ”€ Event Logs              โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ”ง Configuration

Edit config.json to tune the system:

{
  "detection": {
    "face_scale_factor": 1.1,        // Higher = faster but less accurate
    "face_min_neighbors": 6,         // Higher = fewer false positives
    "eye_scale_factor": 1.1,
    "eye_min_neighbors": 5,
    "clahe_clip_limit": 2.0          // Image enhancement strength
  },
  "drowsiness": {
    "history_window": 15,            // Frames to average (delay)
    "blink_threshold_frames": 2,     // Frames to consider a blink
    "eye_closed_ratio": 0.4,         // % eyes closed = drowsy
    "drowsy_threshold": 35,          // Score threshold for DROWSY
    "sleep_threshold": 75            // Score threshold for SLEEPING
  },
  "performance": {
    "skip_frames": 2,                // Process every Nth frame
    "frame_width": 640,
    "frame_height": 480,
    "target_fps": 30
  },
  "alerts": {
    "beep_cooldown": 2.0,            // Seconds between alerts
    "warning_threshold": 35,
    "critical_threshold": 75
  }
}

๐Ÿ“Š Features Explained

1. Real-time Detection

  • Face Detection: Haar Cascade (fast, edge-optimized)
  • Eye Detection: Haar Cascade in face ROI
  • Smile/Yawn Detection: Optional feature for fatigue analysis

2. Drowsiness Scoring

  • Rolling 15-frame window tracks eye closure
  • Hysteresis prevents false positives
  • Score decays when eyes open (recovery time)
  • Thresholds: DROWSY (>35) โ†’ SLEEPING (>75)

3. Blink Detection

  • Distinguishes between normal blinks and eye closure
  • Tracks blink rate (blinks/30s)
  • Abnormal patterns trigger warnings

4. Multi-level Alerts

Level Trigger Response
INFO Normal operation Green border, โœ… message
WARNING Score > 35 Orange border, โš ๏ธ message
CRITICAL Score > 75 Red border, ๐Ÿšจ alert + beep

5. Event Logging

  • Persistent JSON log (vision_events.json)
  • Filterable by alert level
  • Timestamp, score, and message
  • Export for analysis

๐ŸŽ›๏ธ Dashboard Controls

Sidebar Settings

  • Camera Mode: Live Camera or Demo Mode
  • Eye Sensitivity: 3-20 (lower = more sensitive)
  • Frame Skip: Process every Nth frame (1-5)
  • Audio/Visual Alerts: Toggle on/off
  • Event Logs: View, filter, export, clear

Main Display

  • Live Video Feed with annotated detections
  • Real-time Metrics: Status, score, eyes detected, blink rate
  • Drowsiness Chart: Score over time
  • Blink Rate Chart: Blink frequency trend

๐Ÿ“ˆ Performance Optimization

Frame Skipping

Process every 2nd or 3rd frame (configurable) while maintaining smooth UI:

skip_frames = 2
if frame_count % skip_frames == 0:
    detect_faces_and_eyes()

Benefit: 2x-3x faster with minimal latency (50-100ms delay)

CLAHE Enhancement

Contrast Limited Adaptive Histogram Equalization improves detection in poor lighting:

clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
enhanced = clahe.apply(gray_frame)

ROI-based Detection

Eyes are searched only in face region:

roi = frame[face_y:face_y+face_h, face_x:face_x+face_w]
eyes = detect_eyes(roi)  # Much faster than full frame

๐Ÿ”ฎ Future Enhancements

Phase 2: Deep Learning Integration

# Optional: Replace Haar Cascades with YOLO/RetinaFace
from ultralytics import YOLO
face_model = YOLO("yolov8n-face.pt")
faces = face_model.predict(frame)

Phase 3: Multi-person Support

  • Track multiple drivers simultaneously
  • Per-person thresholds and profiles

Phase 4: Advanced Analytics

  • Fatigue prediction (predicts drowsiness before critical)
  • Driving pattern analysis
  • Integration with CAN bus (vehicle data)

Phase 5: Cloud Sync

  • Upload alerts to backend
  • Mobile notifications
  • Fleet monitoring dashboard

๐Ÿ› Troubleshooting

Camera Not Detected

# Check available cameras
python -c "import cv2; print(cv2.VideoCapture(0).isOpened())"

# Try different camera indices
# In app.py, change: cap = cv2.VideoCapture(0)  โ†’  cap = cv2.VideoCapture(1)

Low FPS

  1. Increase skip_frames in settings (3-4)
  2. Reduce frame_width/frame_height in config
  3. Lower face_scale_factor to 1.05

False Positives (False Alerts)

  1. Increase eye_min_neighbors in config (7-8)
  2. Adjust eye_closed_ratio (0.5-0.6)
  3. Increase history_window (20-25)

Can't Find Eyes

  1. Ensure good lighting
  2. Remove glasses or adjust angle
  3. Lower eye_min_neighbors (3-4)
  4. Increase eye_sensitivity slider (15-18)

๐Ÿ“ Project Structure

pro-vision-v2/
โ”œโ”€โ”€ app.py                    # Streamlit dashboard (main entry point)
โ”œโ”€โ”€ detection_engine.py       # Core detection logic
โ”œโ”€โ”€ alert_system.py          # Alert & logging system
โ”œโ”€โ”€ config.json              # Tunable parameters
โ”œโ”€โ”€ requirements.txt         # Python dependencies
โ”œโ”€โ”€ vision_events.json       # Event log (auto-generated)
โ””โ”€โ”€ README.md               # This file

๐Ÿ” Data Privacy

  • No cloud transmission: All processing local
  • No persistent video: Frames not stored
  • Logs only: Timestamps and status changes
  • Opt-out: Disable logging in config

๐Ÿ“„ License

Free to use and modify. Built for educational use only.


๐Ÿค Contributing

Want to improve Pro-Vision?

  1. Better Detection: Experiment with different Haar Cascades
  2. New Features: Add blink rate alerts, fatigue prediction
  3. Performance: Optimize with GPU acceleration (CUDA)
  4. UI: Enhance Streamlit dashboard design

๐Ÿ“ž Support

  • Issues: Check troubleshooting section
  • Enhancement: Modify config.json for your use case
  • Integration: Adapt detection_engine.py for custom pipelines

๐Ÿš€ Deployment

Local Development

streamlit run app.py

Docker (Optional)

FROM python:3.9-slim
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
CMD ["streamlit", "run", "app.py"]

Cloud Deployment (Streamlit Cloud)

  1. Push repo to GitHub
  2. Go to streamlit.io/cloud
  3. Connect repo and deploy

๐Ÿ“Š Performance Benchmarks

Metric Value Notes
FPS 25-30 640x480, skip_frames=2
Latency 100-150ms Face detection + scoring
Memory ~200MB Python + OpenCV
CPU 30-40% Single core (i7 @ 2.6GHz)

Pro-Vision v2.0 - Built for Production โœ…

๐Ÿ‘ฅ Contributors

  • LABBAALLI Hamza
  • ID-BOUBRIK Abdelouahed
  • MAAROUF Yassine
  • IDDAHA Soumaya

Thanks to all contributors who have helped this project!

About

Real-Time Driver Monitoring & Edge-Vision Analytics

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages