Skip to content

nishanth-kj/Sleep-Detection

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

21 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

😴 SleepSafe - AI-Powered Drowsiness Detection System

Version License Platform

Real-time drowsiness detection using computer vision and machine learning

Features β€’ Quick Start β€’ Architecture β€’ Documentation


🎯 Overview

SleepSafe is a cross-platform drowsiness detection ecosystem that prevents accidents caused by fatigue. Using advanced computer vision and AI, the system monitors eye closure patterns in real-time and triggers alerts when drowsiness is detected.

🌟 Key Highlights

  • 🌐 Offline-First Web App: Progressive Web App with TensorFlow.js for browser-based detection
  • πŸ“± Native Mobile Apps: iOS (Swift) and Android (Java) with shared Rust core
  • πŸ¦€ High-Performance Rust Core: Memory-safe, optimized logic shared across platforms
  • 🎨 Beautiful UI: Glassmorphism design with dark/light modes
  • πŸ”’ Privacy-Focused: All processing happens on-device, no data leaves your machine
  • ⚑ Real-Time Performance: Optimized for low-latency detection (< 100ms)

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                         SleepSafe Ecosystem                      β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                  β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚   Web PWA    β”‚  β”‚  Android App β”‚  β”‚     iOS App          β”‚  β”‚
β”‚  β”‚  (Next.js)   β”‚  β”‚   (Java)     β”‚  β”‚     (Swift)          β”‚  β”‚
β”‚  β”‚  TensorFlow  β”‚  β”‚     JNI      β”‚  β”‚       FFI            β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β”‚         β”‚                 β”‚                       β”‚              β”‚
β”‚         β”‚                 β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜              β”‚
β”‚         β”‚                             β”‚                          β”‚
β”‚         β”‚                      β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”               β”‚
β”‚         β”‚                      β”‚   Rust Core     β”‚               β”‚
β”‚         β”‚                      β”‚  (libsleep)     β”‚               β”‚
β”‚         β”‚                      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜               β”‚
β”‚         β”‚                                                        β”‚
β”‚    β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”            β”‚
β”‚    β”‚         MediaPipe Face Mesh                    β”‚            β”‚
β”‚    β”‚    (468 Facial Landmarks Detection)            β”‚            β”‚
β”‚    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜            β”‚
β”‚                                                                  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“‚ Project Structure

Sleep-Detection/
β”‚
β”œβ”€β”€ 🌐 web/                    # Next.js Progressive Web App
β”‚   β”œβ”€β”€ app/
β”‚   β”‚   β”œβ”€β”€ page.tsx           # Main detection interface
β”‚   β”‚   β”œβ”€β”€ layout.tsx         # App shell
β”‚   β”‚   └── globals.css        # Global styles
β”‚   β”œβ”€β”€ public/
β”‚   β”‚   β”œβ”€β”€ manifest.json      # PWA manifest
β”‚   β”‚   └── icons/             # App icons
β”‚   β”œβ”€β”€ package.json           # Dependencies
β”‚   └── Dockerfile             # Container config
β”‚
β”œβ”€β”€ 🐍 api/                    # FastAPI Backend
β”‚   β”œβ”€β”€ main.py                # API entry point
β”‚   β”œβ”€β”€ models/                # Pydantic models
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   └── models.py
β”‚   β”œβ”€β”€ services/              # Business logic
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   └── services.py
β”‚   β”œβ”€β”€ mlops/                 # ML training
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   └── train_model.py
β”‚   β”œβ”€β”€ db/                    # Database (Django-style)
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ database.py
β”‚   β”‚   β”œβ”€β”€ models.py
β”‚   β”‚   β”œβ”€β”€ sleepsafe.db       # SQLite database
β”‚   β”‚   └── postgres/          # PostgreSQL data (Docker)
β”‚   β”œβ”€β”€ pyproject.toml
β”‚   └── Dockerfile
β”‚
β”œβ”€β”€ πŸ¦€ core/                   # Rust Shared Library
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   └── lib.rs             # FFI/JNI exports
β”‚   └── Cargo.toml             # Rust dependencies
β”‚
β”œβ”€β”€ πŸ“± app/                    # Native Mobile Apps
β”‚   β”œβ”€β”€ android/               # Android Application
β”‚   β”‚   └── app/src/main/
β”‚   β”‚       β”œβ”€β”€ java/.../MainActivity.java
β”‚   β”‚       └── AndroidManifest.xml
β”‚   β”‚
β”‚   └── ios/                   # iOS Application
β”‚       └── SleepDetection/
β”‚           β”œβ”€β”€ ViewController.swift
β”‚           β”œβ”€β”€ AppDelegate.swift
β”‚           └── SleepCoreBridge.h  # C bridge for Rust
β”‚
β”œβ”€β”€ πŸ“¦ lib/                    # Future Libraries
β”‚   β”œβ”€β”€ npm/                   # (Planned) NPM package
β”‚   └── pypi/                  # (Planned) PyPI package
β”‚
β”œβ”€β”€ πŸ“š docs/                   # Documentation
β”‚   β”œβ”€β”€ ARCHITECTURE.md        # System design
β”‚   β”œβ”€β”€ DEPLOYMENT.md          # Deployment guide
β”‚   β”œβ”€β”€ DATABASE-STRUCTURE.md  # Database setup
β”‚   β”œβ”€β”€ BACKEND-COMPLETE.md    # Backend features
β”‚   └── DOCKER.md              # Docker guide
β”‚
β”œβ”€β”€ docker-compose.yml         # Multi-container orchestration
β”œβ”€β”€ .env.example               # Environment template
└── README.md                  # This file

