A real-time driver drowsiness detection system that uses AI-powered computer vision to enhance road safety. Road-Sense monitors driver alertness and provides immediate alerts when drowsiness is detected, helping prevent accidents caused by driver fatigue.
Live Demo: road-sense-coral.vercel.app
- Overview
- Features
- Tech Stack
- Project Structure
- Prerequisites
- Installation
- Configuration
- Running the Application
- API Endpoints
- ML Model Details
- Architecture
- License
- Links
Road-Sense is an intelligent transportation safety system designed to detect and alert drivers about drowsiness in real time. By leveraging YOLOv8 computer vision technology and real-time WebSocket communication, the application processes live camera feeds to identify drowsy or asleep states and triggers immediate audio/visual alerts.
This full-stack application combines:
- Frontend: A modern React-based web interface for live monitoring
- Backend: An Express.js server for user management and trip tracking
- ML Pipeline: A Python-based drowsiness detection engine using YOLOv8
- Real-time Drowsiness Detection: Uses YOLOv8 to detect drowsy and asleep states
- Live Camera Feed Processing: Processes webcam input at real-time speeds
- Instant Alerts: Visual and audio alerts triggered within 2 seconds of drowsiness detection
- Trip Tracking: Records trips with start/end locations and safety scores
- User Authentication: Secure user registration and JWT-based authentication
- Safety Score System: Calculates driver safety metrics based on drowsiness incidents
- Vehicle Documentation: Upload and store vehicle-related documents
- Real-time Communication: WebSocket-based frame transmission and results
- Google Maps integration for location tracking
- Document upload with Cloudinary storage
- Email notifications via Resend
- User profile management
- Responsive UI with Chakra UI and Tailwind CSS
- Framework: React 19.1.1 with Vite
- UI Libraries: Chakra UI, Tailwind CSS, Framer Motion
- State Management: React Context API
- Maps: Google Maps API integration
- Communication: Socket.io for real-time updates
- HTTP Client: Axios
- Icons: Lucide React, React Icons
- Server: Express.js 5.1.0
- Database: MongoDB with Mongoose ODM
- Authentication: JWT (JSON Web Tokens)
- Password Security: bcrypt, bcryptjs
- File Upload: Multer with Cloudinary storage
- Email: Resend API
- Real-time: Socket.io
- Database Automation: Puppeteer for web automation
- Framework: Python with Flask & Flask-SocketIO
- Model: YOLOv8 (Ultralytics)
- Computer Vision: OpenCV
- Dataset: Custom drowsiness detection dataset
- Real-time Processing: Base64 frame encoding/decoding
Road-Sense/
โโโ client/ # Frontend React application
โ โโโ src/
โ โ โโโ components/ # React components
โ โ โโโ pages/ # Page components
โ โ โโโ context/ # Context API setup
โ โ โโโ assets/ # Static assets
โ โ โโโ App.jsx
โ โ โโโ main.jsx
โ โโโ package.json
โ โโโ vite.config.js
โ โโโ index.html
โ
โโโ server/ # Backend Express application
โ โโโ db/
โ โ โโโ models/
โ โ โ โโโ user.js # User schema
โ โ โ โโโ trips.js # Trip tracking schema
โ โ โ โโโ score.js # Safety score schema
โ โ โ โโโ vehicleDocuments.js # Document storage schema
โ โ โโโ dbConnect.js # MongoDB connection
โ โโโ middleware/
โ โ โโโ auth.js # JWT authentication
โ โ โโโ multer.js # File upload middleware
โ โโโ config/ # Configuration files
โ โโโ server.js # Main server file
โ โโโ package.json
โ
โโโ ml/
โ โโโ Driver-Drowsiness-Detection/
โ โโโ drowsiness_detector.py # Flask ML server
โ โโโ requirements.txt # Python dependencies
โ โโโ dataset.yaml # Dataset configuration
โ โโโ runs/ # Model training outputs
โ
โโโ README.md
Before you begin, ensure you have the following installed:
- Node.js: v16 or higher
- Python: v3.8 or higher
- MongoDB: Local or cloud instance (e.g., MongoDB Atlas)
- Git: For cloning the repository
- Firebase credentials (for authentication)
- Cloudinary account (for image storage)
- Google Maps API key
- Resend API key (for emails)
- JWT Secret key
git clone https://github.com/kad-link/Road-Sense.git
cd Road-Sensecd client
npm install
cd ..cd server
npm install
cd ..cd ml/Driver-Drowsiness-Detection
pip install -r requirements.txt
cd ../..Create a .env file in the server/ directory:
# Database
MONGODB_URI=your_mongodb_connection_string
# JWT
JWT_SECRET=your_jwt_secret_key
# Firebase
FIREBASE_PROJECT_ID=your_firebase_project_id
FIREBASE_CLIENT_EMAIL=your_firebase_client_email
FIREBASE_PRIVATE_KEY=your_firebase_private_key
# Cloudinary
CLOUDINARY_NAME=your_cloudinary_name
CLOUDINARY_API_KEY=your_cloudinary_api_key
CLOUDINARY_API_SECRET=your_cloudinary_api_secret
# Email
RESEND_API_KEY=your_resend_api_key
# Server
PORT=5000
CORS_ORIGIN=http://localhost:5173Create a .env file in the client/ directory:
VITE_BACKEND_URL=http://localhost:5000
VITE_GOOGLE_MAPS_API_KEY=your_google_maps_api_key
VITE_FIREBASE_API_KEY=your_firebase_api_key
VITE_FIREBASE_PROJECT_ID=your_firebase_project_idThe ML server runs on port 8800 by default. Update the connection URL in the frontend if needed.
Open three terminal windows and run each service:
Terminal 1 - Frontend:
cd client
npm run devRuns on http://localhost:5173
Terminal 2 - Backend:
cd server
node server.jsRuns on http://localhost:5000
Terminal 3 - ML Server:
cd ml/Driver-Drowsiness-Detection
python drowsiness_detector.pyRuns on http://localhost:8800
Frontend:
cd client
npm run build
npm run previewBackend:
cd server
# Ensure all environment variables are set
node server.jsML Server:
cd ml/Driver-Drowsiness-Detection
python drowsiness_detector.pyPOST /registeruser- Register new userPOST /loginuser- User loginPOST /logout- User logout
POST /starttrip- Start a new tripPOST /endtrip- End an active tripGET /usertrips- Get all user tripsGET /tripdetails/:tripId- Get trip details
POST /uploaddocs- Upload vehicle documentsGET /getdocs- Retrieve vehicle documents
POST /addscore- Add safety scoreGET /getscore- Get user scores
GET /userprofile- Get user informationPOST /updateprofile- Update user profile
Model Architecture: YOLOv8 Nano (efficient for real-time processing)
Classes Detected:
asleep- Eyes closed for an extended perioddrowsy- Eyes closing, head nodding, reduced alertnessalert- Normal awake state
Key Features:
- Confidence threshold: 0.4
- Alert trigger: Drowsiness detected for โฅ 2 seconds
- Real-time inference at 30+ FPS
- Lightweight model (~7MB) for fast processing
- Custom-trained on drowsiness detection dataset
Model Training:
- Framework: Ultralytics YOLOv8
- Training output:
ml/Driver-Drowsiness-Detection/runs/detect/train/weights/last.pt - Automatic fallback to base YOLOv8 if custom model not found
- Receive base64-encoded frame from frontend
- Decode to numpy array
- Run YOLOv8 inference
- Detect drowsiness/asleep states
- Draw bounding boxes and annotations
- Encode annotated frame back to base64
- Send results with alert status
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ FRONTEND (React + Vite) โ
โ - User Interface โ
โ - Camera Feed Display โ
โ - Real-time Alert System โ
โ - Trip Management UI โ
โโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ HTTP/REST & WebSocket (Socket.io)
โ
โโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ BACKEND (Express + MongoDB) โ
โ - User Authentication (JWT) โ
โ - Trip Tracking โ
โ - Safety Scoring โ
โ - Document Management โ
โ - Email Notifications โ
โโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ WebSocket Connection
โ
โโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ML SERVER (Flask + YOLOv8 + Socket.io) โ
โ - Real-time Frame Processing โ
โ - Drowsiness Detection โ
โ - Alert Generation โ
โ - Annotated Frame Output โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
This project is licensed under the ISC License โ see the LICENSE file for details.
- Live Application: road-sense-coral.vercel.app
- Repository: github.com/kad-link/Road-Sense
- Issues: Report Issues
Made with โค๏ธ for safer roads