Spaces:
Sleeping
Sleeping
FocusGuard
Real-time webcam-based focus detection system combining geometric feature extraction with machine learning classification. The pipeline extracts 17 facial features (EAR, gaze, head pose, PERCLOS, blink rate, etc.) from MediaPipe landmarks and classifies attentiveness using MLP and XGBoost models. Served via a React + FastAPI web application with live WebSocket video.
1. Project Structure
βββ data/ Raw collected sessions (collected_<name>/*.npz)
βββ data_preparation/ Data loading, cleaning, and exploration
βββ notebooks/ Training notebooks (MLP, XGBoost) with LOPO evaluation
βββ models/ Feature extraction modules and training scripts
βββ checkpoints/ All saved weights (mlp_best.pt, xgboost_*_best.json, GRU, scalers)
βββ evaluation/ Training logs and metrics (JSON)
βββ ui/ Live OpenCV demo and inference pipeline
βββ src/ React/Vite frontend source
βββ static/ Built frontend (served by FastAPI)
βββ app.py / main.py FastAPI backend (API, WebSocket, DB)
βββ requirements.txt Python dependencies
βββ package.json Frontend dependencies
2. Setup
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
Frontend (only needed if modifying the React app):
npm install
npm run build
cp -r dist/* static/
3. Running
Web application (API + frontend):
uvicorn app:app --host 0.0.0.0 --port 7860
Open http://localhost:7860 in a browser.
Live camera demo (OpenCV):
python ui/live_demo.py
python ui/live_demo.py --xgb # XGBoost mode
Training:
python -m models.mlp.train # MLP
python -m models.xgboost.train # XGBoost
4. Dataset
- 9 participants, each recorded via webcam with real-time labelling (focused / unfocused)
- 144,793 total samples, 10 selected features, binary classification
- Collected using
python -m models.collect_features --name <name> - Stored as
.npzfiles indata/collected_<name>/
5. Models
| Model | Test Accuracy | Test F1 | ROC-AUC |
|---|---|---|---|
| XGBoost (600 trees, depth 8, lr 0.149) | 95.87% | 0.959 | 0.991 |
| MLP (64β32, 30 epochs, lr 1e-3) | 92.92% | 0.929 | 0.971 |
Both evaluated on a held-out 15% stratified test split. LOPO (Leave-One-Person-Out) cross-validation available in notebooks/.
6. Feature Pipeline
- Face mesh β MediaPipe 478-landmark detection
- Head pose β solvePnP β yaw, pitch, roll, face score, gaze offset, head deviation
- Eye scorer β EAR (left/right/avg), horizontal/vertical gaze ratio, MAR
- Temporal tracking β PERCLOS, blink rate, closure duration, yawn duration
- Classification β 10-feature vector β MLP or XGBoost β focused / unfocused
7. Tech Stack
- Backend: Python, FastAPI, WebSocket, aiosqlite
- Frontend: React, Vite, TypeScript
- ML: PyTorch (MLP), XGBoost, scikit-learn
- Vision: MediaPipe, OpenCV