πŸš€ Quick Start

Prerequisites

Component Requirement
Web Node.js 18+, npm 8+
Mobile Android Studio / Xcode
Rust Rust 1.70+ (for core compilation)

2️⃣ Backend API (Python + FastAPI)

The backend provides telemetry logging and MLOps features:

cd api

# Install dependencies
uv sync

# Run development server
uv run uvicorn main:app --reload

πŸ”§ API Docs: http://localhost:8000/docs

Features:

  • Detection event logging
  • Model metrics tracking
  • MLflow experiment tracking
  • Statistics and analytics
  • Database: api/db/sleepsafe.db (Django-style)

Endpoints:

  • POST /telemetry - Log detection event
  • GET /statistics - Get stats
  • GET /dashboard - Dashboard data
  • POST /metrics/model - Log model metrics

Note: Backend is fully functional with SQLite. PostgreSQL optional for production.

🌐 Web Application (Recommended)

The web app is fully functional and works offline:

# Clone repository
git clone https://github.com/nishanth-kj/Sleep-Detection.git
cd Sleep-Detection/web

# Install dependencies
npm install

# Start development server
npm run dev

πŸ“± Open http://localhost:3000 in your browser

How to Use:

  1. Click the camera icon to start detection
  2. Allow camera access when prompted
  3. Position your face in the webcam view
  4. Close your eyes for 3+ seconds to trigger the alarm
  5. Toggle dark/light mode with the moon/sun icon

πŸ“± Mobile Apps

Android Development

Step 1: Build Rust Core
cd core
rustup target add aarch64-linux-android
cargo install cargo-ndk
cargo ndk -t arm64-v8a --platform 24 build --release
Step 2: Copy Library
mkdir -p app/android/app/src/main/jniLibs/arm64-v8a
cp target/aarch64-linux-android/release/libsleep_core.so \
   app/android/app/src/main/jniLibs/arm64-v8a/
Step 3: Open in Android Studio
# Open app/android/ folder
android-studio app/android

Build and run on device or emulator.


iOS Development

Step 1: Build Rust Core
cd core
rustup target add aarch64-apple-ios x86_64-apple-ios
cargo install cargo-lipo
cargo lipo --release
Step 2: Configure Xcode
  1. Open app/ios/SleepDetection.xcodeproj in Xcode
  2. Add core/target/universal/release/libsleep_core.a to Link Binary With Libraries
  3. Set Objective-C Bridging Header to SleepDetection/SleepCoreBridge.h
  4. Build and run on device/simulator

🧠 How It Works

Detection Algorithm: Eye Aspect Ratio (EAR)

The system uses the Eye Aspect Ratio metric to detect eye closure:

       ||p2 - p6|| + ||p3 - p5||
EAR = ───────────────────────────
           2 Γ— ||p1 - p4||

Where p1...p6 are eye landmark coordinates

Detection Logic:

  • EAR > 0.25 β†’ Eyes OPEN βœ…
  • EAR < 0.25 for 10 consecutive frames (β‰ˆ 3 seconds) β†’ DROWSINESS DETECTED 🚨

Technology Stack

Web Frontend

  • Framework: Next.js 16.1 (React 19.2)
  • AI/ML: TensorFlow.js 4.22, MediaPipe Face Mesh 1.0
  • Styling: TailwindCSS 3.4, Framer Motion 12.23
  • PWA: next-pwa 5.6 for offline support
  • Utilities: react-webcam, lucide-react icons

