A production-quality web application that monitors gamer fatigue in real-time using behavioral tracking, eye fatigue detection, and machine learning to compute a Cognitive Drift Score (CDS).
- Behavioral Tracking: Monitor keyboard and mouse activity plus reaction time
- Eye Fatigue Detection: Track blink rate and Eye Aspect Ratio (EAR)
- Cognitive Drift Score (CDS): ML-powered real-time fatigue scoring (0-100)
- State Classification: FOCUS, MILD_DRIFT, HIGH_DRIFT, FATIGUE
- Professional Dashboard: Modern UI with real-time charts and gauges
- Session Management: Track and review past sessions
- Smart Alerts: Notifications when fatigue levels are concerning
- Python 3.x
- FastAPI
- scikit-learn (RandomForest classifier)
- NumPy, Pandas
- Uvicorn
- React 18 + TypeScript
- Vite
- Tailwind CSS
- Recharts
- Axios
- Lucide React (icons)
ml/
├── backend/
│ ├── main.py # FastAPI application
│ ├── models.py # Pydantic models
│ ├── train_model.py # ML training script
│ ├── ml_service.py # ML prediction service
│ ├── requirements.txt # Python dependencies
│ └── ml_models/ # Trained model storage (auto-created)
│
└── frontend/
├── src/
│ ├── components/ # React components
│ ├── pages/ # Page components
│ ├── services/ # API service layer
│ ├── utils/ # Helper functions
│ ├── types.ts # TypeScript interfaces
│ ├── App.tsx # Main app component
│ └── main.tsx # Entry point
├── package.json
├── vite.config.ts
└── tailwind.config.js
- Python 3.8 or higher
- Node.js 18+ and npm
- Modern web browser with webcam support
- Navigate to the backend directory:
cd backend- Install Python dependencies:
pip install -r requirements.txt- Train the ML model (optional - will auto-train on first run):
python train_model.py- Start the FastAPI server:
python main.pyOr using uvicorn directly:
uvicorn main:app --reload --host 0.0.0.0 --port 8000The backend API will be available at http://localhost:8000
- Navigate to the frontend directory:
cd frontend- Install dependencies:
npm install- Start the development server:
npm run devThe frontend will be available at http://localhost:5173
- Open http://localhost:5173 in your browser
- Click "Start Monitoring Session" on the landing page
- Allow webcam access when prompted
- The system will begin tracking:
- Keyboard activity (keys per minute)
- Mouse movement (events per minute)
- Eye metrics (blink rate, EAR)
- Real-time CDS calculation
-
CDS Gauge: Shows your current Cognitive Drift Score (0-100)
- 0-30: FOCUS (green)
- 30-55: MILD_DRIFT (yellow)
- 55-80: HIGH_DRIFT (orange)
- 80-100: FATIGUE (red)
-
Real-time Charts: Visualize metrics over time
-
Alert Banner: Appears when HIGH_DRIFT or FATIGUE detected
-
Current Metrics: Live display of all tracked values
- Navigate to "Sessions" in the navigation menu
- View all past sessions with statistics
- Click "View Details" to see detailed charts and metrics
POST /api/session/start- Start new sessionPOST /api/session/metrics- Send metrics and get CDSPOST /api/session/end- End sessionGET /api/sessions- Get all sessionsGET /api/sessions/{session_id}- Get session details
GET /api/model/info- Get model informationPOST /api/model/train- Retrain the model
The system uses a RandomForest classifier trained on synthetic data with the following features:
- reaction_time_ms: Response time in milliseconds
- key_rate: Keyboard activity per minute
- mouse_rate: Mouse events per minute
- blink_rate: Blinks per minute
- ear: Eye Aspect Ratio (0.15-0.35)
The model outputs:
- Predicted state: FOCUS, MILD_DRIFT, HIGH_DRIFT, or FATIGUE
- CDS score: Weighted probability score (0-100)
Frontend:
cd frontend
npm run buildThe built files will be in frontend/dist/
Backend:
cd backend
python train_model.py # Verify ML pipelineEdit backend/ml_service.py to modify the CDS-to-state mapping:
if cds < 30:
state = "FOCUS"
elif cds < 55:
state = "MILD_DRIFT"
# ... etcEdit frontend/tailwind.config.js to customize colors:
colors: {
'gaming-dark': '#0a0e27',
'gaming-purple': '#8b5cf6',
// ... add your colors
}- Ensure browser has camera permissions
- Use HTTPS or localhost (required for getUserMedia)
- Check if another application is using the camera
- Verify backend is running on port 8000
- Check CORS settings in
backend/main.py - Ensure frontend API URL matches in
frontend/src/services/api.ts
- Run
python train_model.pyto generate model files - Check
ml_models/directory exists - Verify sklearn version compatibility
- Integrate actual eye-tracking library (e.g., WebGazer.js)
- Add WebSocket support for lower latency
- Persistent database (SQLite/PostgreSQL)
- User authentication and profiles
- Exportable session reports (PDF)
- Mobile app support
- Customizable alert sounds
This project is provided as-is for educational and personal use.
Built with ❤️ for gamers who need to stay sharp during overnight sessions.