Spaces:
Sleeping
Sleeping
File size: 3,199 Bytes
fad97ce | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | # 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
```bash
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
```
Frontend (only needed if modifying the React app):
```bash
npm install
npm run build
cp -r dist/* static/
```
## 3. Running
**Web application (API + frontend):**
```bash
uvicorn app:app --host 0.0.0.0 --port 7860
```
Open http://localhost:7860 in a browser.
**Live camera demo (OpenCV):**
```bash
python ui/live_demo.py
python ui/live_demo.py --xgb # XGBoost mode
```
**Training:**
```bash
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 `.npz` files in `data/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
1. **Face mesh** β MediaPipe 478-landmark detection
2. **Head pose** β solvePnP β yaw, pitch, roll, face score, gaze offset, head deviation
3. **Eye scorer** β EAR (left/right/avg), horizontal/vertical gaze ratio, MAR
4. **Temporal tracking** β PERCLOS, blink rate, closure duration, yawn duration
5. **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
|