Mobile Core

  • Language: Rust 2021 Edition
  • Build: Cargo with aggressive optimizations
  • Features:
    • opt-level = 3 - Maximum optimization
    • lto = true - Link-time optimization
    • codegen-units = 1 - Single compilation unit
    • panic = "abort" - Smaller binary size

Native Integrations

  • Android: JNI (Java Native Interface)
  • iOS: FFI (Foreign Function Interface) via C bridge

✨ Features

Core Functionality

βœ… Real-Time Face Detection

  • 468 facial landmarks tracked at 30 FPS
  • MediaPipe Face Mesh model (optimized for web)

βœ… Eye Closure Monitoring

  • Continuous EAR calculation for both eyes
  • Configurable threshold and frame count

βœ… Smart Alerting

  • Audio alarm using Web Audio API
  • Visual on-screen alerts
  • Mute/unmute toggle

βœ… Offline Capability

  • PWA with service worker caching
  • Install to home screen (mobile/desktop)
  • Works without internet after first load

βœ… Dark/Light Modes

  • System preference detection
  • Manual toggle
  • Smooth transitions

UI/UX

🎨 Modern Design

  • Glassmorphism effects
  • Smooth animations with Framer Motion
  • Responsive layout (mobile-first)

πŸ“Š Live Statistics

  • Current EAR value display
  • Online/offline indicator
  • FPS counter
  • Detection status

🐳 Docker Deployment

Note: Docker Compose currently references an empty api/ directory. To run only the web app: Run the complete stack with Docker:

docker compose up -d --build

Services:

Data Persistence:

  • PostgreSQL: api/db/postgres/
  • MLruns: api/mlruns/

Commands:

# Start all services
docker compose up -d

# View logs
docker compose logs -f

# Stop all
docker compose down

# Run ML training
docker compose --profile training up ml_training

πŸ“š Documentation

  • πŸ“– ARCHITECTURE.md - System design, diagrams, data flow
  • πŸš€ DEPLOYMENT.md - Detailed deployment instructions
  • πŸ’» Code Comments - Inline documentation in all source files

πŸ”’ Privacy & Security

On-Device Processing

  • NO data is sent to external servers
  • Facial landmarks processed locally
  • Web app works 100% offline

Data Storage

  • NO persistent storage of video/images
  • NO tracking or analytics
  • Optional browser cache for PWA only

Permissions

  • Camera: Required for face detection
  • Audio: For alarm playback (Web Audio API)

πŸ› οΈ Development

Web App Commands

npm run dev      # Start dev server
npm run build    # Build for production
npm run start    # Run production server
npm run lint     # Run ESLint

Rust Core Commands

cargo build --release  # Build optimized library
cargo test             # Run unit tests
cargo clippy           # Lint checks
cargo fmt              # Format code

Environment Variables

None required! The app works out-of-the-box.


πŸ› Known Issues & Roadmap

Current Status

Component Status
Web PWA βœ… Fully Functional
Rust Core βœ… Code Complete
Android App πŸ—οΈ Skeleton Code

πŸ› Current Status & TODOs

βœ… Complete & Working

  • Web PWA (Next.js + TensorFlow.js)
  • Backend API (FastAPI + SQLAlchemy)
  • Database (SQLite + PostgreSQL support)
  • MLOps (MLflow + training pipeline)
  • Docker setup (multi-container)
  • Documentation (comprehensive)

πŸ—οΈ In Progress

  • Compile Rust core for Android (libsleep_core.so)
  • Compile Rust core for iOS (libsleep_core.a)
  • Integrate Rust with mobile apps
  • Publish NPM package (lib/npm)
  • Publish PyPI package (lib/pypi)

πŸ“Š Database

  • Location: api/db/ (Django-style)
  • SQLite: api/db/sleepsafe.db
  • PostgreSQL: api/db/postgres/ (Docker)
  • Models: 4 tables (events, metrics, sessions, system) support
  • Customizable EAR thresholds
  • Bluetooth alerting (mobile)

🀝 Contributing

Contributions are welcome! Please follow these guidelines:

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

Development Setup

# Setup all components
npm install       # Web dependencies
cargo build       # Rust core

πŸ“„ License

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


πŸ™ Acknowledgments

  • MediaPipe team for Face Mesh model
  • TensorFlow.js for browser ML capabilities
  • Rust community for FFI/JNI tooling
  • Next.js team for the amazing framework

πŸ“§ Contact & Support


Made with ❀️ for safer roads and workplaces

⭐ Star this repo if you find it useful!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages