IntegrationTest / others /README.md
Yingtao-Zheng's picture
Add general README into folder /others
fad97ce

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 .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