Skip to content

kad-link/Road-Sense

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

31 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿš— Road-Sense

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


๐Ÿ“‹ Table of Contents


๐ŸŽฏ Overview

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

โœจ Features

Core Features

  • 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

Additional Features

  • 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

๐Ÿ›  Tech Stack

Frontend

  • 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

Backend

  • 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

Machine Learning

  • Framework: Python with Flask & Flask-SocketIO
  • Model: YOLOv8 (Ultralytics)
  • Computer Vision: OpenCV
  • Dataset: Custom drowsiness detection dataset
  • Real-time Processing: Base64 frame encoding/decoding

๐Ÿ“ Project Structure

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

๐Ÿ“ฆ Prerequisites

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

Required API Keys & Services

  • Firebase credentials (for authentication)
  • Cloudinary account (for image storage)
  • Google Maps API key
  • Resend API key (for emails)
  • JWT Secret key

๐Ÿš€ Installation

1. Clone the Repository

git clone https://github.com/kad-link/Road-Sense.git
cd Road-Sense

2. Install Frontend Dependencies

cd client
npm install
cd ..

3. Install Backend Dependencies

cd server
npm install
cd ..

4. Install ML Dependencies

cd ml/Driver-Drowsiness-Detection
pip install -r requirements.txt
cd ../..

โš™๏ธ Configuration

Backend Configuration (.env)

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:5173

Frontend Configuration (.env)

Create 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_id

ML Server Configuration

The ML server runs on port 8800 by default. Update the connection URL in the frontend if needed.


๐ŸŽฎ Running the Application

Development Mode

Open three terminal windows and run each service:

Terminal 1 - Frontend:

cd client
npm run dev

Runs on http://localhost:5173

Terminal 2 - Backend:

cd server
node server.js

Runs on http://localhost:5000

Terminal 3 - ML Server:

cd ml/Driver-Drowsiness-Detection
python drowsiness_detector.py

Runs on http://localhost:8800

Production Build

Frontend:

cd client
npm run build
npm run preview

Backend:

cd server
# Ensure all environment variables are set
node server.js

ML Server:

cd ml/Driver-Drowsiness-Detection
python drowsiness_detector.py

๐Ÿ“ก API Endpoints

Authentication

  • POST /registeruser - Register new user
  • POST /loginuser - User login
  • POST /logout - User logout

Trip Management

  • POST /starttrip - Start a new trip
  • POST /endtrip - End an active trip
  • GET /usertrips - Get all user trips
  • GET /tripdetails/:tripId - Get trip details

Vehicle Documents

  • POST /uploaddocs - Upload vehicle documents
  • GET /getdocs - Retrieve vehicle documents

Scoring

  • POST /addscore - Add safety score
  • GET /getscore - Get user scores

User Profile

  • GET /userprofile - Get user information
  • POST /updateprofile - Update user profile

๐Ÿง  ML Model Details

Drowsiness Detection Model

Model Architecture: YOLOv8 Nano (efficient for real-time processing)

Classes Detected:

  • asleep - Eyes closed for an extended period
  • drowsy - Eyes closing, head nodding, reduced alertness
  • alert - 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

Frame Processing Pipeline

  1. Receive base64-encoded frame from frontend
  2. Decode to numpy array
  3. Run YOLOv8 inference
  4. Detect drowsiness/asleep states
  5. Draw bounding boxes and annotations
  6. Encode annotated frame back to base64
  7. Send results with alert status

๐Ÿ— Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                    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                                   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ“ License

This project is licensed under the ISC License โ€“ see the LICENSE file for details.


๐Ÿ”— Links


Made with โค๏ธ for safer roads

About

Resources

Stars

Watchers

Forks

Releases

No releases published

Contributors