Spaces:
Sleeping
Sleeping
Commit Β·
d209694
1
Parent(s): 59d3081
Put all the models together (expect UI)
Browse filesThis view is limited to 50 files because it contains too many changes. Β See raw diff
Files changed (50)
- .gitignore+29-8
- DockerfileAdded+27
- README.md+87-6
- app.pyAdded+1
- checkpoints/hybrid_focus_config.jsonAdded+10
- MLP/models/meta_20260224_024200.npz β checkpoints/meta_best.npzRenamed+2-2
- MLP/models/mlp_20260224_024200.joblib β checkpoints/mlp_best.ptRenamed+2-2
- MLP/models/scaler_20260224_024200.joblib β checkpoints/scaler_best.joblibRenamed+2-2
- checkpoints/xgboost_face_orientation_best.jsonAdded
- data_preparation/CNN/eye_crops/val/open/.gitkeepDeleted-1
- data_preparation/MLP/explore_collected_data.ipynbDeleted
- data_preparation/MLP/train_mlp.ipynbDeleted
- data_preparation/README.md+61-27
- models/geometric/__init__.py β data_preparation/__init__.pyRenamed
- data_preparation/data_exploration.ipynbAdded
- data_preparation/prepare_dataset.pyAdded+232
- docker-compose.ymlAdded+5
- eslint.config.jsAdded+29
- evaluation/README.md+45-2
- index.htmlAdded+17
- main.pyAdded+964
- models/README.md+51-8
- models/attention/__init__.py β models/__init__.pyRenamed
- models/attention/classifier.pyDeleted
- models/attention/fusion.pyDeleted
- models/attention/train.pyDeleted
- models/cnn/notebooks/EyeCNN.ipynbAdded+107
- models/cnn/notebooks/EyeCNN_Train_Evaluate_new.ipynbAdded
- models/cnn/notebooks/EyeCNN_Training_Evaluate.ipynbAdded
- models/cnn/notebooks/README.mdAdded+1
- models/attention/collect_features.py β models/collect_features.pyRenamed+26-19
- models/eye_classifier.pyAdded+69
- models/eye_crop.pyAdded+77
- models/geometric/eye_behaviour/eye_scorer.py β models/eye_scorer.pyRenamed+7-3
- models/pretrained/face_mesh/face_mesh.py β models/face_mesh.pyRenamed+6-3
- models/geometric/eye_behaviour/__init__.pyDeleted
- models/geometric/face_orientation/__init__.pyDeleted-1
- models/geometric/face_orientation/head_pose.py β models/head_pose.pyRenamed+10-1
- models/mlp/eval_accuracy.pyAdded+54
- models/mlp/sweep.pyAdded+66
- models/mlp/train.py+53-32
- models/prepare_dataset.pyDeleted-91
- models/pretrained/__init__.pyDeleted
- models/pretrained/face_mesh/.gitkeepDeleted
- models/pretrained/face_mesh/__init__.pyDeleted
- models/xgboost/add_accuracy.pyAdded+60
- models/xgboost/checkpoints/face_orientation_best.jsonAdded
- models/xgboost/eval_accuracy.pyAdded+54
- models/xgboost/fetch_sweep_results.pyAdded+46
- models/xgboost/sweep.pyAdded+135
112345678910111213141516171819202122232425264 unmodified lines313233123414151617181935363738394041__pycache__/# Logslogs*.lognpm-debug.log*yarn-debug.log*yarn-error.log*pnpm-debug.log*lerna-debug.log*
node_modules/dist/dist-ssr/*.local
# Editor directories and files.vscode/.idea/.DS_Store*.suo*.ntvs**.njsproj*.sln*.sw?*.py[cod]*$py.class*.so4 unmodified lines.env*.egg-info/.eggs/dist/build/.idea/.vscode/*.swp*.swodocs/.DS_StoreThumbs.db
# Project specificfocus_guard.dbstatic/__pycache__/docs/123456789101112131415161718192021222324252627FROM python:3.10-slim
RUN useradd -m -u 1000 userENV HOME=/home/user PATH=/home/user/.local/bin:$PATH
ENV PYTHONUNBUFFERED=1
WORKDIR /app
RUN apt-get update && apt-get install -y --no-install-recommends libglib2.0-0 libsm6 libxrender1 libxext6 libxcb1 libgl1 libgomp1 ffmpeg libavcodec-dev libavformat-dev libavutil-dev libswscale-dev libavdevice-dev libopus-dev libvpx-dev libsrtp2-dev build-essential nodejs npm && rm -rf /var/lib/apt/lists/*
COPY requirements.txt ./RUN pip install --no-cache-dir -r requirements.txt
COPY . .
RUN npm install && npm run build && mkdir -p /app/static && cp -R dist/* /app/static/
ENV FOCUSGUARD_CACHE_DIR=/app/.cache/focusguardRUN python -c "from models.face_mesh import _ensure_model; _ensure_model()"
RUN mkdir -p /app/data && chown -R user:user /app
USER userEXPOSE 7860
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860", "--log-level", "debug"]123345678561078910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091# FocusGuard
Webcam-based focus detection: face mesh, head pose, eye (geometry or YOLO), plus an MLP trained on collected features.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.
- **data_preparation/** β collect data, notebooks, processed/collected files- **models/** β face mesh, head pose, eye scorer, YOLO classifier, MLP training, attention feature collection- **evaluation/** β metrics and run logs- **ui/** β live demo (geometry+YOLO or MLP-only)## 1. Project Structure
Run from here: `pip install -r requirements.txt` then `python ui/live_demo.py` or `python ui/live_demo.py --mlp`.```βββ 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
```bashpython -m venv venvsource venv/bin/activatepip install -r requirements.txt```
Frontend (only needed if modifying the React app):
```bashnpm installnpm run buildcp -r dist/* static/```
## 3. Running
**Web application (API + frontend):**
```bashuvicorn app:app --host 0.0.0.0 --port 7860```
Open http://localhost:7860 in a browser.
**Live camera demo (OpenCV):**
```bashpython ui/live_demo.pypython ui/live_demo.py --xgb # XGBoost mode```
**Training:**
```bashpython -m models.mlp.train # MLPpython -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 detection2. **Head pose** β solvePnP β yaw, pitch, roll, face score, gaze offset, head deviation3. **Eye scorer** β EAR (left/right/avg), horizontal/vertical gaze ratio, MAR4. **Temporal tracking** β PERCLOS, blink rate, closure duration, yawn duration5. **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, OpenCV1from main import app12345678910{ "w_mlp": 0.6000000000000001, "w_geo": 0.3999999999999999, "threshold": 0.35, "use_yawn_veto": true, "geo_face_weight": 0.4, "geo_eye_weight": 0.6, "mar_yawn_threshold": 0.55, "metric": "f1"}12323version https://git-lfs.github.com/spec/v1oid sha256:769bb62c7bf04aafd808e9b2623e795c2d92bcb933313ebf553d6fce5ebe7143size 1616oid sha256:5d78d1df5e25536a2c82c4b8f5fd0c26dd35f44b28fd59761634cbf78c7546f8size 419612323version https://git-lfs.github.com/spec/v1oid sha256:a72933fcf2d0aed998c6303ea4298c04618d937c7f17bf492e76efcf3b4b54d7size 50484oid sha256:c2f55129785b6882c304483aa5399f5bf6c9ed6e73dfec7ca6f36cd0436156c8size 1449712323version https://git-lfs.github.com/spec/v1oid sha256:3f9ef3721cee28f1472886556e001d0f6ed0abe09011d979a70ca9bf447d453esize 823oid sha256:02ed6b4c0d99e0254c6a740a949da2384db58ec7d3e6df6432b9bfcd3a296c71size 7831
112334567569789101111121312131514151617181920212223181920212425232426272826293028293132313334333536353738394041373839404142434445464748495051525354555657585960616263646566676869707172737475# Data Preparation# data_preparation/
## Folder StructureShared data loading, cleaning, and exploratory analysis.
### collected/Contains raw session files in `.npz` format. Generated using:## 1. Files
python -m models.attention.collect_features| File | Description ||------|-------------|| `prepare_dataset.py` | Central data loading module used by all training scripts and notebooks || `data_exploration.ipynb` | EDA notebook: feature distributions, class balance, correlations |
Each session includes:- 17-dimensional feature vectors- Corresponding labels## 2. prepare_dataset.py
---Provides a consistent pipeline for loading raw `.npz` data from `data/`:
| Function | Purpose ||----------|---------|| `load_all_pooled(model_name)` | Load all participants, clean, select features, concatenate || `load_per_person(model_name)` | Load grouped by person (for LOPO cross-validation) || `get_numpy_splits(model_name)` | Load + stratified 70/15/15 split + StandardScaler || `get_dataloaders(model_name)` | Same as above, wrapped in PyTorch DataLoaders || `_split_and_scale(features, labels, ...)` | Reusable split + optional scaling |
### MLP/Contains notebooks for:- Exploring collected data- Training the sklearn MLP model (10 features)### Cleaning rules
Trained models are saved to:../MLP/models/- `yaw` clipped to [-45, 45], `pitch`/`roll` to [-30, 30]- `ear_left`, `ear_right`, `ear_avg` clipped to [0, 0.85]
---### Selected features (face_orientation)
### CNN/Eye crop directory structure for CNN training (YOLO).`head_deviation`, `s_face`, `s_eye`, `h_gaze`, `pitch`, `ear_left`, `ear_avg`, `ear_right`, `gaze_offset`, `perclos`
---## 3. data_exploration.ipynb
## Collecting DataRun from this folder or from the project root. Covers:
**Step-by-step**1. Per-feature statistics (mean, std, min, max)2. Class distribution (focused vs unfocused)3. Feature histograms and box plots4. Correlation matrix
1. From repo root Install deps: `pip install -r requirements.txt`.3. Run: `python -m models.attention.collect_features --name yourname`.4. Webcam opens. Look at the camera; press **1** when focused, **0** when unfocused. Switch every 10β30 sec so you get both labels.5. Press **p** to pause/resume.6. Press **q** when done. One `.npz` is saved to `data_preparation/collected/` (17 features + labels).## 4. How to run
`prepare_dataset.py` is a **library module**, not a standalone script. You donβt run it directly; you import it from code that needs data.
**From repo root:**
```bash# Optional: quick test that loading workspython -c "from data_preparation.prepare_dataset import load_all_pooledX, y, names = load_all_pooled('face_orientation')print(f'Loaded {X.shape[0]} samples, {X.shape[1]} features: {names}')"```
**Used by:**
- `python -m models.mlp.train`- `python -m models.xgboost.train`- `notebooks/mlp.ipynb`, `notebooks/xgboost.ipynb`- `data_preparation/data_exploration.ipynb`
## 5. Usage (in code)
```pythonfrom data_preparation.prepare_dataset import load_all_pooled, get_numpy_splits
# pooled dataX, y, names = load_all_pooled("face_orientation")
# ready-to-train splitssplits, n_features, n_classes, scaler = get_numpy_splits("face_orientation")X_train, y_train = splits["X_train"], splits["y_train"]```123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232import osimport glob
import numpy as npfrom sklearn.preprocessing import StandardScalerfrom sklearn.model_selection import train_test_split
try: import torch from torch.utils.data import Dataset, DataLoaderexcept ImportError: # pragma: no cover torch = None
class Dataset: # type: ignore pass
class _MissingTorchDataLoader: # type: ignore def __init__(self, *args, **kwargs): raise ImportError( "PyTorch not installed" )
DataLoader = _MissingTorchDataLoader # type: ignore
DATA_DIR = os.path.join(os.path.dirname(__file__), "..", "data")
SELECTED_FEATURES = { "face_orientation": [ 'head_deviation', 's_face', 's_eye', 'h_gaze', 'pitch', 'ear_left', 'ear_avg', 'ear_right', 'gaze_offset', 'perclos' ], "eye_behaviour": [ 'ear_left', 'ear_right', 'ear_avg', 'mar', 'blink_rate', 'closure_duration', 'perclos', 'yawn_duration' ]}
class FeatureVectorDataset(Dataset): def __init__(self, features: np.ndarray, labels: np.ndarray): self.features = torch.tensor(features, dtype=torch.float32) self.labels = torch.tensor(labels, dtype=torch.long)
def __len__(self): return len(self.labels)
def __getitem__(self, idx): return self.features[idx], self.labels[idx]
# ββ Low-level helpers ββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _clean_npz(raw, names): """Apply clipping rules in-place. Shared by all loaders.""" for col, lo, hi in [('yaw', -45, 45), ('pitch', -30, 30), ('roll', -30, 30)]: if col in names: raw[:, names.index(col)] = np.clip(raw[:, names.index(col)], lo, hi) for feat in ['ear_left', 'ear_right', 'ear_avg']: if feat in names: raw[:, names.index(feat)] = np.clip(raw[:, names.index(feat)], 0, 0.85) return raw
def _load_one_npz(npz_path, target_features): """Load a single .npz file, clean and select features. Returns (X, y, selected_feature_names).""" data = np.load(npz_path, allow_pickle=True) raw = data['features'].astype(np.float32) labels = data['labels'].astype(np.int64) names = list(data['feature_names']) raw = _clean_npz(raw, names) selected = [f for f in target_features if f in names] idx = [names.index(f) for f in selected] return raw[:, idx], labels, selected
# ββ Public data loaders ββββββββββββββββββββββββββββββββββββββββββββββββββ
def load_all_pooled(model_name: str = "face_orientation", data_dir: str = None): """Load all collected_*/*.npz, clean, select features, concatenate.
Returns (X_all, y_all, all_feature_names). """ data_dir = data_dir or DATA_DIR target_features = SELECTED_FEATURES.get(model_name, SELECTED_FEATURES["face_orientation"]) pattern = os.path.join(data_dir, "collected_*", "*.npz") npz_files = sorted(glob.glob(pattern))
if not npz_files: print("[DATA] Warning: No .npz files found. Falling back to synthetic.") X, y = _generate_synthetic_data(model_name) return X, y, target_features
all_X, all_y = [], [] all_names = None for npz_path in npz_files: X, y, names = _load_one_npz(npz_path, target_features) if all_names is None: all_names = names all_X.append(X) all_y.append(y) print(f"[DATA] + {os.path.basename(npz_path)}: {X.shape[0]} samples")
X_all = np.concatenate(all_X, axis=0) y_all = np.concatenate(all_y, axis=0) print(f"[DATA] Loaded {len(npz_files)} file(s) for '{model_name}': " f"{X_all.shape[0]} total samples, {X_all.shape[1]} features") return X_all, y_all, all_names
def load_per_person(model_name: str = "face_orientation", data_dir: str = None): """Load collected_*/*.npz grouped by person (folder name).
Returns dict { person_name: (X, y) } where X/y are per-person numpy arrays. Also returns (X_all, y_all) as pooled data. """ data_dir = data_dir or DATA_DIR target_features = SELECTED_FEATURES.get(model_name, SELECTED_FEATURES["face_orientation"]) pattern = os.path.join(data_dir, "collected_*", "*.npz") npz_files = sorted(glob.glob(pattern))
if not npz_files: raise FileNotFoundError(f"No .npz files matching {pattern}")
by_person = {} all_X, all_y = [], [] for npz_path in npz_files: folder = os.path.basename(os.path.dirname(npz_path)) person = folder.replace("collected_", "", 1) X, y, _ = _load_one_npz(npz_path, target_features) all_X.append(X) all_y.append(y) if person not in by_person: by_person[person] = [] by_person[person].append((X, y)) print(f"[DATA] + {person}/{os.path.basename(npz_path)}: {X.shape[0]} samples")
for person, chunks in by_person.items(): by_person[person] = ( np.concatenate([c[0] for c in chunks], axis=0), np.concatenate([c[1] for c in chunks], axis=0), )
X_all = np.concatenate(all_X, axis=0) y_all = np.concatenate(all_y, axis=0) print(f"[DATA] {len(by_person)} persons, {X_all.shape[0]} total samples, {X_all.shape[1]} features") return by_person, X_all, y_all
def load_raw_npz(npz_path): """Load a single .npz without cleaning or feature selection. For exploration notebooks.""" data = np.load(npz_path, allow_pickle=True) features = data['features'].astype(np.float32) labels = data['labels'].astype(np.int64) names = list(data['feature_names']) return features, labels, names
# ββ Legacy helpers (used by models/mlp/train.py and models/xgboost/train.py) β
def _load_real_data(model_name: str): X, y, _ = load_all_pooled(model_name) return X, y
def _generate_synthetic_data(model_name: str): target_features = SELECTED_FEATURES.get(model_name, SELECTED_FEATURES["face_orientation"]) n = 500 d = len(target_features) c = 2 rng = np.random.RandomState(42) features = rng.randn(n, d).astype(np.float32) labels = rng.randint(0, c, size=n).astype(np.int64) print(f"[DATA] Using synthetic data for '{model_name}': {n} samples, {d} features, {c} classes") return features, labels
def _split_and_scale(features, labels, split_ratios, seed, scale): """Split data into train/val/test (stratified) and optionally scale.""" test_ratio = split_ratios[2] val_ratio = split_ratios[1] / (split_ratios[0] + split_ratios[1])
X_train_val, X_test, y_train_val, y_test = train_test_split( features, labels, test_size=test_ratio, random_state=seed, stratify=labels, ) X_train, X_val, y_train, y_val = train_test_split( X_train_val, y_train_val, test_size=val_ratio, random_state=seed, stratify=y_train_val, )
scaler = None if scale: scaler = StandardScaler() X_train = scaler.fit_transform(X_train) X_val = scaler.transform(X_val) X_test = scaler.transform(X_test) print("[DATA] Applied StandardScaler (fitted on training split)")
splits = { "X_train": X_train, "y_train": y_train, "X_val": X_val, "y_val": y_val, "X_test": X_test, "y_test": y_test, }
print(f"[DATA] Split (stratified): train={len(y_train)}, val={len(y_val)}, test={len(y_test)}") return splits, scaler
def get_numpy_splits(model_name: str, split_ratios=(0.7, 0.15, 0.15), seed: int = 42, scale: bool = True): """Return raw numpy arrays for non-PyTorch models (e.g. XGBoost).""" features, labels = _load_real_data(model_name) num_features = features.shape[1] num_classes = int(labels.max()) + 1 splits, scaler = _split_and_scale(features, labels, split_ratios, seed, scale) return splits, num_features, num_classes, scaler
def get_dataloaders(model_name: str, batch_size: int = 32, split_ratios=(0.7, 0.15, 0.15), seed: int = 42, scale: bool = True): """Return PyTorch DataLoaders for neural-network models.""" features, labels = _load_real_data(model_name) num_features = features.shape[1] num_classes = int(labels.max()) + 1 splits, scaler = _split_and_scale(features, labels, split_ratios, seed, scale)
train_ds = FeatureVectorDataset(splits["X_train"], splits["y_train"]) val_ds = FeatureVectorDataset(splits["X_val"], splits["y_val"]) test_ds = FeatureVectorDataset(splits["X_test"], splits["y_test"])
train_loader = DataLoader(train_ds, batch_size=batch_size, shuffle=True) val_loader = DataLoader(val_ds, batch_size=batch_size, shuffle=False) test_loader = DataLoader(test_ds, batch_size=batch_size, shuffle=False)
return train_loader, val_loader, test_loader, num_features, num_classes, scaler
12345services: focus-guard: build: . ports: - "7860:7860"1234567891011121314151617181920212223242526272829import js from '@eslint/js'import globals from 'globals'import reactHooks from 'eslint-plugin-react-hooks'import reactRefresh from 'eslint-plugin-react-refresh'import { defineConfig, globalIgnores } from 'eslint/config'
export default defineConfig([ globalIgnores(['dist']), { files: ['**/*.{js,jsx}'], extends: [ js.configs.recommended, reactHooks.configs.flat.recommended, reactRefresh.configs.vite, ], languageOptions: { ecmaVersion: 2020, globals: globals.browser, parserOptions: { ecmaVersion: 'latest', ecmaFeatures: { jsx: true }, sourceType: 'module', }, }, rules: { 'no-unused-vars': ['error', { varsIgnorePattern: '^[A-Z_]' }], }, },])1123345678910111213141516171819202122232425262728293031323334353637383940414243444546# evaluation# evaluation/
Place metrics scripts, run configs, and results here. Logs dir is used by `models.mlp.train` for training logs.Training logs and performance metrics.
## 1. Contents
```logs/βββ face_orientation_training_log.json # MLP (latest run)βββ mlp_face_orientation_training_log.json # MLP (alternate)βββ xgboost_face_orientation_training_log.json # XGBoost```
## 2. Log Format
Each JSON file records the full training history:
**MLP logs:**```json{ "config": { "epochs": 30, "lr": 0.001, "batch_size": 32, ... }, "history": { "train_loss": [0.287, 0.260, ...], "val_loss": [0.256, 0.245, ...], "train_acc": [0.889, 0.901, ...], "val_acc": [0.905, 0.909, ...] }, "test": { "accuracy": 0.929, "f1": 0.929, "roc_auc": 0.971 }}```
**XGBoost logs:**```json{ "config": { "n_estimators": 600, "max_depth": 8, "learning_rate": 0.149, ... }, "train_losses": [0.577, ...], "val_losses": [0.576, ...], "test": { "accuracy": 0.959, "f1": 0.959, "roc_auc": 0.991 }}```
## 3. Generated By
- `python -m models.mlp.train` β writes MLP log- `python -m models.xgboost.train` β writes XGBoost log- Notebooks in `notebooks/` also save logs here1234567891011121314151617<!doctype html><html lang="en">
<head> <meta charset="UTF-8" /> <link rel="icon" type="image/svg+xml" href="/vite.svg" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Focus Guard</title> <link href="https://fonts.googleapis.com/css2?family=Nunito:wght@400;700&display=swap" rel="stylesheet"></head>
<body> <div id="root"></div> <script type="module" src="/src/main.jsx"></script></body>
</html>123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964from fastapi import FastAPI, WebSocket, WebSocketDisconnect, HTTPException, Requestfrom fastapi.staticfiles import StaticFilesfrom fastapi.responses import FileResponsefrom fastapi.middleware.cors import CORSMiddlewarefrom pydantic import BaseModelfrom typing import Optional, List, Anyimport base64import cv2import numpy as npimport aiosqliteimport jsonfrom datetime import datetime, timedeltaimport mathimport osfrom pathlib import Pathfrom typing import Callableimport asyncioimport concurrent.futuresimport threading
from aiortc import RTCPeerConnection, RTCSessionDescription, VideoStreamTrackfrom av import VideoFrame
from mediapipe.tasks.python.vision import FaceLandmarksConnectionsfrom ui.pipeline import FaceMeshPipeline, MLPPipeline, HybridFocusPipeline, XGBoostPipelinefrom models.face_mesh import FaceMeshDetector
# ================ FACE MESH DRAWING (server-side, for WebRTC) ================
_FONT = cv2.FONT_HERSHEY_SIMPLEX_CYAN = (255, 255, 0)_GREEN = (0, 255, 0)_MAGENTA = (255, 0, 255)_ORANGE = (0, 165, 255)_RED = (0, 0, 255)_WHITE = (255, 255, 255)_LIGHT_GREEN = (144, 238, 144)
_TESSELATION_CONNS = [(c.start, c.end) for c in FaceLandmarksConnections.FACE_LANDMARKS_TESSELATION]_CONTOUR_CONNS = [(c.start, c.end) for c in FaceLandmarksConnections.FACE_LANDMARKS_CONTOURS]_LEFT_EYEBROW = [70, 63, 105, 66, 107, 55, 65, 52, 53, 46]_RIGHT_EYEBROW = [300, 293, 334, 296, 336, 285, 295, 282, 283, 276]_NOSE_BRIDGE = [6, 197, 195, 5, 4, 1, 19, 94, 2]_LIPS_OUTER = [61, 146, 91, 181, 84, 17, 314, 405, 321, 375, 291, 409, 270, 269, 267, 0, 37, 39, 40, 185, 61]_LIPS_INNER = [78, 95, 88, 178, 87, 14, 317, 402, 318, 324, 308, 415, 310, 311, 312, 13, 82, 81, 80, 191, 78]_LEFT_EAR_POINTS = [33, 160, 158, 133, 153, 145]_RIGHT_EAR_POINTS = [362, 385, 387, 263, 373, 380]
def _lm_px(lm, idx, w, h): return (int(lm[idx, 0] * w), int(lm[idx, 1] * h))
def _draw_polyline(frame, lm, indices, w, h, color, thickness): for i in range(len(indices) - 1): cv2.line(frame, _lm_px(lm, indices[i], w, h), _lm_px(lm, indices[i + 1], w, h), color, thickness, cv2.LINE_AA)
def _draw_face_mesh(frame, lm, w, h): """Draw tessellation, contours, eyebrows, nose, lips, eyes, irises, gaze lines.""" # Tessellation (gray triangular grid, semi-transparent) overlay = frame.copy() for s, e in _TESSELATION_CONNS: cv2.line(overlay, _lm_px(lm, s, w, h), _lm_px(lm, e, w, h), (200, 200, 200), 1, cv2.LINE_AA) cv2.addWeighted(overlay, 0.3, frame, 0.7, 0, frame) # Contours for s, e in _CONTOUR_CONNS: cv2.line(frame, _lm_px(lm, s, w, h), _lm_px(lm, e, w, h), _CYAN, 1, cv2.LINE_AA) # Eyebrows _draw_polyline(frame, lm, _LEFT_EYEBROW, w, h, _LIGHT_GREEN, 2) _draw_polyline(frame, lm, _RIGHT_EYEBROW, w, h, _LIGHT_GREEN, 2) # Nose _draw_polyline(frame, lm, _NOSE_BRIDGE, w, h, _ORANGE, 1) # Lips _draw_polyline(frame, lm, _LIPS_OUTER, w, h, _MAGENTA, 1) _draw_polyline(frame, lm, _LIPS_INNER, w, h, (200, 0, 200), 1) # Eyes left_pts = np.array([_lm_px(lm, i, w, h) for i in FaceMeshDetector.LEFT_EYE_INDICES], dtype=np.int32) cv2.polylines(frame, [left_pts], True, _GREEN, 2, cv2.LINE_AA) right_pts = np.array([_lm_px(lm, i, w, h) for i in FaceMeshDetector.RIGHT_EYE_INDICES], dtype=np.int32) cv2.polylines(frame, [right_pts], True, _GREEN, 2, cv2.LINE_AA) # EAR key points for indices in [_LEFT_EAR_POINTS, _RIGHT_EAR_POINTS]: for idx in indices: cv2.circle(frame, _lm_px(lm, idx, w, h), 3, (0, 255, 255), -1, cv2.LINE_AA) # Irises + gaze lines for iris_idx, eye_inner, eye_outer in [ (FaceMeshDetector.LEFT_IRIS_INDICES, 133, 33), (FaceMeshDetector.RIGHT_IRIS_INDICES, 362, 263), ]: iris_pts = np.array([_lm_px(lm, i, w, h) for i in iris_idx], dtype=np.int32) center = iris_pts[0] if len(iris_pts) >= 5: radii = [np.linalg.norm(iris_pts[j] - center) for j in range(1, 5)] radius = max(int(np.mean(radii)), 2) cv2.circle(frame, tuple(center), radius, _MAGENTA, 2, cv2.LINE_AA) cv2.circle(frame, tuple(center), 2, _WHITE, -1, cv2.LINE_AA) eye_cx = int((lm[eye_inner, 0] + lm[eye_outer, 0]) / 2.0 * w) eye_cy = int((lm[eye_inner, 1] + lm[eye_outer, 1]) / 2.0 * h) dx, dy = center[0] - eye_cx, center[1] - eye_cy cv2.line(frame, tuple(center), (int(center[0] + dx * 3), int(center[1] + dy * 3)), _RED, 1, cv2.LINE_AA)
def _draw_hud(frame, result, model_name): """Draw status bar and detail overlay matching live_demo.py.""" h, w = frame.shape[:2] is_focused = result["is_focused"] status = "FOCUSED" if is_focused else "NOT FOCUSED" color = _GREEN if is_focused else _RED
# Top bar cv2.rectangle(frame, (0, 0), (w, 55), (0, 0, 0), -1) cv2.putText(frame, status, (10, 28), _FONT, 0.8, color, 2, cv2.LINE_AA) cv2.putText(frame, model_name.upper(), (w - 150, 28), _FONT, 0.45, _WHITE, 1, cv2.LINE_AA)
# Detail line conf = result.get("mlp_prob", result.get("raw_score", 0.0)) mar_s = f" MAR:{result['mar']:.2f}" if result.get("mar") is not None else "" sf = result.get("s_face", 0) se = result.get("s_eye", 0) detail = f"conf:{conf:.2f} S_face:{sf:.2f} S_eye:{se:.2f}{mar_s}" cv2.putText(frame, detail, (10, 48), _FONT, 0.4, _WHITE, 1, cv2.LINE_AA)
# Head pose (top right) if result.get("yaw") is not None: cv2.putText(frame, f"yaw:{result['yaw']:+.0f} pitch:{result['pitch']:+.0f} roll:{result['roll']:+.0f}", (w - 280, 48), _FONT, 0.4, (180, 180, 180), 1, cv2.LINE_AA)
# Yawn indicator if result.get("is_yawning"): cv2.putText(frame, "YAWN", (10, 75), _FONT, 0.7, _ORANGE, 2, cv2.LINE_AA)
# Landmark indices used for face mesh drawing on client (union of all groups).# Sending only these instead of all 478 saves ~60% of the landmarks payload._MESH_INDICES = sorted(set( [10,338,297,332,284,251,389,356,454,323,361,288,397,365,379,378,400,377,152,148,176,149,150,136,172,58,132,93,234,127,162,21,54,103,67,109] # face oval + [33,7,163,144,145,153,154,155,133,173,157,158,159,160,161,246] # left eye + [362,382,381,380,374,373,390,249,263,466,388,387,386,385,384,398] # right eye + [468,469,470,471,472, 473,474,475,476,477] # irises + [70,63,105,66,107,55,65,52,53,46] # left eyebrow + [300,293,334,296,336,285,295,282,283,276] # right eyebrow + [6,197,195,5,4,1,19,94,2] # nose bridge + [61,146,91,181,84,17,314,405,321,375,291,409,270,269,267,0,37,39,40,185] # lips outer + [78,95,88,178,87,14,317,402,318,324,308,415,310,311,312,13,82,81,80,191] # lips inner + [33,160,158,133,153,145] # left EAR key points + [362,385,387,263,373,380] # right EAR key points))# Build a lookup: original_index -> position in sparse array, so client can reconstruct._MESH_INDEX_SET = set(_MESH_INDICES)
# Initialize FastAPI appapp = FastAPI(title="Focus Guard API")
# Add CORS middlewareapp.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"],)
# Global variablesdb_path = "focus_guard.db"pcs = set()_cached_model_name = "mlp" # in-memory cache, updated via /api/settings
async def _wait_for_ice_gathering(pc: RTCPeerConnection): if pc.iceGatheringState == "complete": return done = asyncio.Event()
@pc.on("icegatheringstatechange") def _on_state_change(): if pc.iceGatheringState == "complete": done.set()
await done.wait()
# ================ DATABASE MODELS ================
async def init_database(): """Initialize SQLite database with required tables""" async with aiosqlite.connect(db_path) as db: # FocusSessions table await db.execute(""" CREATE TABLE IF NOT EXISTS focus_sessions ( id INTEGER PRIMARY KEY AUTOINCREMENT, start_time TIMESTAMP NOT NULL, end_time TIMESTAMP, duration_seconds INTEGER DEFAULT 0, focus_score REAL DEFAULT 0.0, total_frames INTEGER DEFAULT 0, focused_frames INTEGER DEFAULT 0, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ) """)
# FocusEvents table await db.execute(""" CREATE TABLE IF NOT EXISTS focus_events ( id INTEGER PRIMARY KEY AUTOINCREMENT, session_id INTEGER NOT NULL, timestamp TIMESTAMP NOT NULL, is_focused BOOLEAN NOT NULL, confidence REAL NOT NULL, detection_data TEXT, FOREIGN KEY (session_id) REFERENCES focus_sessions (id) ) """)
# UserSettings table await db.execute(""" CREATE TABLE IF NOT EXISTS user_settings ( id INTEGER PRIMARY KEY CHECK (id = 1), sensitivity INTEGER DEFAULT 6, notification_enabled BOOLEAN DEFAULT 1, notification_threshold INTEGER DEFAULT 30, frame_rate INTEGER DEFAULT 30, model_name TEXT DEFAULT 'mlp' ) """)
# Insert default settings if not exists await db.execute(""" INSERT OR IGNORE INTO user_settings (id, sensitivity, notification_enabled, notification_threshold, frame_rate, model_name) VALUES (1, 6, 1, 30, 30, 'mlp') """)
await db.commit()
# ================ PYDANTIC MODELS ================
class SessionCreate(BaseModel): pass
class SessionEnd(BaseModel): session_id: int
class SettingsUpdate(BaseModel): sensitivity: Optional[int] = None notification_enabled: Optional[bool] = None notification_threshold: Optional[int] = None frame_rate: Optional[int] = None model_name: Optional[str] = None
class VideoTransformTrack(VideoStreamTrack): def __init__(self, track, session_id: int, get_channel: Callable[[], Any]): super().__init__() self.track = track self.session_id = session_id self.get_channel = get_channel self.last_inference_time = 0 self.min_inference_interval = 1 / 60 self.last_frame = None
async def recv(self): frame = await self.track.recv() img = frame.to_ndarray(format="bgr24") if img is None: return frame
# Normalize size for inference/drawing img = cv2.resize(img, (640, 480))
now = datetime.now().timestamp() do_infer = (now - self.last_inference_time) >= self.min_inference_interval
if do_infer: self.last_inference_time = now model_name = _cached_model_name if model_name not in pipelines or pipelines.get(model_name) is None: model_name = 'mlp' active_pipeline = pipelines.get(model_name)
if active_pipeline is not None: loop = asyncio.get_event_loop() out = await loop.run_in_executor( _inference_executor, _process_frame_safe, active_pipeline, img, model_name, ) is_focused = out["is_focused"] confidence = out.get("mlp_prob", out.get("raw_score", 0.0)) metadata = {"s_face": out.get("s_face", 0.0), "s_eye": out.get("s_eye", 0.0), "mar": out.get("mar", 0.0), "model": model_name}
# Draw face mesh + HUD on the video frame h_f, w_f = img.shape[:2] lm = out.get("landmarks") if lm is not None: _draw_face_mesh(img, lm, w_f, h_f) _draw_hud(img, out, model_name) else: is_focused = False confidence = 0.0 metadata = {"model": model_name} cv2.rectangle(img, (0, 0), (img.shape[1], 55), (0, 0, 0), -1) cv2.putText(img, "NO MODEL", (10, 28), _FONT, 0.8, _RED, 2, cv2.LINE_AA) if self.session_id: await store_focus_event(self.session_id, is_focused, confidence, metadata)
channel = self.get_channel() if channel and channel.readyState == "open": try: channel.send(json.dumps({"type": "detection", "focused": is_focused, "confidence": round(confidence, 3), "detections": detections})) except Exception: pass
self.last_frame = img elif self.last_frame is not None: img = self.last_frame
new_frame = VideoFrame.from_ndarray(img, format="bgr24") new_frame.pts = frame.pts new_frame.time_base = frame.time_base return new_frame
# ================ DATABASE OPERATIONS ================
async def create_session(): async with aiosqlite.connect(db_path) as db: cursor = await db.execute( "INSERT INTO focus_sessions (start_time) VALUES (?)", (datetime.now().isoformat(),) ) await db.commit() return cursor.lastrowid
async def end_session(session_id: int): async with aiosqlite.connect(db_path) as db: cursor = await db.execute( "SELECT start_time, total_frames, focused_frames FROM focus_sessions WHERE id = ?", (session_id,) ) row = await cursor.fetchone()
if not row: return None
start_time_str, total_frames, focused_frames = row start_time = datetime.fromisoformat(start_time_str) end_time = datetime.now() duration = (end_time - start_time).total_seconds() focus_score = focused_frames / total_frames if total_frames > 0 else 0.0
await db.execute(""" UPDATE focus_sessions SET end_time = ?, duration_seconds = ?, focus_score = ? WHERE id = ? """, (end_time.isoformat(), int(duration), focus_score, session_id))
await db.commit()
return { 'session_id': session_id, 'start_time': start_time_str, 'end_time': end_time.isoformat(), 'duration_seconds': int(duration), 'focus_score': round(focus_score, 3), 'total_frames': total_frames, 'focused_frames': focused_frames }
async def store_focus_event(session_id: int, is_focused: bool, confidence: float, metadata: dict): async with aiosqlite.connect(db_path) as db: await db.execute(""" INSERT INTO focus_events (session_id, timestamp, is_focused, confidence, detection_data) VALUES (?, ?, ?, ?, ?) """, (session_id, datetime.now().isoformat(), is_focused, confidence, json.dumps(metadata)))
await db.execute(""" UPDATE focus_sessions SET total_frames = total_frames + 1, focused_frames = focused_frames + ? WHERE id = ? """, (1 if is_focused else 0, session_id)) await db.commit()
class _EventBuffer: """Buffer focus events in memory and flush to DB in batches to avoid per-frame DB writes."""
def __init__(self, flush_interval: float = 2.0): self._buf: list = [] self._lock = asyncio.Lock() self._flush_interval = flush_interval self._task: asyncio.Task | None = None self._total_frames = 0 self._focused_frames = 0
def start(self): if self._task is None: self._task = asyncio.create_task(self._flush_loop())
async def stop(self): if self._task: self._task.cancel() try: await self._task except asyncio.CancelledError: pass self._task = None await self._flush()
def add(self, session_id: int, is_focused: bool, confidence: float, metadata: dict): self._buf.append((session_id, datetime.now().isoformat(), is_focused, confidence, json.dumps(metadata))) self._total_frames += 1 if is_focused: self._focused_frames += 1
async def _flush_loop(self): while True: await asyncio.sleep(self._flush_interval) await self._flush()
async def _flush(self): async with self._lock: if not self._buf: return batch = self._buf[:] total = self._total_frames focused = self._focused_frames self._buf.clear() self._total_frames = 0 self._focused_frames = 0
if not batch: return
session_id = batch[0][0] try: async with aiosqlite.connect(db_path) as db: await db.executemany(""" INSERT INTO focus_events (session_id, timestamp, is_focused, confidence, detection_data) VALUES (?, ?, ?, ?, ?) """, batch) await db.execute(""" UPDATE focus_sessions SET total_frames = total_frames + ?, focused_frames = focused_frames + ? WHERE id = ? """, (total, focused, session_id)) await db.commit() except Exception as e: print(f"[DB] Flush error: {e}")
# ================ STARTUP/SHUTDOWN ================
pipelines = { "geometric": None, "mlp": None, "hybrid": None, "xgboost": None,}
# Thread pool for CPU-bound inference so the event loop stays responsive._inference_executor = concurrent.futures.ThreadPoolExecutor( max_workers=4, thread_name_prefix="inference",)# One lock per pipeline so shared state (TemporalTracker, etc.) is not corrupted when# multiple frames are processed in parallel by the thread pool._pipeline_locks = {name: threading.Lock() for name in ("geometric", "mlp", "hybrid", "xgboost")}
def _process_frame_safe(pipeline, frame, model_name: str): """Run process_frame in executor with per-pipeline lock.""" with _pipeline_locks[model_name]: return pipeline.process_frame(frame)
@app.on_event("startup")async def startup_event(): global pipelines, _cached_model_name print(" Starting Focus Guard API...") await init_database() # Load cached model name from DB async with aiosqlite.connect(db_path) as db: cursor = await db.execute("SELECT model_name FROM user_settings WHERE id = 1") row = await cursor.fetchone() if row: _cached_model_name = row[0] print("[OK] Database initialized")
try: pipelines["geometric"] = FaceMeshPipeline() print("[OK] FaceMeshPipeline (geometric) loaded") except Exception as e: print(f"[WARN] FaceMeshPipeline unavailable: {e}")
try: pipelines["mlp"] = MLPPipeline() print("[OK] MLPPipeline loaded") except Exception as e: print(f"[ERR] Failed to load MLPPipeline: {e}")
try: pipelines["hybrid"] = HybridFocusPipeline() print("[OK] HybridFocusPipeline loaded") except Exception as e: print(f"[WARN] HybridFocusPipeline unavailable: {e}")
try: pipelines["xgboost"] = XGBoostPipeline() print("[OK] XGBoostPipeline loaded") except Exception as e: print(f"[ERR] Failed to load XGBoostPipeline: {e}")
@app.on_event("shutdown")async def shutdown_event(): _inference_executor.shutdown(wait=False) print(" Shutting down Focus Guard API...")
# ================ WEBRTC SIGNALING ================
@app.post("/api/webrtc/offer")async def webrtc_offer(offer: dict): try: print(f"Received WebRTC offer")
pc = RTCPeerConnection() pcs.add(pc)
session_id = await create_session() print(f"Created session: {session_id}")
channel_ref = {"channel": None}
@pc.on("datachannel") def on_datachannel(channel): print(f"Data channel opened") channel_ref["channel"] = channel
@pc.on("track") def on_track(track): print(f"Received track: {track.kind}") if track.kind == "video": local_track = VideoTransformTrack(track, session_id, lambda: channel_ref["channel"]) pc.addTrack(local_track) print(f"Video track added")
@track.on("ended") async def on_ended(): print(f"Track ended")
@pc.on("connectionstatechange") async def on_connectionstatechange(): print(f"Connection state changed: {pc.connectionState}") if pc.connectionState in ("failed", "closed", "disconnected"): try: await end_session(session_id) except Exception as e: print(f"β Error ending session: {e}") pcs.discard(pc) await pc.close()
await pc.setRemoteDescription(RTCSessionDescription(sdp=offer["sdp"], type=offer["type"])) print(f"Remote description set")
answer = await pc.createAnswer() await pc.setLocalDescription(answer) print(f"Answer created")
await _wait_for_ice_gathering(pc) print(f"ICE gathering complete")
return {"sdp": pc.localDescription.sdp, "type": pc.localDescription.type, "session_id": session_id}
except Exception as e: print(f"WebRTC offer error: {e}") import traceback traceback.print_exc() raise HTTPException(status_code=500, detail=f"WebRTC error: {str(e)}")
# ================ WEBSOCKET ================
@app.websocket("/ws/video")async def websocket_endpoint(websocket: WebSocket): await websocket.accept() session_id = None frame_count = 0 running = True event_buffer = _EventBuffer(flush_interval=2.0)
# Latest frame slot β only the most recent frame is kept, older ones are dropped. # Using a dict so nested functions can mutate without nonlocal issues. _slot = {"frame": None} _frame_ready = asyncio.Event()
async def _receive_loop(): """Receive messages as fast as possible. Binary = frame, text = control.""" nonlocal session_id, running try: while running: msg = await websocket.receive() msg_type = msg.get("type", "")
if msg_type == "websocket.disconnect": running = False _frame_ready.set() return
# Binary message β JPEG frame (fast path, no base64) raw_bytes = msg.get("bytes") if raw_bytes is not None and len(raw_bytes) > 0: _slot["frame"] = raw_bytes _frame_ready.set() continue
# Text message β JSON control command (or legacy base64 frame) text = msg.get("text") if not text: continue data = json.loads(text)
if data["type"] == "frame": # Legacy base64 path (fallback) _slot["frame"] = base64.b64decode(data["image"]) _frame_ready.set()
elif data["type"] == "start_session": session_id = await create_session() event_buffer.start() for p in pipelines.values(): if p is not None and hasattr(p, "reset_session"): p.reset_session() await websocket.send_json({"type": "session_started", "session_id": session_id})
elif data["type"] == "end_session": if session_id: await event_buffer.stop() summary = await end_session(session_id) if summary: await websocket.send_json({"type": "session_ended", "summary": summary}) session_id = None except WebSocketDisconnect: running = False _frame_ready.set() except Exception as e: print(f"[WS] receive error: {e}") running = False _frame_ready.set()
async def _process_loop(): """Process only the latest frame, dropping stale ones.""" nonlocal frame_count, running loop = asyncio.get_event_loop() while running: await _frame_ready.wait() _frame_ready.clear() if not running: return
# Grab latest frame and clear slot raw = _slot["frame"] _slot["frame"] = None if raw is None: continue
try: nparr = np.frombuffer(raw, np.uint8) frame = cv2.imdecode(nparr, cv2.IMREAD_COLOR) if frame is None: continue frame = cv2.resize(frame, (640, 480))
model_name = _cached_model_name if model_name not in pipelines or pipelines.get(model_name) is None: model_name = "mlp" active_pipeline = pipelines.get(model_name)
landmarks_list = None if active_pipeline is not None: out = await loop.run_in_executor( _inference_executor, _process_frame_safe, active_pipeline, frame, model_name, ) is_focused = out["is_focused"] confidence = out.get("mlp_prob", out.get("raw_score", 0.0))
lm = out.get("landmarks") if lm is not None: # Send all 478 landmarks as flat array for tessellation drawing landmarks_list = [ [round(float(lm[i, 0]), 3), round(float(lm[i, 1]), 3)] for i in range(lm.shape[0]) ]
if session_id: event_buffer.add(session_id, is_focused, confidence, { "s_face": out.get("s_face", 0.0), "s_eye": out.get("s_eye", 0.0), "mar": out.get("mar", 0.0), "model": model_name, }) else: is_focused = False confidence = 0.0
resp = { "type": "detection", "focused": is_focused, "confidence": round(confidence, 3), "model": model_name, "fc": frame_count, } if active_pipeline is not None: # Send detailed metrics for HUD if out.get("yaw") is not None: resp["yaw"] = round(out["yaw"], 1) resp["pitch"] = round(out["pitch"], 1) resp["roll"] = round(out["roll"], 1) if out.get("mar") is not None: resp["mar"] = round(out["mar"], 3) resp["sf"] = round(out.get("s_face", 0), 3) resp["se"] = round(out.get("s_eye", 0), 3) if landmarks_list is not None: resp["lm"] = landmarks_list await websocket.send_json(resp) frame_count += 1 except Exception as e: print(f"[WS] process error: {e}")
try: await asyncio.gather(_receive_loop(), _process_loop()) except Exception: pass finally: running = False if session_id: await event_buffer.stop() await end_session(session_id)
# ================ API ENDPOINTS ================
@app.post("/api/sessions/start")async def api_start_session(): session_id = await create_session() return {"session_id": session_id}
@app.post("/api/sessions/end")async def api_end_session(data: SessionEnd): summary = await end_session(data.session_id) if not summary: raise HTTPException(status_code=404, detail="Session not found") return summary
@app.get("/api/sessions")async def get_sessions(filter: str = "all", limit: int = 50, offset: int = 0): async with aiosqlite.connect(db_path) as db: db.row_factory = aiosqlite.Row
# NEW: If importing/exporting all, remove limit if special flag or high limit # For simplicity: if limit is -1, return all limit_clause = "LIMIT ? OFFSET ?" params = [] base_query = "SELECT * FROM focus_sessions" where_clause = "" if filter == "today": date_filter = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) where_clause = " WHERE start_time >= ?" params.append(date_filter.isoformat()) elif filter == "week": date_filter = datetime.now() - timedelta(days=7) where_clause = " WHERE start_time >= ?" params.append(date_filter.isoformat()) elif filter == "month": date_filter = datetime.now() - timedelta(days=30) where_clause = " WHERE start_time >= ?" params.append(date_filter.isoformat()) elif filter == "all": # Just ensure we only get completed sessions or all sessions where_clause = " WHERE end_time IS NOT NULL"
query = f"{base_query}{where_clause} ORDER BY start_time DESC" # Handle Limit for Exports if limit == -1: # No limit clause for export pass else: query += f" {limit_clause}" params.extend([limit, offset])
cursor = await db.execute(query, tuple(params)) rows = await cursor.fetchall() return [dict(row) for row in rows]
# --- NEW: Import Endpoint ---@app.post("/api/import")async def import_sessions(sessions: List[dict]): count = 0 try: async with aiosqlite.connect(db_path) as db: for session in sessions: # Use .get() to handle potential missing fields from older versions or edits await db.execute(""" INSERT INTO focus_sessions (start_time, end_time, duration_seconds, focus_score, total_frames, focused_frames, created_at) VALUES (?, ?, ?, ?, ?, ?, ?) """, ( session.get('start_time'), session.get('end_time'), session.get('duration_seconds', 0), session.get('focus_score', 0.0), session.get('total_frames', 0), session.get('focused_frames', 0), session.get('created_at', session.get('start_time')) )) count += 1 await db.commit() return {"status": "success", "count": count} except Exception as e: print(f"Import Error: {e}") return {"status": "error", "message": str(e)}
# --- NEW: Clear History Endpoint ---@app.delete("/api/history")async def clear_history(): try: async with aiosqlite.connect(db_path) as db: # Delete events first (foreign key good practice) await db.execute("DELETE FROM focus_events") await db.execute("DELETE FROM focus_sessions") await db.commit() return {"status": "success", "message": "History cleared"} except Exception as e: return {"status": "error", "message": str(e)}
@app.get("/api/sessions/{session_id}")async def get_session(session_id: int): async with aiosqlite.connect(db_path) as db: db.row_factory = aiosqlite.Row cursor = await db.execute("SELECT * FROM focus_sessions WHERE id = ?", (session_id,)) row = await cursor.fetchone() if not row: raise HTTPException(status_code=404, detail="Session not found") session = dict(row) cursor = await db.execute("SELECT * FROM focus_events WHERE session_id = ? ORDER BY timestamp", (session_id,)) events = [dict(r) for r in await cursor.fetchall()] session['events'] = events return session
@app.get("/api/settings")async def get_settings(): async with aiosqlite.connect(db_path) as db: db.row_factory = aiosqlite.Row cursor = await db.execute("SELECT * FROM user_settings WHERE id = 1") row = await cursor.fetchone() if row: return dict(row) else: return {'sensitivity': 6, 'notification_enabled': True, 'notification_threshold': 30, 'frame_rate': 30, 'model_name': 'mlp'}
@app.put("/api/settings")async def update_settings(settings: SettingsUpdate): async with aiosqlite.connect(db_path) as db: cursor = await db.execute("SELECT id FROM user_settings WHERE id = 1") exists = await cursor.fetchone() if not exists: await db.execute("INSERT INTO user_settings (id, sensitivity) VALUES (1, 6)") await db.commit()
updates = [] params = [] if settings.sensitivity is not None: updates.append("sensitivity = ?") params.append(max(1, min(10, settings.sensitivity))) if settings.notification_enabled is not None: updates.append("notification_enabled = ?") params.append(settings.notification_enabled) if settings.notification_threshold is not None: updates.append("notification_threshold = ?") params.append(max(5, min(300, settings.notification_threshold))) if settings.frame_rate is not None: updates.append("frame_rate = ?") params.append(max(5, min(60, settings.frame_rate))) if settings.model_name is not None and settings.model_name in pipelines and pipelines[settings.model_name] is not None: updates.append("model_name = ?") params.append(settings.model_name) global _cached_model_name _cached_model_name = settings.model_name
if updates: query = f"UPDATE user_settings SET {', '.join(updates)} WHERE id = 1" await db.execute(query, params) await db.commit() return {"status": "success", "updated": len(updates) > 0}
@app.get("/api/stats/summary")async def get_stats_summary(): async with aiosqlite.connect(db_path) as db: cursor = await db.execute("SELECT COUNT(*) FROM focus_sessions WHERE end_time IS NOT NULL") total_sessions = (await cursor.fetchone())[0] cursor = await db.execute("SELECT SUM(duration_seconds) FROM focus_sessions WHERE end_time IS NOT NULL") total_focus_time = (await cursor.fetchone())[0] or 0 cursor = await db.execute("SELECT AVG(focus_score) FROM focus_sessions WHERE end_time IS NOT NULL") avg_focus_score = (await cursor.fetchone())[0] or 0.0 cursor = await db.execute("SELECT DISTINCT DATE(start_time) as session_date FROM focus_sessions WHERE end_time IS NOT NULL ORDER BY session_date DESC") dates = [row[0] for row in await cursor.fetchall()]
streak_days = 0 if dates: current_date = datetime.now().date() for i, date_str in enumerate(dates): session_date = datetime.fromisoformat(date_str).date() expected_date = current_date - timedelta(days=i) if session_date == expected_date: streak_days += 1 else: break return { 'total_sessions': total_sessions, 'total_focus_time': int(total_focus_time), 'avg_focus_score': round(avg_focus_score, 3), 'streak_days': streak_days }
@app.get("/api/models")async def get_available_models(): """Return list of loaded model names and which is currently active.""" available = [name for name, p in pipelines.items() if p is not None] async with aiosqlite.connect(db_path) as db: cursor = await db.execute("SELECT model_name FROM user_settings WHERE id = 1") row = await cursor.fetchone() current = row[0] if row else "mlp" if current not in available and available: current = available[0] return {"available": available, "current": current}
@app.get("/api/mesh-topology")async def get_mesh_topology(): """Return tessellation edge pairs for client-side face mesh drawing (cached by client).""" return {"tessellation": _TESSELATION_CONNS}
@app.get("/health")async def health_check(): available = [name for name, p in pipelines.items() if p is not None] return {"status": "healthy", "models_loaded": available, "database": os.path.exists(db_path)}
# ================ STATIC FILES (SPA SUPPORT) ================
# Resolve static dir from this file so it works regardless of cwd_STATIC_DIR = Path(__file__).resolve().parent / "static"_ASSETS_DIR = _STATIC_DIR / "assets"
# 1. Mount the assets folder (JS/CSS) first so /assets/* is never caught by catch-allif _ASSETS_DIR.is_dir(): app.mount("/assets", StaticFiles(directory=str(_ASSETS_DIR)), name="assets")
# 2. Catch-all for SPA: serve index.html for app routes, never for /assets (would break JS MIME type)@app.get("/{full_path:path}")async def serve_react_app(full_path: str, request: Request): if full_path.startswith("api") or full_path.startswith("ws"): raise HTTPException(status_code=404, detail="Not Found") # Don't serve HTML for asset paths; let them 404 so we don't break module script loading if full_path.startswith("assets") or full_path.startswith("assets/"): raise HTTPException(status_code=404, detail="Not Found")
index_path = _STATIC_DIR / "index.html" if index_path.is_file(): return FileResponse(str(index_path)) return {"message": "React app not found. Please run 'npm run build' and copy dist to static."}1123456783410567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253# models# models/
- **cnn/eye_attention/** β YOLO open/closed eye classifier, crop helper, train stub- **mlp/** β PyTorch MLP on feature vectors (face_orientation / eye_behaviour); checkpoints under `mlp/face_orientation_model/`, `mlp/eye_behaviour_model/`- **geometric/face_orientation/** β head pose (solvePnP). **geometric/eye_behaviour/** β EAR, gaze, MAR- **pretrained/face_mesh/** β MediaPipe face landmarks (no training)- **attention/** β webcam feature collection (17-d), stubs for train/classifier/fusion- **prepare_dataset.py** β loads from `data_preparation/processed/` or synthetic; used by `mlp/train.py`Feature extraction modules and model training scripts.
Run legacy MLP training: `python -m models.mlp.train`. The sklearn MLP used in the live demo is trained in `data_preparation/MLP/train_mlp.ipynb` and saved under `../MLP/models/`.## 1. Feature Extraction
Root-level modules form the real-time inference pipeline:
| Module | Input | Output ||--------|-------|--------|| `face_mesh.py` | BGR frame | 478 MediaPipe landmarks || `head_pose.py` | Landmarks, frame size | yaw, pitch, roll, face/eye score, gaze offset, head deviation || `eye_scorer.py` | Landmarks | EAR (left/right/avg), gaze ratio (h/v), MAR || `eye_crop.py` | Landmarks, frame | Cropped eye region images || `eye_classifier.py` | Eye crops or landmarks | Eye open/closed prediction (geometric fallback) || `collect_features.py` | BGR frame | 17-d feature vector + temporal features (PERCLOS, blink rate, etc.) |
## 2. Training Scripts
| Folder | Model | Command ||--------|-------|---------|| `mlp/` | PyTorch MLP (64β32, 2-class) | `python -m models.mlp.train` || `xgboost/` | XGBoost (600 trees, depth 8) | `python -m models.xgboost.train` |
### mlp/
- `train.py` β training loop with early stopping, ClearML opt-in- `sweep.py` β hyperparameter search (Optuna: lr, batch_size)- `eval_accuracy.py` β load checkpoint and print test metrics- Saves to **`checkpoints/mlp_best.pt`**
### xgboost/
- `train.py` β training with eval-set logging- `sweep.py` / `sweep_local.py` β hyperparameter search (Optuna + ClearML)- `eval_accuracy.py` β load checkpoint and print test metrics- Saves to **`checkpoints/xgboost_face_orientation_best.json`**
## 3. Data Loading
All training scripts import from `data_preparation.prepare_dataset`:
```pythonfrom data_preparation.prepare_dataset import get_numpy_splits # XGBoostfrom data_preparation.prepare_dataset import get_dataloaders # MLP (PyTorch)```
## 4. Results
| Model | Test Accuracy | F1 | ROC-AUC ||-------|--------------|-----|---------|| XGBoost | 95.87% | 0.959 | 0.991 || MLP | 92.92% | 0.929 | 0.971 |123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [], "gpuType": "T4" }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" }, "accelerator": "GPU" }, "cells": [ { "cell_type": "code", "source": [ "import os\n", "import torch\n", "import torch.nn as nn\n", "import torch.optim as optim\n", "from torch.utils.data import DataLoader\n", "from torchvision import datasets, transforms\n", "\n", "from google.colab import drive\n", "drive.mount('/content/drive')\n", "!cp -r /content/drive/MyDrive/Dataset_clean /content/\n", "\n", "#Verify structure\n", "for split in ['train', 'val', 'test']:\n", " path = f'/content/Dataset_clean/{split}'\n", " classes = os.listdir(path)\n", " total = sum(len(os.listdir(os.path.join(path, c))) for c in classes)\n", " print(f'{split}: {total} images | classes: {classes}')" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "sE1F3em-V5go", "outputId": "2c73a9a6-a198-468c-a2cc-253b2de7cc3f" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount(\"/content/drive\", force_remount=True).\n" ] } ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "nG2bh66rQ56G" }, "outputs": [], "source": [ "class EyeCNN(nn.Module):\n", " def __init__(self, num_classes=2):\n", " super(EyeCNN, self).__init__()\n", " self.conv_layers = nn.Sequential(\n", " nn.Conv2d(3, 32, 3, 1, 1),\n", " nn.BatchNorm2d(32),\n", " nn.ReLU(),\n", " nn.MaxPool2d(2, 2),\n", "\n", " nn.Conv2d(32, 64, 3, 1, 1),\n", " nn.BatchNorm2d(64),\n", " nn.ReLU(),\n", " nn.MaxPool2d(2, 2),\n", "\n", " nn.Conv2d(64, 128, 3, 1, 1),\n", " nn.BatchNorm2d(128),\n", " nn.ReLU(),\n", " nn.MaxPool2d(2, 2),\n", "\n", " nn.Conv2d(128, 256, 3, 1, 1),\n", " nn.BatchNorm2d(256),\n", " nn.ReLU(),\n", " nn.MaxPool2d(2, 2)\n", " )\n", "\n", " self.fc_layers = nn.Sequential(\n", " nn.AdaptiveAvgPool2d((1, 1)),\n", " nn.Flatten(),\n", " nn.Linear(256, 512),\n", " nn.ReLU(),\n", " nn.Dropout(0.35),\n", " nn.Linear(512, num_classes)\n", " )\n", "\n", " def forward(self, x):\n", " x = self.conv_layers(x)\n", " x = self.fc_layers(x)\n", " return x" ] } ]}1# GAP Large Project11235 unmodified lines91011131213141517181916171819202115 unmodified lines373839414041424341 unmodified lines8586878990888990919293929394959695969798991001011021031041051069810710810911011110310411211311411510711611711811970 unmodified lines19019119218419319419519650 unmodified lines247248249241250251252253254247255256257# Usage: python -m models.attention.collect_features [--name alice] [--duration 600]
import argparseimport collections5 unmodified linesimport cv2import numpy as np
_PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))_PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))if _PROJECT_ROOT not in sys.path: sys.path.insert(0, _PROJECT_ROOT)
from models.pretrained.face_mesh.face_mesh import FaceMeshDetectorfrom models.geometric.face_orientation.head_pose import HeadPoseEstimatorfrom models.geometric.eye_behaviour.eye_scorer import EyeBehaviourScorer, compute_gaze_ratio, compute_marfrom models.face_mesh import FaceMeshDetectorfrom models.head_pose import HeadPoseEstimatorfrom models.eye_scorer import EyeBehaviourScorer, compute_gaze_ratio, compute_mar
FONT = cv2.FONT_HERSHEY_SIMPLEXGREEN = (0, 255, 0)15 unmodified lines
class TemporalTracker: EAR_BLINK_THRESH = 0.21 MAR_YAWN_THRESH = 0.04 MAR_YAWN_THRESH = 0.55 PERCLOS_WINDOW = 60 BLINK_WINDOW_SEC = 30.0
41 unmodified lines return perclos, blink_rate, closure_dur, yawn_dur
def extract_features(landmarks, w, h, head_pose, eye_scorer, temporal): from models.geometric.eye_behaviour.eye_scorer import _LEFT_EYE_EAR, _RIGHT_EYE_EAR, compute_eardef extract_features(landmarks, w, h, head_pose, eye_scorer, temporal, *, _pre=None): from models.eye_scorer import _LEFT_EYE_EAR, _RIGHT_EYE_EAR, compute_ear
ear_left = compute_ear(landmarks, _LEFT_EYE_EAR) ear_right = compute_ear(landmarks, _RIGHT_EYE_EAR) p = _pre or {}
ear_left = p.get("ear_left", compute_ear(landmarks, _LEFT_EYE_EAR)) ear_right = p.get("ear_right", compute_ear(landmarks, _RIGHT_EYE_EAR)) ear_avg = (ear_left + ear_right) / 2.0 h_gaze, v_gaze = compute_gaze_ratio(landmarks) mar = compute_mar(landmarks)
if "h_gaze" in p and "v_gaze" in p: h_gaze, v_gaze = p["h_gaze"], p["v_gaze"] else: h_gaze, v_gaze = compute_gaze_ratio(landmarks)
mar = p.get("mar", compute_mar(landmarks))
angles = p.get("angles") if angles is None: angles = head_pose.estimate(landmarks, w, h) angles = head_pose.estimate(landmarks, w, h) yaw = angles[0] if angles else 0.0 pitch = angles[1] if angles else 0.0 roll = angles[2] if angles else 0.0
s_face = head_pose.score(landmarks, w, h) s_eye = eye_scorer.score(landmarks) s_face = p.get("s_face", head_pose.score(landmarks, w, h)) s_eye = p.get("s_eye", eye_scorer.score(landmarks))
gaze_offset = math.sqrt((h_gaze - 0.5) ** 2 + (v_gaze - 0.5) ** 2) head_deviation = math.sqrt(yaw ** 2 + pitch ** 2) head_deviation = math.sqrt(yaw ** 2 + pitch ** 2) # cleaned downstream
perclos, blink_rate, closure_dur, yawn_dur = temporal.update(ear_avg, mar)
70 unmodified lines parser.add_argument("--duration", type=int, default=600, help="Max recording time (seconds, default 10 min)") parser.add_argument("--output-dir", type=str, default=os.path.join(_PROJECT_ROOT, "data_preparation", "collected"), default=os.path.join(_PROJECT_ROOT, "data", "collected_data"), help="Where to save .npz files") args = parser.parse_args()
50 unmodified lines landmarks = detector.process(frame) face_ok = landmarks is not None
# record if labeling + face visible if face_ok and label is not None: vec = extract_features(landmarks, w, h, head_pose, eye_scorer, temporal) features_list.append(vec) labels_list.append(label)
# count transitions if prev_label is not None and label != prev_label: transitions += 1 prev_label = label123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869from __future__ import annotations
from abc import ABC, abstractmethod
import numpy as np
class EyeClassifier(ABC): @property @abstractmethod def name(self) -> str: pass
@abstractmethod def predict_score(self, crops_bgr: list[np.ndarray]) -> float: pass
class GeometricOnlyClassifier(EyeClassifier): @property def name(self) -> str: return "geometric"
def predict_score(self, crops_bgr: list[np.ndarray]) -> float: return 1.0
class YOLOv11Classifier(EyeClassifier): def __init__(self, checkpoint_path: str, device: str = "cpu"): from ultralytics import YOLO
self._model = YOLO(checkpoint_path) self._device = device
names = self._model.names self._attentive_idx = None for idx, cls_name in names.items(): if cls_name in ("open", "attentive"): self._attentive_idx = idx break if self._attentive_idx is None: self._attentive_idx = max(names.keys()) print(f"[YOLO] Classes: {names}, attentive_idx={self._attentive_idx}")
@property def name(self) -> str: return "yolo"
def predict_score(self, crops_bgr: list[np.ndarray]) -> float: if not crops_bgr: return 1.0 results = self._model.predict(crops_bgr, device=self._device, verbose=False) scores = [float(r.probs.data[self._attentive_idx]) for r in results] return sum(scores) / len(scores) if scores else 1.0
def load_eye_classifier( path: str | None = None, backend: str = "yolo", device: str = "cpu",) -> EyeClassifier: if path is None or backend == "geometric": return GeometricOnlyClassifier()
try: return YOLOv11Classifier(path, device=device) except ImportError: print("[CLASSIFIER] ultralytics required for YOLO. pip install ultralytics") raise1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677import cv2import numpy as np
from models.face_mesh import FaceMeshDetector
LEFT_EYE_CONTOUR = FaceMeshDetector.LEFT_EYE_INDICESRIGHT_EYE_CONTOUR = FaceMeshDetector.RIGHT_EYE_INDICES
IMAGENET_MEAN = (0.485, 0.456, 0.406)IMAGENET_STD = (0.229, 0.224, 0.225)
CROP_SIZE = 96
def _bbox_from_landmarks( landmarks: np.ndarray, indices: list[int], frame_w: int, frame_h: int, expand: float = 0.4,) -> tuple[int, int, int, int]: pts = landmarks[indices, :2] px = pts[:, 0] * frame_w py = pts[:, 1] * frame_h
x_min, x_max = px.min(), px.max() y_min, y_max = py.min(), py.max() w = x_max - x_min h = y_max - y_min cx = (x_min + x_max) / 2 cy = (y_min + y_max) / 2
size = max(w, h) * (1 + expand) half = size / 2
x1 = int(max(cx - half, 0)) y1 = int(max(cy - half, 0)) x2 = int(min(cx + half, frame_w)) y2 = int(min(cy + half, frame_h))
return x1, y1, x2, y2
def extract_eye_crops( frame: np.ndarray, landmarks: np.ndarray, expand: float = 0.4, crop_size: int = CROP_SIZE,) -> tuple[np.ndarray, np.ndarray, tuple, tuple]: h, w = frame.shape[:2]
left_bbox = _bbox_from_landmarks(landmarks, LEFT_EYE_CONTOUR, w, h, expand) right_bbox = _bbox_from_landmarks(landmarks, RIGHT_EYE_CONTOUR, w, h, expand)
left_crop = frame[left_bbox[1] : left_bbox[3], left_bbox[0] : left_bbox[2]] right_crop = frame[right_bbox[1] : right_bbox[3], right_bbox[0] : right_bbox[2]]
if left_crop.size == 0: left_crop = np.zeros((crop_size, crop_size, 3), dtype=np.uint8) else: left_crop = cv2.resize(left_crop, (crop_size, crop_size), interpolation=cv2.INTER_AREA)
if right_crop.size == 0: right_crop = np.zeros((crop_size, crop_size, 3), dtype=np.uint8) else: right_crop = cv2.resize(right_crop, (crop_size, crop_size), interpolation=cv2.INTER_AREA)
return left_crop, right_crop, left_bbox, right_bbox
def crop_to_tensor(crop_bgr: np.ndarray): import torch
rgb = cv2.cvtColor(crop_bgr, cv2.COLOR_BGR2RGB).astype(np.float32) / 255.0 for c in range(3): rgb[:, :, c] = (rgb[:, :, c] - IMAGENET_MEAN[c]) / IMAGENET_STD[c] return torch.from_numpy(rgb.transpose(2, 0, 1))94 unmodified lines95969798989910038 unmodified lines1391401411431421431441451461471482 unmodified lines15115215315215415515615715815994 unmodified lines
def compute_mar(landmarks: np.ndarray) -> float: # Mouth aspect ratio: high = mouth open (yawning / sleepy) top = landmarks[_MOUTH_TOP, :2] bottom = landmarks[_MOUTH_BOTTOM, :2] left = landmarks[_MOUTH_LEFT, :2]38 unmodified lines return 0.5 * (1.0 + math.cos(math.pi * t))
def score(self, landmarks: np.ndarray) -> float: ear = compute_avg_ear(landmarks) left_ear = compute_ear(landmarks, _LEFT_EYE_EAR) right_ear = compute_ear(landmarks, _RIGHT_EYE_EAR) # Use minimum EAR so closing ONE eye is enough to drop the score ear = min(left_ear, right_ear) ear_s = self._ear_score(ear) if ear_s < 0.3: return ear_s2 unmodified lines return ear_s * gaze_s
def detailed_score(self, landmarks: np.ndarray) -> dict: ear = compute_avg_ear(landmarks) left_ear = compute_ear(landmarks, _LEFT_EYE_EAR) right_ear = compute_ear(landmarks, _RIGHT_EYE_EAR) ear = min(left_ear, right_ear) ear_s = self._ear_score(ear) h_ratio, v_ratio = compute_gaze_ratio(landmarks) gaze_s = self._gaze_score(h_ratio, v_ratio)1234546 unmodified lines52535454555657585960616260616364656667import osimport timefrom pathlib import Pathfrom urllib.request import urlretrieve
46 unmodified lines running_mode=RunningMode.VIDEO, ) self._landmarker = FaceLandmarker.create_from_options(options) self._frame_ts = 0 # ms, for video API self._t0 = time.monotonic() self._last_ts = 0
def process(self, bgr_frame: np.ndarray) -> np.ndarray | None: # BGR in -> (478,3) norm x,y,z or None rgb = cv2.cvtColor(bgr_frame, cv2.COLOR_BGR2RGB) mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=rgb) ts = max(int((time.monotonic() - self._t0) * 1000), self._last_ts + 1) self._frame_ts += 33 # ~30fps result = self._landmarker.detect_for_video(mp_image, self._frame_ts) self._last_ts = ts result = self._landmarker.detect_for_video(mp_image, ts)
if not result.face_landmarks: return None1
24 unmodified lines25262728293031328 unmodified lines414243444546474849509 unmodified lines606162576364656667686924 unmodified lines self._camera_matrix = None self._frame_size = None self._dist_coeffs = np.zeros((4, 1), dtype=np.float64) self._cache_key = None self._cache_result = None
def _get_camera_matrix(self, frame_w: int, frame_h: int) -> np.ndarray: if self._camera_matrix is not None and self._frame_size == (frame_w, frame_h):8 unmodified lines return self._camera_matrix
def _solve(self, landmarks: np.ndarray, frame_w: int, frame_h: int): key = (landmarks.data.tobytes(), frame_w, frame_h) if self._cache_key == key: return self._cache_result
image_points = np.array( [ [landmarks[i, 0] * frame_w, landmarks[i, 1] * frame_h]9 unmodified lines self._dist_coeffs, flags=cv2.SOLVEPNP_ITERATIVE, ) return success, rvec, tvec, image_points result = (success, rvec, tvec, image_points) self._cache_key = key self._cache_result = result return result
def estimate( self, landmarks: np.ndarray, frame_w: int, frame_h: int123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354"""Load saved MLP checkpoint and print test accuracy, F1, AUC."""import osimport sys
import numpy as npimport torchfrom sklearn.metrics import f1_score, roc_auc_score
REPO_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))if REPO_ROOT not in sys.path: sys.path.insert(0, REPO_ROOT)
from data_preparation.prepare_dataset import get_dataloadersfrom models.mlp.train import BaseModel
CKPT_PATH = os.path.join(REPO_ROOT, "checkpoints", "mlp_best.pt")
def main(): if not os.path.isfile(CKPT_PATH): print(f"No checkpoint at {CKPT_PATH}. Train first: python -m models.mlp.train") return
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") train_loader, val_loader, test_loader, num_features, num_classes, _ = get_dataloaders( model_name="face_orientation", batch_size=32, split_ratios=(0.7, 0.15, 0.15), seed=42, )
model = BaseModel(num_features, num_classes).to(device) model.load_state_dict(torch.load(CKPT_PATH, map_location=device, weights_only=True)) model.eval()
criterion = torch.nn.CrossEntropyLoss() test_loss, test_acc, test_probs, test_preds, test_labels = model.test_step( test_loader, criterion, device )
f1 = float(f1_score(test_labels, test_preds, average="weighted")) if num_classes > 2: auc = float(roc_auc_score(test_labels, test_probs, multi_class="ovr", average="weighted")) else: auc = float(roc_auc_score(test_labels, test_probs[:, 1]))
print("MLP (face_orientation) β test set") print(" Accuracy: {:.2%}".format(test_acc)) print(" F1: {:.4f}".format(f1)) print(" ROC-AUC: {:.4f}".format(auc))
if __name__ == "__main__": main()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566"""MLP Hyperparameter Sweep (Optuna).Run: python -m models.mlp.sweep"""import osimport sys
import optunaimport torchimport torch.nn as nnimport torch.optim as optim
REPO_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))if REPO_ROOT not in sys.path: sys.path.insert(0, REPO_ROOT)
from data_preparation.prepare_dataset import get_dataloadersfrom models.mlp.train import BaseModel, set_seed
SEED = 42N_TRIALS = 20EPOCHS_PER_TRIAL = 15
def objective(trial): set_seed(SEED)
lr = trial.suggest_float("lr", 1e-4, 1e-2, log=True) batch_size = trial.suggest_categorical("batch_size", [16, 32, 64])
train_loader, val_loader, _, num_features, num_classes, _ = get_dataloaders( model_name="face_orientation", batch_size=batch_size, split_ratios=(0.7, 0.15, 0.15), seed=SEED, )
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model = BaseModel(num_features, num_classes).to(device) criterion = nn.CrossEntropyLoss() optimizer = optim.Adam(model.parameters(), lr=lr)
best_val_acc = 0.0 for epoch in range(1, EPOCHS_PER_TRIAL + 1): model.training_step(train_loader, optimizer, criterion, device) val_loss, val_acc = model.validation_step(val_loader, criterion, device) if val_acc > best_val_acc: best_val_acc = val_acc
return 1.0 - best_val_acc # minimize (1 - accuracy)
def main(): study = optuna.create_study(direction="minimize", study_name="mlp_sweep") print(f"[SWEEP] MLP Optuna sweep: {N_TRIALS} trials, {EPOCHS_PER_TRIAL} epochs each") study.optimize(objective, n_trials=N_TRIALS)
print("\n[SWEEP] Top-5 trials by validation accuracy") best = sorted(study.trials, key=lambda t: t.value if t.value is not None else float("inf"))[:5] for i, t in enumerate(best, 1): acc = (1.0 - t.value) * 100 print(f" #{i} Val Acc: {acc:.2f}% params={t.params}")
if __name__ == "__main__": main()5 unmodified lines678910101112121314151617181 unmodified line2021222122232425232425262729303132333435363738392829303132333435363738394065 unmodified lines1061071081091101111121131141151 unmodified line1171181191201181211221231241251261271281211291301311323 unmodified lines1361371381311391401411424 unmodified lines14714814914215015115214415315414615515615715815916016116217 unmodified lines1801811821731831841751761771781791801851861871881891901911921937 unmodified lines2012022031942042052062072082092102112122132142152162172182192202212225 unmodified linesimport torchimport torch.nn as nnimport torch.optim as optimfrom sklearn.metrics import f1_score, roc_auc_score
from clearml import Taskfrom data_preparation.prepare_dataset import get_dataloaders
from models.prepare_dataset import get_dataloadersUSE_CLEARML = False
_PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))CFG = { "model_name": "face_orientation", "epochs": 30,1 unmodified line "lr": 1e-3, "seed": 42, "split_ratios": (0.7, 0.15, 0.15), "checkpoints_dir": { "face_orientation": os.path.join(os.path.dirname(__file__), "face_orientation_model"), "eye_behaviour": os.path.join(os.path.dirname(__file__), "eye_behaviour_model"), }, "logs_dir": os.path.join(os.path.dirname(__file__), "..", "..", "evaluation", "logs"), "checkpoints_dir": os.path.join(_PROJECT_ROOT, "checkpoints"), "logs_dir": os.path.join(_PROJECT_ROOT, "evaluation", "logs"),}
# ==== ClearML Initialisation =============================================task = Task.init( project_name="Focus Guard", task_name=f"MLP Model Training", tags=["training", "mlp_model"])
prefix = 'checkpoints/'+task.name+'_'+task.id+'/'os.makedirs(prefix, exist_ok=True)
task.connect(CFG) # ==== ClearML (opt-in) =============================================task = Noneif USE_CLEARML: from clearml import Task task = Task.init( project_name="Focus Guard", task_name="MLP Model Training", tags=["training", "mlp_model"] ) task.connect(CFG)
65 unmodified lines total_loss = 0.0 correct = 0 total = 0 all_preds = [] all_labels = [] all_probs = []
for features, labels in loader: features, labels = features.to(device), labels.to(device)1 unmodified line loss = criterion(outputs, labels)
total_loss += loss.item() * features.size(0) preds = outputs.argmax(dim=1) correct += (outputs.argmax(dim=1) == labels).sum().item() correct += (preds == labels).sum().item() total += features.size(0) probs = torch.softmax(outputs, dim=1) all_preds.extend(preds.cpu().numpy()) all_labels.extend(labels.cpu().numpy()) all_probs.extend(probs.cpu().numpy())
return total_loss / total, correct / total return total_loss / total, correct / total, np.array(all_probs), np.array(all_preds), np.array(all_labels)
def main():3 unmodified lines print(f"[TRAIN] Device: {device}") print(f"[TRAIN] Model: {CFG['model_name']}")
train_loader, val_loader, test_loader, num_features, num_classes = get_dataloaders( train_loader, val_loader, test_loader, num_features, num_classes, scaler = get_dataloaders( model_name=CFG["model_name"], batch_size=CFG["batch_size"], split_ratios=CFG["split_ratios"],4 unmodified lines criterion = nn.CrossEntropyLoss() optimizer = optim.Adam(model.parameters(), lr=CFG["lr"])
print(f"[TRAIN] Parameters: {sum(p.numel() for p in model.parameters()):,}") param_count = sum(p.numel() for p in model.parameters()) print(f"[TRAIN] Parameters: {param_count:,}")
ckpt_dir = CFG["checkpoints_dir"][CFG["model_name"]] ckpt_dir = CFG["checkpoints_dir"] os.makedirs(ckpt_dir, exist_ok=True) best_ckpt_path = os.path.join(ckpt_dir, "best_model.pt") best_ckpt_path = os.path.join(ckpt_dir, "mlp_best.pt")
history = { "model_name": CFG["model_name"], "param_count": param_count, "epochs": [], "train_loss": [], "train_acc": [],17 unmodified lines history["val_acc"].append(round(val_acc, 4))
# Log scalars to ClearML current_lr = optimizer.param_groups[0]['lr'] if task is not None: task.logger.report_scalar("Loss", "Train", float(train_loss), iteration=epoch) task.logger.report_scalar("Accuracy", "Train", float(train_acc), iteration=epoch) task.logger.report_scalar("Loss", "Val", float(val_loss), iteration=epoch) task.logger.report_scalar("Accuracy", "Val", float(val_acc), iteration=epoch) task.logger.report_scalar("Learning Rate", "LR", float(current_lr), iteration=epoch) task.logger.flush() task.logger.report_scalar("Loss", "Train", float(train_loss), iteration=epoch) task.logger.report_scalar("Accuracy", "Train", float(train_acc), iteration=epoch) task.logger.report_scalar("Loss", "Val", float(val_loss), iteration=epoch) task.logger.report_scalar("Accuracy", "Val", float(val_acc), iteration=epoch) task.logger.report_scalar("Learning Rate", "LR", float(current_lr), iteration=epoch) task.logger.flush()
marker = "" if val_acc > best_val_acc:7 unmodified lines print(f"Checkpoint saved to: {best_ckpt_path}")
model.load_state_dict(torch.load(best_ckpt_path, weights_only=True)) test_loss, test_acc = model.test_step(test_loader, criterion, device) test_loss, test_acc, test_probs, test_preds, test_labels = model.test_step(test_loader, criterion, device) test_f1 = f1_score(test_labels, test_preds, average='weighted') # Handle potentially >2 classes for AUC if num_classes > 2: test_auc = roc_auc_score(test_labels, test_probs, multi_class='ovr', average='weighted') else: test_auc = roc_auc_score(test_labels, test_probs[:, 1]) print(f"\n[TEST] Loss: {test_loss:.4f} | Accuracy: {test_acc:.2%}") print(f"[TEST] F1: {test_f1:.4f} | ROC-AUC: {test_auc:.4f}")
history["test_loss"] = round(test_loss, 4) history["test_acc"] = round(test_acc, 4) history["test_f1"] = round(test_f1, 4) history["test_auc"] = round(test_auc, 4)
logs_dir = CFG["logs_dir"] os.makedirs(logs_dir, exist_ok=True)12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091import osimport numpy as npimport torchfrom torch.utils.data import Dataset, DataLoader, random_split
DATA_DIR = os.path.join(os.path.dirname(__file__), "..", "data_preparation", "processed")
FEATURE_FILES = { "face_orientation": { "features": "face_orientation_features.npy", "labels": "face_orientation_labels.npy", }, "eye_behaviour": { "features": "eye_behaviour_features.npy", "labels": "eye_behaviour_labels.npy", },}
SYNTHETIC_CONFIG = { "face_orientation": {"num_samples": 500, "num_features": 12, "num_classes": 2}, "eye_behaviour": {"num_samples": 500, "num_features": 8, "num_classes": 2},}
class FeatureVectorDataset(Dataset): def __init__(self, features: np.ndarray, labels: np.ndarray): self.features = torch.tensor(features, dtype=torch.float32) self.labels = torch.tensor(labels, dtype=torch.long)
def __len__(self): return len(self.labels)
def __getitem__(self, idx): return self.features[idx], self.labels[idx]
def _load_real_data(model_name: str): file_cfg = FEATURE_FILES.get(model_name) if file_cfg is None: return None
feat_path = os.path.join(DATA_DIR, file_cfg["features"]) label_path = os.path.join(DATA_DIR, file_cfg["labels"])
if os.path.exists(feat_path) and os.path.exists(label_path): features = np.load(feat_path) labels = np.load(label_path) print(f"[DATA] Loaded real data for '{model_name}': {features.shape[0]} samples, {features.shape[1]} features") return features, labels
return None
def _generate_synthetic_data(model_name: str): cfg = SYNTHETIC_CONFIG.get(model_name, SYNTHETIC_CONFIG["face_orientation"]) n = cfg["num_samples"] d = cfg["num_features"] c = cfg["num_classes"]
rng = np.random.RandomState(42) features = rng.randn(n, d).astype(np.float32) labels = rng.randint(0, c, size=n).astype(np.int64)
print(f"[DATA] Using synthetic data for '{model_name}': {n} samples, {d} features, {c} classes") return features, labels
def get_dataloaders(model_name: str, batch_size: int = 32, split_ratios=(0.7, 0.15, 0.15), seed: int = 42): data = _load_real_data(model_name) if data is None: data = _generate_synthetic_data(model_name)
features, labels = data num_features = features.shape[1] num_classes = int(labels.max()) + 1
dataset = FeatureVectorDataset(features, labels) total = len(dataset) train_n = int(total * split_ratios[0]) val_n = int(total * split_ratios[1]) test_n = total - train_n - val_n
gen = torch.Generator().manual_seed(seed) train_ds, val_ds, test_ds = random_split(dataset, [train_n, val_n, test_n], generator=gen)
train_loader = DataLoader(train_ds, batch_size=batch_size, shuffle=True) val_loader = DataLoader(val_ds, batch_size=batch_size, shuffle=False) test_loader = DataLoader(test_ds, batch_size=batch_size, shuffle=False)
print(f"[DATA] Split: train={train_n}, val={val_n}, test={test_n}") return train_loader, val_loader, test_loader, num_features, num_classes123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960import pandas as pdimport numpy as npfrom xgboost import XGBClassifierfrom sklearn.metrics import accuracy_scorefrom data_preparation.prepare_dataset import get_numpy_splitsimport os
print("Loading dataset for evaluation...")splits, _, _, _ = get_numpy_splits( model_name="face_orientation", split_ratios=(0.7, 0.15, 0.15), seed=42, scale=False)X_train, y_train = splits["X_train"], splits["y_train"]X_val, y_val = splits["X_val"], splits["y_val"]
csv_path = 'models/xgboost/sweep_results_all_40.csv'df = pd.read_csv(csv_path)
# We will calculate accuracy for each rowaccuracies = []
print(f"Re-evaluating {len(df)} configurations for accuracy. This will take a few minutes...")for idx, row in df.iterrows(): params = { "n_estimators": int(row["n_estimators"]), "max_depth": int(row["max_depth"]), "learning_rate": float(row["learning_rate"]), "subsample": float(row["subsample"]), "colsample_bytree": float(row["colsample_bytree"]), "reg_alpha": float(row["reg_alpha"]), "reg_lambda": float(row["reg_lambda"]), "random_state": 42, "use_label_encoder": False, "verbosity": 0, "eval_metric": "logloss" } # Train the exact same model quickly model = XGBClassifier(**params) model.fit(X_train, y_train) # Get validation predictions and calculate accuracy val_preds = model.predict(X_val) acc = accuracy_score(y_val, val_preds) accuracies.append(round(acc, 4)) if (idx + 1) % 5 == 0: print(f"Processed {idx + 1}/{len(df)} trials...")
# Add accuracy column and save back to CSVdf.insert(2, 'val_accuracy', accuracies)df.to_csv(csv_path, index=False)
print(f"\nDone! Updated {csv_path} with 'val_accuracy'.")# Display the top 5 by accuracy now just to seetop5_acc = df.nlargest(5, 'val_accuracy')[['task_id', 'val_accuracy', 'val_f1', 'val_loss']]print("\nTop 5 Trials by Accuracy:")print(top5_acc.to_string(index=False))123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354"""Load saved XGBoost checkpoint and print test accuracy, F1, AUC."""import osimport sys
import numpy as npfrom sklearn.metrics import f1_score, roc_auc_scorefrom xgboost import XGBClassifier
# run from repo root: python -m models.xgboost.eval_accuracyREPO_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))if REPO_ROOT not in sys.path: sys.path.insert(0, REPO_ROOT)
from data_preparation.prepare_dataset import get_numpy_splits
MODEL_NAME = "face_orientation"CKPT_DIR = os.path.join(REPO_ROOT, "checkpoints")MODEL_PATH = os.path.join(CKPT_DIR, f"xgboost_{MODEL_NAME}_best.json")
def main(): if not os.path.isfile(MODEL_PATH): print(f"No checkpoint at {MODEL_PATH}. Train first: python -m models.xgboost.train") return
splits, num_features, num_classes, _ = get_numpy_splits( model_name=MODEL_NAME, split_ratios=(0.7, 0.15, 0.15), seed=42, scale=False, ) X_test = splits["X_test"] y_test = splits["y_test"]
model = XGBClassifier() model.load_model(MODEL_PATH)
preds = model.predict(X_test) probs = model.predict_proba(X_test) acc = float(np.mean(preds == y_test)) f1 = float(f1_score(y_test, preds, average="weighted")) if num_classes > 2: auc = float(roc_auc_score(y_test, probs, multi_class="ovr", average="weighted")) else: auc = float(roc_auc_score(y_test, probs[:, 1]))
print("XGBoost (face_orientation) β test set") print(" Accuracy: {:.2%}".format(acc)) print(" F1: {:.4f}".format(f1)) print(" ROC-AUC: {:.4f}".format(auc))
if __name__ == "__main__": main()12345678910111213141516171819202122232425262728293031323334353637383940414243444546import csvfrom clearml import Task
print("Fetching tasks from ClearML...")tasks = Task.get_tasks( project_name='FocusGuards Large Group Project', tags=['optuna_manual'], task_filter={'status': ['completed', 'failed']})
results = []for t in tasks: if t.get_status() != 'completed': continue params = t.get_parameters() # We reported logloss as Loss/Val, and F1 as Summary/val_f1 metrics = t.get_last_scalar_metrics() val_loss = metrics.get('Loss', {}).get('Val', {}).get('last', float('inf')) val_f1 = metrics.get('Summary', {}).get('val_f1', {}).get('last', 0.0) val_acc = metrics.get('Summary', {}).get('val_accuracy', {}).get('last', 0.0) row = { 'task_id': t.id, 'val_loss': round(val_loss, 4) if val_loss != float('inf') else val_loss, 'val_f1': round(val_f1, 4), 'val_acc': round(val_acc, 4), } # Default Optuna parameter names parsed back from ClearML storage format for k in ['n_estimators', 'max_depth', 'learning_rate', 'subsample', 'colsample_bytree', 'reg_alpha', 'reg_lambda']: val = params.get(f"General/{k}") or params.get(k) row[k] = val results.append(row)
# Sort by lowest validation lossresults.sort(key=lambda x: x['val_loss'])
filepath = 'models/xgboost/sweep_results_all_40.csv'with open(filepath, 'w', newline='') as f: fieldnames = ['task_id', 'val_loss', 'val_f1', 'val_acc', 'n_estimators', 'max_depth', 'learning_rate', 'subsample', 'colsample_bytree', 'reg_alpha', 'reg_lambda'] writer = csv.DictWriter(f, fieldnames=fieldnames, extrasaction='ignore') writer.writeheader() writer.writerows(results)
print(f"Successfully grabbed {len(results)} trials and saved to {filepath}")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135"""XGBoost Hyperparameter Sweep β ClearML HPO Controller======================================================Run this script once to launch the full sweep. It clones the baseXGBoost training Task (identified by BASE_TASK_ID) and dispatches oneclone per trial, each with a different hyperparameter configuration.
Prerequisites-------------1. Run train.py at least once so a completed Task exists in ClearML. Copy its Task-ID into BASE_TASK_ID below.2. Have at least one `clearml-agent` listening on the "default" queue, OR set EXECUTE_LOCALLY = True to run trials in-process (slower but needs no agent).
Usage----- python models/xgboost/sweep.py"""
import timefrom clearml import Taskfrom clearml.automation import ( HyperParameterOptimizer, UniformParameterRange, DiscreteParameterRange,)
try: from clearml.automation.optuna import OptimizerOptuna # preferred OPTIMIZER_CLASS = OptimizerOptunaexcept ImportError: from clearml.automation import OptimizerBOHB # fallback OPTIMIZER_CLASS = OptimizerBOHB
# ββ Configuration βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Paste the Task-ID of a successfully completed XGBoost training run here.# Find it in the ClearML UI: Projects β Focus Guard β XGBoost Model Training# β right-click the task β Copy ID.BASE_TASK_ID = "0f42afbb3396400babc7a1a0728e7326"
# Set True to run trials in the same process (no agent needed, but serial).# Set False to dispatch to clearml-agents on the "default" queue (parallel).EXECUTE_LOCALLY = True
# Total budget and concurrencyMAX_TRIALS = 40 # total number of hyperparameter configurations to tryMAX_CONCURRENT = 4 # how many trials to run in parallel (match agent count)
# ββ Search space ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# These names must match the keys in CFG inside train.py exactly, because# task.connect(CFG) registers them under those names.
SEARCH_SPACE = [ DiscreteParameterRange("General/n_estimators", values=[100, 200, 400, 600]), DiscreteParameterRange("General/max_depth", values=[3, 4, 5, 6, 8]), UniformParameterRange( "General/learning_rate", min_value=0.01, max_value=0.30), UniformParameterRange( "General/subsample", min_value=0.50, max_value=1.00), UniformParameterRange( "General/colsample_bytree",min_value=0.50, max_value=1.00), UniformParameterRange( "General/reg_alpha", min_value=0.00, max_value=2.00), UniformParameterRange( "General/reg_lambda", min_value=0.50, max_value=5.00),]
# Switch back to the per-epoch validation logloss scalar curve since Optuna # integration struggles to use single-value metrics natively.OBJECTIVE_METRIC_TITLE = "Loss"OBJECTIVE_METRIC_SERIES = "Val"OBJECTIVE_SIGN = "min" # minimize logloss
# ββ Main ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def main(): # Register this controller as its own ClearML Task so it is tracked too. controller_task = Task.init( project_name="FocusGuards Large Group Project", task_name="XGBoost HPO Sweep Controller", task_type=Task.TaskTypes.optimizer, tags=["sweep", "xgboost", "hpo"], )
optimizer = HyperParameterOptimizer( base_task_id=BASE_TASK_ID, hyper_parameters=SEARCH_SPACE, objective_metric_title=OBJECTIVE_METRIC_TITLE, objective_metric_series=OBJECTIVE_METRIC_SERIES, objective_metric_sign=OBJECTIVE_SIGN, optimizer_class=OPTIMIZER_CLASS, # Execution execution_queue="default", max_number_of_concurrent_tasks=MAX_CONCURRENT, total_max_jobs=MAX_TRIALS, # Early stop a trial if validation loss hasn't improved in 10 rounds # (relies on the per-round "Loss/Val" scalars logged in train.py) min_iteration_per_job=10, max_iteration_per_job=600, )
if EXECUTE_LOCALLY: optimizer.start_locally(job_complete_callback=_on_trial_done) else: optimizer.start(job_complete_callback=_on_trial_done)
print("[SWEEP] Optimizer started. Waiting for trials to complete β¦") print(f"[SWEEP] Budget: {MAX_TRIALS} trials, {MAX_CONCURRENT} concurrent")
# Poll until the budget is exhausted optimizer.wait() optimizer.stop()
# ββ Print best result βββββββββββββββββββββββββββββββββββββββββββββββββββββ top_k = optimizer.get_top_experiments(top_k=5) print("\n[SWEEP] ββ Top-5 trials by Validation Loss ββββββββββββββββββββββββββββββ") for rank, task in enumerate(top_k, 1): params = task.get_parameters() cfg = {k.split("/")[-1]: v for k, v in params.items() if k.startswith("General/")} metrics = task.get_last_scalar_metrics() val_loss = metrics.get(OBJECTIVE_METRIC_TITLE, {}).get(OBJECTIVE_METRIC_SERIES, {}).get("last", float('inf')) val_acc = metrics.get("Summary", {}).get("val_accuracy", {}).get("last", 0.0) val_f1 = metrics.get("Summary", {}).get("val_f1", {}).get("last", 0.0) print(f" #{rank} Val_Loss={val_loss:.4f} Val_Acc={val_acc:.2%} Val_F1={val_f1:.4f} task_id={task.id}") print(f" {cfg}")
controller_task.close()
def _on_trial_done(job_id: str, objective: float): """Callback fired each time a trial finishes.""" print(f"[SWEEP] Trial {job_id} finished β {OBJECTIVE_METRIC_TITLE}={objective:.4f}")
if __name__ == "__main__": main().gitignore+29-8
DockerfileAdded+27
README.md+87-6
app.pyAdded+1
checkpoints/hybrid_focus_config.jsonAdded+10
MLP/models/meta_20260224_024200.npz β checkpoints/meta_best.npzRenamed+2-2
MLP/models/mlp_20260224_024200.joblib β checkpoints/mlp_best.ptRenamed+2-2
MLP/models/scaler_20260224_024200.joblib β checkpoints/scaler_best.joblibRenamed+2-2
checkpoints/xgboost_face_orientation_best.jsonAdded
data_preparation/CNN/eye_crops/val/open/.gitkeepDeleted-1
data_preparation/MLP/explore_collected_data.ipynbDeleted
data_preparation/MLP/train_mlp.ipynbDeleted
data_preparation/README.md+61-27
models/geometric/__init__.py β data_preparation/__init__.pyRenamed
data_preparation/data_exploration.ipynbAdded
data_preparation/prepare_dataset.pyAdded+232
docker-compose.ymlAdded+5
eslint.config.jsAdded+29
evaluation/README.md+45-2
index.htmlAdded+17
main.pyAdded+964
models/README.md+51-8
models/attention/__init__.py β models/__init__.pyRenamed
models/attention/classifier.pyDeleted
models/attention/fusion.pyDeleted
models/attention/train.pyDeleted
models/cnn/notebooks/EyeCNN.ipynbAdded+107
models/cnn/notebooks/EyeCNN_Train_Evaluate_new.ipynbAdded
models/cnn/notebooks/EyeCNN_Training_Evaluate.ipynbAdded
models/cnn/notebooks/README.mdAdded+1
models/attention/collect_features.py β models/collect_features.pyRenamed+26-19
models/eye_classifier.pyAdded+69
models/eye_crop.pyAdded+77
models/geometric/eye_behaviour/eye_scorer.py β models/eye_scorer.pyRenamed+7-3
models/pretrained/face_mesh/face_mesh.py β models/face_mesh.pyRenamed+6-3
models/geometric/eye_behaviour/__init__.pyDeleted
models/geometric/face_orientation/__init__.pyDeleted-1
models/geometric/face_orientation/head_pose.py β models/head_pose.pyRenamed+10-1
models/mlp/eval_accuracy.pyAdded+54
models/mlp/sweep.pyAdded+66
models/mlp/train.py+53-32
models/prepare_dataset.pyDeleted-91
models/pretrained/__init__.pyDeleted
models/pretrained/face_mesh/.gitkeepDeleted
models/pretrained/face_mesh/__init__.pyDeleted
models/xgboost/add_accuracy.pyAdded+60
models/xgboost/checkpoints/face_orientation_best.jsonAdded
models/xgboost/eval_accuracy.pyAdded+54
models/xgboost/fetch_sweep_results.pyAdded+46
models/xgboost/sweep.pyAdded+135