Spaces:
Sleeping
Sleeping
k22056537 commited on
Commit ·
2fb4cf4
1
Parent(s): f2375d3
reorg folders, add MLP demo
Browse filesFiles changed (36)
- .gitignore-16
- MLP/models/meta_20260224_024200.npzAdded+3
- MLP/models/mlp_20260224_024200.joblibAdded+3
- MLP/models/scaler_20260224_024200.joblibAdded+3
- README.md+7-7
- data_preparation/CNN/eye_crops/val/open/.gitkeepAdded+1
- data_preparation/MLP/explore_collected_data.ipynbAdded
- data_preparation/MLP/train_mlp.ipynbAdded
- data_preparation/README.md+40-2
- evaluation/README.md+1-1
- models/README.md+7-5
- models/attention/__init__.pyAdded+1
- models/attention/classifier.pyAdded
- models/attention/collect_features.pyAdded+349
- models/attention/fusion.pyAdded
- models/attention/train.pyAdded
- models/cnn/__init__.pyAdded
- models/cnn/eye_attention/__init__.pyAdded+1
- models/cnn/eye_attention/classifier.pyAdded+69
- models/cnn/eye_attention/crop.pyAdded+70
- models/cnn/eye_attention/train.pyAdded
- models/geometric/__init__.pyAdded
- models/geometric/eye_behaviour/__init__.pyAdded
- models/geometric/eye_behaviour/eye_scorer.pyAdded+164
- models/geometric/face_orientation/__init__.pyAdded+1
- models/geometric/face_orientation/head_pose.pyAdded+112
- models/mlp/__init__.pyAdded
- models/mlp/train.pyAdded+184
- models/pretrained/__init__.pyAdded
- models/pretrained/face_mesh/.gitkeepAdded
- models/pretrained/face_mesh/__init__.pyAdded
- models/pretrained/face_mesh/face_mesh.pyAdded+91
- requirements.txt+2-2
- ui/README.md+10-12
- ui/live_demo.py+47-37
- ui/pipeline.py+73-15
11237 unmodified lines11121315161415161721222324252627282930311833341920# Python__pycache__/*.py[cod]*$py.class7 unmodified lines.eggs/dist/build/
# IDE.idea/.vscode/*.swp*.swo
# Data and outputs (optional: uncomment if you don’t want to track large files)# data_preparation/raw/# data_preparation/processed/*.npy# evaluation/logs/# evaluation/results/
# Model checkpoints (uncomment to ignore .pt files)# *.pt
# Projectdocs/
# OS.DS_StoreThumbs.db123version https://git-lfs.github.com/spec/v1oid sha256:769bb62c7bf04aafd808e9b2623e795c2d92bcb933313ebf553d6fce5ebe7143size 1616123version https://git-lfs.github.com/spec/v1oid sha256:a72933fcf2d0aed998c6303ea4298c04618d937c7f17bf492e76efcf3b4b54d7size 50484123version https://git-lfs.github.com/spec/v1oid sha256:3f9ef3721cee28f1472886556e001d0f6ed0abe09011d979a70ca9bf447d453esize 8231123345567897891010# GAP — FocusGuard# FocusGuard
Real-time focus estimation from webcam (head pose + eye behaviour). Webcam-based focus detection: face mesh, head pose, eye (geometry or YOLO), plus an MLP trained on collected features.
## Layout- **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)
- **data_preparation/** — Dataset team (raw data, processed, scripts)- **models/** — Face orientation, eye behaviour, fusion, landmarks. Training entry: `models/train.py`- **evaluation/** — Metrics, runs, results- **ui/** — Live demo + session viewRun from here: `pip install -r requirements.txt` then `python ui/live_demo.py` or `python ui/live_demo.py --mlp`.1
112334567891011121314151617181920212223242526272829303132333435363738394041# data_preparation# Data Preparation
Dataset team owns layout and scripts here.## Folder Structure
### collected/Contains raw session files in `.npz` format. Generated using:
python -m models.attention.collect_features
Each session includes:- 17-dimensional feature vectors- Corresponding labels
---
### MLP/Contains notebooks for:- Exploring collected data- Training the sklearn MLP model (10 features)
Trained models are saved to:../MLP/models/
---
### CNN/Eye crop directory structure for CNN training (YOLO).
---
## Collecting Data
**Step-by-step**
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).1233# evaluation
Metrics, experiment configs, and results live here.Place metrics scripts, run configs, and results here. Logs dir is used by `models.mlp.train` for training logs.1234563456789810# models
- `face_orientation_model/` — S_face- `eye_behaviour_model/` — S_eye- `attention_score_fusion/` — fusion + smoothing- `face_landmarks_pretrained/` — MediaPipe FaceMesh (no training)- **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`
`train.py` trains the MLP on feature vectors; `prepare_dataset.py` loads from `data_preparation/processed/` or synthetic.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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349# Usage: python -m models.attention.collect_features [--name alice] [--duration 600]
import argparseimport collectionsimport mathimport osimport sysimport time
import cv2import numpy as np
_PROJECT_ROOT = os.path.dirname(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_mar
FONT = cv2.FONT_HERSHEY_SIMPLEXGREEN = (0, 255, 0)RED = (0, 0, 255)WHITE = (255, 255, 255)YELLOW = (0, 255, 255)ORANGE = (0, 165, 255)GRAY = (120, 120, 120)
FEATURE_NAMES = [ "ear_left", "ear_right", "ear_avg", "h_gaze", "v_gaze", "mar", "yaw", "pitch", "roll", "s_face", "s_eye", "gaze_offset", "head_deviation", "perclos", "blink_rate", "closure_duration", "yawn_duration",]
NUM_FEATURES = len(FEATURE_NAMES)assert NUM_FEATURES == 17
class TemporalTracker: EAR_BLINK_THRESH = 0.21 MAR_YAWN_THRESH = 0.04 PERCLOS_WINDOW = 60 BLINK_WINDOW_SEC = 30.0
def __init__(self): self.ear_history = collections.deque(maxlen=self.PERCLOS_WINDOW) self.blink_timestamps = collections.deque() self._eyes_closed = False self._closure_start = None self._yawn_start = None
def update(self, ear_avg, mar, now=None): if now is None: now = time.time()
closed = ear_avg < self.EAR_BLINK_THRESH self.ear_history.append(1.0 if closed else 0.0) perclos = sum(self.ear_history) / len(self.ear_history) if self.ear_history else 0.0
if self._eyes_closed and not closed: self.blink_timestamps.append(now) self._eyes_closed = closed
cutoff = now - self.BLINK_WINDOW_SEC while self.blink_timestamps and self.blink_timestamps[0] < cutoff: self.blink_timestamps.popleft() blink_rate = len(self.blink_timestamps) * (60.0 / self.BLINK_WINDOW_SEC)
if closed: if self._closure_start is None: self._closure_start = now closure_dur = now - self._closure_start else: self._closure_start = None closure_dur = 0.0
yawning = mar > self.MAR_YAWN_THRESH if yawning: if self._yawn_start is None: self._yawn_start = now yawn_dur = now - self._yawn_start else: self._yawn_start = None yawn_dur = 0.0
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_ear
ear_left = compute_ear(landmarks, _LEFT_EYE_EAR) 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)
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)
gaze_offset = math.sqrt((h_gaze - 0.5) ** 2 + (v_gaze - 0.5) ** 2) head_deviation = math.sqrt(yaw ** 2 + pitch ** 2)
perclos, blink_rate, closure_dur, yawn_dur = temporal.update(ear_avg, mar)
return np.array([ ear_left, ear_right, ear_avg, h_gaze, v_gaze, mar, yaw, pitch, roll, s_face, s_eye, gaze_offset, head_deviation, perclos, blink_rate, closure_dur, yawn_dur, ], dtype=np.float32)
def quality_report(labels): n = len(labels) n1 = int((labels == 1).sum()) n0 = n - n1 transitions = int(np.sum(np.diff(labels) != 0)) duration_sec = n / 30.0 # approximate at 30fps
warnings = []
print(f"\n{'='*50}") print(f" DATA QUALITY REPORT") print(f"{'='*50}") print(f" Total samples : {n}") print(f" Focused : {n1} ({n1/max(n,1)*100:.1f}%)") print(f" Unfocused : {n0} ({n0/max(n,1)*100:.1f}%)") print(f" Duration : {duration_sec:.0f}s ({duration_sec/60:.1f} min)") print(f" Transitions : {transitions}") if transitions > 0: print(f" Avg segment : {n/transitions:.0f} frames ({n/transitions/30:.1f}s)")
# checks if duration_sec < 120: warnings.append(f"TOO SHORT: {duration_sec:.0f}s — aim for 5-10 minutes (300-600s)")
if n < 3000: warnings.append(f"LOW SAMPLE COUNT: {n} frames — aim for 9000+ (5 min at 30fps)")
balance = n1 / max(n, 1) if balance < 0.3 or balance > 0.7: warnings.append(f"IMBALANCED: {balance:.0%} focused — aim for 35-65% focused")
if transitions < 10: warnings.append(f"TOO FEW TRANSITIONS: {transitions} — switch every 10-30s, aim for 20+")
if transitions == 1: warnings.append("SINGLE BLOCK: you recorded one unfocused + one focused block — " "model will learn temporal position, not focus patterns")
if warnings: print(f"\n ⚠️ WARNINGS ({len(warnings)}):") for w in warnings: print(f" • {w}") print(f"\n Consider re-recording this session.") else: print(f"\n ✅ All checks passed!")
print(f"{'='*50}\n") return len(warnings) == 0
# ---------------------------------------------------------------------------# Maindef main(): parser = argparse.ArgumentParser() parser.add_argument("--name", type=str, default="session", help="Your name or session ID") parser.add_argument("--camera", type=int, default=0, help="Camera index") 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"), help="Where to save .npz files") args = parser.parse_args()
os.makedirs(args.output_dir, exist_ok=True)
detector = FaceMeshDetector() head_pose = HeadPoseEstimator() eye_scorer = EyeBehaviourScorer() temporal = TemporalTracker()
cap = cv2.VideoCapture(args.camera) if not cap.isOpened(): print("[COLLECT] ERROR: can't open camera") return
print("[COLLECT] Data Collection Tool") print(f"[COLLECT] Session: {args.name}, max {args.duration}s") print(f"[COLLECT] Features per frame: {NUM_FEATURES}") print("[COLLECT] Controls:") print(" 1 = FOCUSED (looking at screen normally)") print(" 0 = NOT FOCUSED (phone, away, eyes closed, yawning)") print(" p = pause") print(" q = save & quit") print() print("[COLLECT] TIPS for good data:") print(" • Switch between 1 and 0 every 10-30 seconds") print(" • Aim for 20+ transitions total") print(" • Act out varied scenarios: reading, phone, talking, drowsy") print(" • Record at least 5 minutes") print()
features_list = [] labels_list = [] label = None # None = paused transitions = 0 # count label switches prev_label = None status = "PAUSED -- press 1 (focused) or 0 (not focused)" t_start = time.time() prev_time = time.time() fps = 0.0
try: while True: elapsed = time.time() - t_start if elapsed > args.duration: print(f"[COLLECT] Time limit ({args.duration}s)") break
ret, frame = cap.read() if not ret: break
h, w = frame.shape[:2] 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 = label
now = time.time() fps = 0.9 * fps + 0.1 * (1.0 / max(now - prev_time, 1e-6)) prev_time = now
# --- draw UI --- n = len(labels_list) n1 = sum(1 for x in labels_list if x == 1) n0 = n - n1 remaining = max(0, args.duration - elapsed)
bar_color = GREEN if label == 1 else (RED if label == 0 else (80, 80, 80)) cv2.rectangle(frame, (0, 0), (w, 70), (0, 0, 0), -1) cv2.putText(frame, status, (10, 22), FONT, 0.55, bar_color, 2, cv2.LINE_AA) cv2.putText(frame, f"Samples: {n} (F:{n1} U:{n0}) Switches: {transitions}", (10, 48), FONT, 0.42, WHITE, 1, cv2.LINE_AA) cv2.putText(frame, f"FPS:{fps:.0f}", (w - 80, 22), FONT, 0.45, WHITE, 1, cv2.LINE_AA) cv2.putText(frame, f"{int(remaining)}s left", (w - 80, 48), FONT, 0.42, YELLOW, 1, cv2.LINE_AA)
if n > 0: bar_w = min(w - 20, 300) bar_x = w - bar_w - 10 bar_y = 58 frac = n1 / n cv2.rectangle(frame, (bar_x, bar_y), (bar_x + bar_w, bar_y + 8), (40, 40, 40), -1) cv2.rectangle(frame, (bar_x, bar_y), (bar_x + int(bar_w * frac), bar_y + 8), GREEN, -1) cv2.putText(frame, f"{frac:.0%}F", (bar_x + bar_w + 4, bar_y + 8), FONT, 0.3, GRAY, 1, cv2.LINE_AA)
if not face_ok: cv2.putText(frame, "NO FACE", (w // 2 - 60, h // 2), FONT, 0.7, RED, 2, cv2.LINE_AA)
# red dot = recording if label is not None and face_ok: cv2.circle(frame, (w - 20, 80), 8, RED, -1)
# live warnings warn_y = h - 35 if n > 100 and transitions < 3: cv2.putText(frame, "! Switch more often (aim for 20+ transitions)", (10, warn_y), FONT, 0.38, ORANGE, 1, cv2.LINE_AA) warn_y -= 18 if elapsed > 30 and n > 0: bal = n1 / n if bal < 0.25 or bal > 0.75: cv2.putText(frame, f"! Imbalanced ({bal:.0%} focused) - record more of the other", (10, warn_y), FONT, 0.38, ORANGE, 1, cv2.LINE_AA) warn_y -= 18
cv2.putText(frame, "1:focused 0:unfocused p:pause q:save+quit", (10, h - 10), FONT, 0.38, GRAY, 1, cv2.LINE_AA)
cv2.imshow("FocusGuard -- Data Collection", frame)
key = cv2.waitKey(1) & 0xFF if key == ord("1"): label = 1 status = "Recording: FOCUSED" print(f"[COLLECT] -> FOCUSED (n={n}, transitions={transitions})") elif key == ord("0"): label = 0 status = "Recording: NOT FOCUSED" print(f"[COLLECT] -> NOT FOCUSED (n={n}, transitions={transitions})") elif key == ord("p"): label = None status = "PAUSED" print(f"[COLLECT] paused (n={n})") elif key == ord("q"): break
finally: cap.release() cv2.destroyAllWindows() detector.close()
if len(features_list) > 0: feats = np.stack(features_list) labs = np.array(labels_list, dtype=np.int64)
ts = time.strftime("%Y%m%d_%H%M%S") fname = f"{args.name}_{ts}.npz" fpath = os.path.join(args.output_dir, fname) np.savez(fpath, features=feats, labels=labs, feature_names=np.array(FEATURE_NAMES))
print(f"\n[COLLECT] Saved {len(labs)} samples -> {fpath}") print(f" Shape: {feats.shape} ({NUM_FEATURES} features)")
quality_report(labs) else: print("\n[COLLECT] No data collected")
print("[COLLECT] Done")
if __name__ == "__main__": main()1
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869from __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") raise12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970import cv2import numpy as np
from models.pretrained.face_mesh.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]]
left_crop = cv2.resize(left_crop, (crop_size, crop_size), interpolation=cv2.INTER_AREA) 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))123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164import math
import numpy as np
_LEFT_EYE_EAR = [33, 160, 158, 133, 153, 145]_RIGHT_EYE_EAR = [362, 385, 387, 263, 373, 380]
_LEFT_IRIS_CENTER = 468_RIGHT_IRIS_CENTER = 473
_LEFT_EYE_INNER = 133_LEFT_EYE_OUTER = 33_RIGHT_EYE_INNER = 362_RIGHT_EYE_OUTER = 263
_LEFT_EYE_TOP = 159_LEFT_EYE_BOTTOM = 145_RIGHT_EYE_TOP = 386_RIGHT_EYE_BOTTOM = 374
_MOUTH_TOP = 13_MOUTH_BOTTOM = 14_MOUTH_LEFT = 78_MOUTH_RIGHT = 308_MOUTH_UPPER_1 = 82_MOUTH_UPPER_2 = 312_MOUTH_LOWER_1 = 87_MOUTH_LOWER_2 = 317
MAR_YAWN_THRESHOLD = 0.55
def _distance(p1: np.ndarray, p2: np.ndarray) -> float: return float(np.linalg.norm(p1 - p2))
def compute_ear(landmarks: np.ndarray, eye_indices: list[int]) -> float: p1 = landmarks[eye_indices[0], :2] p2 = landmarks[eye_indices[1], :2] p3 = landmarks[eye_indices[2], :2] p4 = landmarks[eye_indices[3], :2] p5 = landmarks[eye_indices[4], :2] p6 = landmarks[eye_indices[5], :2]
vertical1 = _distance(p2, p6) vertical2 = _distance(p3, p5) horizontal = _distance(p1, p4)
if horizontal < 1e-6: return 0.0
return (vertical1 + vertical2) / (2.0 * horizontal)
def compute_avg_ear(landmarks: np.ndarray) -> float: left_ear = compute_ear(landmarks, _LEFT_EYE_EAR) right_ear = compute_ear(landmarks, _RIGHT_EYE_EAR) return (left_ear + right_ear) / 2.0
def compute_gaze_ratio(landmarks: np.ndarray) -> tuple[float, float]: left_iris = landmarks[_LEFT_IRIS_CENTER, :2] left_inner = landmarks[_LEFT_EYE_INNER, :2] left_outer = landmarks[_LEFT_EYE_OUTER, :2] left_top = landmarks[_LEFT_EYE_TOP, :2] left_bottom = landmarks[_LEFT_EYE_BOTTOM, :2]
right_iris = landmarks[_RIGHT_IRIS_CENTER, :2] right_inner = landmarks[_RIGHT_EYE_INNER, :2] right_outer = landmarks[_RIGHT_EYE_OUTER, :2] right_top = landmarks[_RIGHT_EYE_TOP, :2] right_bottom = landmarks[_RIGHT_EYE_BOTTOM, :2]
left_h_total = _distance(left_inner, left_outer) right_h_total = _distance(right_inner, right_outer)
if left_h_total < 1e-6 or right_h_total < 1e-6: return 0.5, 0.5
left_h_ratio = _distance(left_outer, left_iris) / left_h_total right_h_ratio = _distance(right_outer, right_iris) / right_h_total h_ratio = (left_h_ratio + right_h_ratio) / 2.0
left_v_total = _distance(left_top, left_bottom) right_v_total = _distance(right_top, right_bottom)
if left_v_total < 1e-6 or right_v_total < 1e-6: return h_ratio, 0.5
left_v_ratio = _distance(left_top, left_iris) / left_v_total right_v_ratio = _distance(right_top, right_iris) / right_v_total v_ratio = (left_v_ratio + right_v_ratio) / 2.0
return float(np.clip(h_ratio, 0, 1)), float(np.clip(v_ratio, 0, 1))
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] right = landmarks[_MOUTH_RIGHT, :2] upper1 = landmarks[_MOUTH_UPPER_1, :2] lower1 = landmarks[_MOUTH_LOWER_1, :2] upper2 = landmarks[_MOUTH_UPPER_2, :2] lower2 = landmarks[_MOUTH_LOWER_2, :2]
horizontal = _distance(left, right) if horizontal < 1e-6: return 0.0 v1 = _distance(upper1, lower1) v2 = _distance(top, bottom) v3 = _distance(upper2, lower2) return (v1 + v2 + v3) / (2.0 * horizontal)
class EyeBehaviourScorer: def __init__( self, ear_open: float = 0.30, ear_closed: float = 0.16, gaze_max_offset: float = 0.28, ): self.ear_open = ear_open self.ear_closed = ear_closed self.gaze_max_offset = gaze_max_offset
def _ear_score(self, ear: float) -> float: if ear >= self.ear_open: return 1.0 if ear <= self.ear_closed: return 0.0 return (ear - self.ear_closed) / (self.ear_open - self.ear_closed)
def _gaze_score(self, h_ratio: float, v_ratio: float) -> float: h_offset = abs(h_ratio - 0.5) v_offset = abs(v_ratio - 0.5) offset = math.sqrt(h_offset**2 + v_offset**2) t = min(offset / self.gaze_max_offset, 1.0) return 0.5 * (1.0 + math.cos(math.pi * t))
def score(self, landmarks: np.ndarray) -> float: ear = compute_avg_ear(landmarks) ear_s = self._ear_score(ear) if ear_s < 0.3: return ear_s h_ratio, v_ratio = compute_gaze_ratio(landmarks) gaze_s = self._gaze_score(h_ratio, v_ratio) return ear_s * gaze_s
def detailed_score(self, landmarks: np.ndarray) -> dict: ear = compute_avg_ear(landmarks) ear_s = self._ear_score(ear) h_ratio, v_ratio = compute_gaze_ratio(landmarks) gaze_s = self._gaze_score(h_ratio, v_ratio) s_eye = ear_s if ear_s < 0.3 else ear_s * gaze_s return { "ear": round(ear, 4), "ear_score": round(ear_s, 4), "h_gaze": round(h_ratio, 4), "v_gaze": round(v_ratio, 4), "gaze_score": round(gaze_s, 4), "s_eye": round(s_eye, 4), }1
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112import math
import cv2import numpy as np
_LANDMARK_INDICES = [1, 152, 33, 263, 61, 291]
_MODEL_POINTS = np.array( [ [0.0, 0.0, 0.0], [0.0, -330.0, -65.0], [-225.0, 170.0, -135.0], [225.0, 170.0, -135.0], [-150.0, -150.0, -125.0], [150.0, -150.0, -125.0], ], dtype=np.float64,)
class HeadPoseEstimator: def __init__(self, max_angle: float = 30.0, roll_weight: float = 0.5): self.max_angle = max_angle self.roll_weight = roll_weight self._camera_matrix = None self._frame_size = None self._dist_coeffs = np.zeros((4, 1), dtype=np.float64)
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): return self._camera_matrix focal_length = float(frame_w) cx, cy = frame_w / 2.0, frame_h / 2.0 self._camera_matrix = np.array( [[focal_length, 0, cx], [0, focal_length, cy], [0, 0, 1]], dtype=np.float64, ) self._frame_size = (frame_w, frame_h) return self._camera_matrix
def _solve(self, landmarks: np.ndarray, frame_w: int, frame_h: int): image_points = np.array( [ [landmarks[i, 0] * frame_w, landmarks[i, 1] * frame_h] for i in _LANDMARK_INDICES ], dtype=np.float64, ) camera_matrix = self._get_camera_matrix(frame_w, frame_h) success, rvec, tvec = cv2.solvePnP( _MODEL_POINTS, image_points, camera_matrix, self._dist_coeffs, flags=cv2.SOLVEPNP_ITERATIVE, ) return success, rvec, tvec, image_points
def estimate( self, landmarks: np.ndarray, frame_w: int, frame_h: int ) -> tuple[float, float, float] | None: success, rvec, tvec, _ = self._solve(landmarks, frame_w, frame_h) if not success: return None
rmat, _ = cv2.Rodrigues(rvec) nose_dir = rmat @ np.array([0.0, 0.0, 1.0]) face_up = rmat @ np.array([0.0, 1.0, 0.0])
yaw = math.degrees(math.atan2(nose_dir[0], -nose_dir[2])) pitch = math.degrees(math.asin(np.clip(-nose_dir[1], -1.0, 1.0))) roll = math.degrees(math.atan2(face_up[0], -face_up[1]))
return (yaw, pitch, roll)
def score(self, landmarks: np.ndarray, frame_w: int, frame_h: int) -> float: angles = self.estimate(landmarks, frame_w, frame_h) if angles is None: return 0.0
yaw, pitch, roll = angles deviation = math.sqrt(yaw**2 + pitch**2 + (self.roll_weight * roll) ** 2) t = min(deviation / self.max_angle, 1.0) return 0.5 * (1.0 + math.cos(math.pi * t))
def draw_axes( self, frame: np.ndarray, landmarks: np.ndarray, axis_length: float = 50.0, ) -> np.ndarray: h, w = frame.shape[:2] success, rvec, tvec, image_points = self._solve(landmarks, w, h) if not success: return frame
camera_matrix = self._get_camera_matrix(w, h) nose = tuple(image_points[0].astype(int))
axes_3d = np.float64( [[axis_length, 0, 0], [0, axis_length, 0], [0, 0, axis_length]] ) projected, _ = cv2.projectPoints( axes_3d, rvec, tvec, camera_matrix, self._dist_coeffs )
colors = [(0, 0, 255), (0, 255, 0), (255, 0, 0)] for i, color in enumerate(colors): pt = tuple(projected[i].ravel().astype(int)) cv2.line(frame, nose, pt, color, 2)
return frame123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184import jsonimport osimport random
import numpy as npimport torchimport torch.nn as nnimport torch.optim as optim
from models.prepare_dataset import get_dataloaders
CFG = { "model_name": "face_orientation", "epochs": 30, "batch_size": 32, "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"),}
def set_seed(seed: int): random.seed(seed) np.random.seed(seed) torch.manual_seed(seed) if torch.cuda.is_available(): torch.cuda.manual_seed_all(seed)
class BaseModel(nn.Module): def __init__(self, num_features: int, num_classes: int): super().__init__() self.network = nn.Sequential( nn.Linear(num_features, 64), nn.ReLU(), nn.Linear(64, 32), nn.ReLU(), nn.Linear(32, num_classes), )
def forward(self, x): return self.network(x)
def training_step(self, loader, optimizer, criterion, device): self.train() total_loss = 0.0 correct = 0 total = 0
for features, labels in loader: features, labels = features.to(device), labels.to(device)
optimizer.zero_grad() outputs = self(features) loss = criterion(outputs, labels) loss.backward() optimizer.step()
total_loss += loss.item() * features.size(0) correct += (outputs.argmax(dim=1) == labels).sum().item() total += features.size(0)
return total_loss / total, correct / total
@torch.no_grad() def validation_step(self, loader, criterion, device): self.eval() total_loss = 0.0 correct = 0 total = 0
for features, labels in loader: features, labels = features.to(device), labels.to(device) outputs = self(features) loss = criterion(outputs, labels)
total_loss += loss.item() * features.size(0) correct += (outputs.argmax(dim=1) == labels).sum().item() total += features.size(0)
return total_loss / total, correct / total
@torch.no_grad() def test_step(self, loader, criterion, device): self.eval() total_loss = 0.0 correct = 0 total = 0
for features, labels in loader: features, labels = features.to(device), labels.to(device) outputs = self(features) loss = criterion(outputs, labels)
total_loss += loss.item() * features.size(0) correct += (outputs.argmax(dim=1) == labels).sum().item() total += features.size(0)
return total_loss / total, correct / total
def main(): set_seed(CFG["seed"])
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") print(f"[TRAIN] Device: {device}") print(f"[TRAIN] Model: {CFG['model_name']}")
train_loader, val_loader, test_loader, num_features, num_classes = get_dataloaders( model_name=CFG["model_name"], batch_size=CFG["batch_size"], split_ratios=CFG["split_ratios"], seed=CFG["seed"], )
model = BaseModel(num_features, num_classes).to(device) criterion = nn.CrossEntropyLoss() optimizer = optim.Adam(model.parameters(), lr=CFG["lr"])
print(f"[TRAIN] Parameters: {sum(p.numel() for p in model.parameters()):,}")
ckpt_dir = CFG["checkpoints_dir"][CFG["model_name"]] os.makedirs(ckpt_dir, exist_ok=True) best_ckpt_path = os.path.join(ckpt_dir, "best_model.pt")
history = { "model_name": CFG["model_name"], "epochs": [], "train_loss": [], "train_acc": [], "val_loss": [], "val_acc": [], }
best_val_acc = 0.0
print(f"\n{'Epoch':>6} | {'Train Loss':>10} | {'Train Acc':>9} | {'Val Loss':>10} | {'Val Acc':>9}") print("-" * 60)
for epoch in range(1, CFG["epochs"] + 1): train_loss, train_acc = model.training_step(train_loader, optimizer, criterion, device) val_loss, val_acc = model.validation_step(val_loader, criterion, device)
history["epochs"].append(epoch) history["train_loss"].append(round(train_loss, 4)) history["train_acc"].append(round(train_acc, 4)) history["val_loss"].append(round(val_loss, 4)) history["val_acc"].append(round(val_acc, 4))
marker = "" if val_acc > best_val_acc: best_val_acc = val_acc torch.save(model.state_dict(), best_ckpt_path) marker = " *"
print(f"{epoch:>6} | {train_loss:>10.4f} | {train_acc:>8.2%} | {val_loss:>10.4f} | {val_acc:>8.2%}{marker}")
print(f"\nBest validation accuracy: {best_val_acc:.2%}") 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) print(f"\n[TEST] Loss: {test_loss:.4f} | Accuracy: {test_acc:.2%}")
history["test_loss"] = round(test_loss, 4) history["test_acc"] = round(test_acc, 4)
logs_dir = CFG["logs_dir"] os.makedirs(logs_dir, exist_ok=True) log_path = os.path.join(logs_dir, f"{CFG['model_name']}_training_log.json")
with open(log_path, "w") as f: json.dump(history, f, indent=2)
print(f"[LOG] Training history saved to: {log_path}")
if __name__ == "__main__": main()12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091import osfrom pathlib import Pathfrom urllib.request import urlretrieve
import cv2import numpy as npimport mediapipe as mpfrom mediapipe.tasks.python.vision import FaceLandmarkerOptions, FaceLandmarker, RunningModefrom mediapipe.tasks import python as mp_tasks
_MODEL_URL = ( "https://storage.googleapis.com/mediapipe-models/face_landmarker/" "face_landmarker/float16/latest/face_landmarker.task")
def _ensure_model() -> str: cache_dir = Path(os.environ.get( "FOCUSGUARD_CACHE_DIR", Path.home() / ".cache" / "focusguard", )) model_path = cache_dir / "face_landmarker.task" if model_path.exists(): return str(model_path) cache_dir.mkdir(parents=True, exist_ok=True) print(f"[FACE_MESH] Downloading model to {model_path}...") urlretrieve(_MODEL_URL, model_path) print("[FACE_MESH] Download complete.") return str(model_path)
class FaceMeshDetector: LEFT_EYE_INDICES = [33, 7, 163, 144, 145, 153, 154, 155, 133, 173, 157, 158, 159, 160, 161, 246] RIGHT_EYE_INDICES = [362, 382, 381, 380, 374, 373, 390, 249, 263, 466, 388, 387, 386, 385, 384, 398] LEFT_IRIS_INDICES = [468, 469, 470, 471, 472] RIGHT_IRIS_INDICES = [473, 474, 475, 476, 477]
def __init__( self, max_num_faces: int = 1, min_detection_confidence: float = 0.5, min_tracking_confidence: float = 0.5, ): model_path = _ensure_model() options = FaceLandmarkerOptions( base_options=mp_tasks.BaseOptions(model_asset_path=model_path), num_faces=max_num_faces, min_face_detection_confidence=min_detection_confidence, min_face_presence_confidence=min_detection_confidence, min_tracking_confidence=min_tracking_confidence, running_mode=RunningMode.VIDEO, ) self._landmarker = FaceLandmarker.create_from_options(options) self._frame_ts = 0 # ms, for video API
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) self._frame_ts += 33 # ~30fps result = self._landmarker.detect_for_video(mp_image, self._frame_ts)
if not result.face_landmarks: return None
face = result.face_landmarks[0] return np.array([(lm.x, lm.y, lm.z) for lm in face], dtype=np.float32)
def get_pixel_landmarks(self, landmarks: np.ndarray, frame_w: int, frame_h: int) -> np.ndarray: # norm -> pixel (x,y) pixel = np.zeros((landmarks.shape[0], 2), dtype=np.int32) pixel[:, 0] = (landmarks[:, 0] * frame_w).astype(np.int32) pixel[:, 1] = (landmarks[:, 1] * frame_h).astype(np.int32) return pixel
def get_3d_landmarks(self, landmarks: np.ndarray, frame_w: int, frame_h: int) -> np.ndarray: # norm -> pixel-scale x,y,z (z scaled by width) pts = np.zeros_like(landmarks) pts[:, 0] = landmarks[:, 0] * frame_w pts[:, 1] = landmarks[:, 1] * frame_h pts[:, 2] = landmarks[:, 2] * frame_w return pts
def close(self): self._landmarker.close()
def __enter__(self): return self
def __exit__(self, *args): self.close()112345767# Face mesh + head pose + eye behaviour (Stage 2); eye CNN needs torchmediapipe>=0.10.14opencv-python>=4.8.0numpy>=1.24.0torch>=2.0.0torchvision>=0.15.0# ultralytics # optional: for YOLO open/closed eye classifierscikit-learn>=1.2.0joblib>=1.2.01234567834567812910151112131417181516201722181920# ui
Live demo and session view.
## Stage 2 (face mesh + head pose + eye)
- **pipeline.py** — face mesh → S_face (head pose) + S_eye (geometry + optional YOLO/MobileNet) + MAR/yawn → focus.- **live_demo.py** — webcam + mesh, FOCUSED/NOT FOCUSED, MAR, YAWN, optional eye model.- **pipeline.py** — `FaceMeshPipeline` (head + eye geo ± YOLO → focus) and `MLPPipeline` (loads latest MLP from `MLP/models/`, 10 features → focus)- **live_demo.py** — webcam window, mesh overlay, FOCUSED / NOT FOCUSED
From repo root:
```bashpip install -r requirements.txtpython ui/live_demo.py```With YOLO open/closed model (face mesh crops eyes → 224×224 → YOLO):
MLP only (no head/eye fusion, just your trained MLP):
```bashpip install ultralyticspython ui/live_demo.py --eye-model path/to/yolo.pt --eye-backend yolopython ui/live_demo.py --mlp```With MobileNetV2 (96×96 crops): `--eye-model path/to/best_model.pt --eye-backend mobilenet`.
`q` = quit, `m` = cycle mesh mode (full / contours / off).With YOLO eye model: `python ui/live_demo.py --eye-model path/to/yolo.pt`
`q` quit, `m` cycle mesh (full / contours / off).121237 unmodified lines11121316171415161917181996 unmodified lines11611711812212312412512612712812913011912012112212312412512612712812913013113213313413513613713313413513613713813914014114214313813914014114214314414514614714814915015115215315415015515615715818 unmodified lines177178179180175181182183184185186187182188189190191192193194195196187188189190191192193194195197198199200201202203204205206207208# Stage 1 demo — webcam + face mesh overlay
import argparseimport osimport sys7 unmodified linesif _PROJECT_ROOT not in sys.path: sys.path.insert(0, _PROJECT_ROOT)
from ui.pipeline import FaceMeshPipelinefrom models.face_mesh.face_mesh import FaceMeshDetectorfrom ui.pipeline import FaceMeshPipeline, MLPPipelinefrom models.pretrained.face_mesh.face_mesh import FaceMeshDetector
# DrawingFONT = cv2.FONT_HERSHEY_SIMPLEXCYAN = (255, 255, 0)GREEN = (0, 255, 0)96 unmodified lines
def main(): parser = argparse.ArgumentParser(description="FocusGuard — Face mesh + focus (Stage 2)") parser.add_argument("--camera", type=int, default=0, help="Camera index") parser.add_argument("--max-angle", type=float, default=22.0, help="Max head angle for S_face (deg), smaller = tighter") parser.add_argument("--alpha", type=float, default=0.4, help="S_face weight") parser.add_argument("--beta", type=float, default=0.6, help="S_eye weight") parser.add_argument("--threshold", type=float, default=0.55, help="Score >= this = FOCUSED (higher = stricter)") parser.add_argument("--eye-model", type=str, default=None, help="Path to eye model (YOLO .pt or MobileNet .pt); omit = geometry only") parser.add_argument("--eye-backend", type=str, default="auto", choices=["auto", "mobilenet", "yolo", "geometric"], help="Eye model backend (auto = detect from file)") parser.add_argument("--eye-blend", type=float, default=0.5, help="Blend: (1-blend)*geo + blend*model when model loaded") parser = argparse.ArgumentParser() parser.add_argument("--camera", type=int, default=0) parser.add_argument("--mlp", action="store_true", help="Use MLP model only (load latest from MLP/models/)") parser.add_argument("--mlp-dir", type=str, default=None, help="MLP models dir (default: shared/MLP/models)") parser.add_argument("--max-angle", type=float, default=22.0) parser.add_argument("--alpha", type=float, default=0.4) parser.add_argument("--beta", type=float, default=0.6) parser.add_argument("--threshold", type=float, default=0.55) parser.add_argument("--eye-model", type=str, default=None) parser.add_argument("--eye-backend", type=str, default="yolo", choices=["yolo", "geometric"]) parser.add_argument("--eye-blend", type=float, default=0.5) args = parser.parse_args()
use_mlp_only = args.mlp
if use_mlp_only: print("[DEMO] MLP only — loading latest from MLP/models/") pipeline = MLPPipeline(model_dir=args.mlp_dir) else: eye_mode = " + model" if args.eye_model else " only" print("[DEMO] Face mesh + head pose + eye (geometry" + eye_mode + ")") pipeline = FaceMeshPipeline( max_angle=args.max_angle, alpha=args.alpha, beta=args.beta, threshold=args.threshold, eye_model_path=args.eye_model, eye_backend=args.eye_backend, eye_blend=args.eye_blend, ) eye_mode = " + model" if args.eye_model else " only" print("[DEMO] Face mesh + head pose + eye (geometry" + eye_mode + ")") pipeline = FaceMeshPipeline( max_angle=args.max_angle, alpha=args.alpha, beta=args.beta, threshold=args.threshold, eye_model_path=args.eye_model, eye_backend=args.eye_backend, eye_blend=args.eye_blend, )
cap = cv2.VideoCapture(args.camera) if not cap.isOpened(): print("[DEMO] ERROR: Cannot open camera") return
print("[DEMO] q = quit, m = cycle mesh mode (full / contours / off)") print("[DEMO] q = quit, m = cycle mesh (full/contours/off)" if not use_mlp_only else "[DEMO] q = quit, m = mesh") prev_time = time.time() fps = 0.0 mesh_mode = MESH_FULL18 unmodified lines elif mesh_mode == MESH_CONTOURS: draw_contours(frame, lm, w, h) draw_eyes_and_irises(frame, lm, w, h) if not use_mlp_only: pipeline.head_pose.draw_axes(frame, lm) pipeline.head_pose.draw_axes(frame, lm) if result.get("left_bbox") and result.get("right_bbox"): lx1, ly1, lx2, ly2 = result["left_bbox"] rx1, ry1, rx2, ry2 = result["right_bbox"] cv2.rectangle(frame, (lx1, ly1), (lx2, ly2), YELLOW, 1) cv2.rectangle(frame, (rx1, ry1), (rx2, ry2), YELLOW, 1)
# Status bar: FOCUSED / NOT FOCUSED; YAWN when mouth open (sleepy) status = "FOCUSED" if result["is_focused"] else "NOT FOCUSED" status_color = GREEN if result["is_focused"] else RED cv2.rectangle(frame, (0, 0), (w, 55), (0, 0, 0), -1) cv2.putText(frame, status, (10, 28), FONT, 0.8, status_color, 2, cv2.LINE_AA) if use_mlp_only: cv2.putText(frame, "MLP", (10, 48), FONT, 0.45, WHITE, 1, cv2.LINE_AA) cv2.putText(frame, f"FPS: {fps:.0f}", (w - 80, 28), FONT, 0.45, WHITE, 1, cv2.LINE_AA) cv2.putText(frame, "q:quit m:mesh", (w - 120, 48), FONT, 0.4, (180, 180, 180), 1, cv2.LINE_AA) else: mar_str = f" MAR:{result['mar']:.2f}" if result.get("mar") is not None else "" cv2.putText(frame, f"S_face:{result['s_face']:.2f} S_eye:{result['s_eye']:.2f}{mar_str} score:{result['raw_score']:.2f}", (10, 48), FONT, 0.45, WHITE, 1, cv2.LINE_AA) if result.get("is_yawning"): cv2.putText(frame, "YAWN", (10, 75), FONT, 0.7, ORANGE, 2, cv2.LINE_AA) if result["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) eye_label = f"eye:{pipeline.eye_classifier.name}" if pipeline.has_eye_model else "eye:geo" cv2.putText(frame, f"{_MESH_NAMES[mesh_mode]} {eye_label} FPS: {fps:.0f}", (w - 320, 28), FONT, 0.45, WHITE, 1, cv2.LINE_AA) cv2.putText(frame, "q:quit m:mesh", (w - 140, 48), FONT, 0.4, (180, 180, 180), 1, cv2.LINE_AA) mar_str = f" MAR:{result['mar']:.2f}" if result.get("mar") is not None else "" cv2.putText(frame, f"S_face:{result['s_face']:.2f} S_eye:{result['s_eye']:.2f}{mar_str} score:{result['raw_score']:.2f}", (10, 48), FONT, 0.45, WHITE, 1, cv2.LINE_AA) if result.get("is_yawning"): cv2.putText(frame, "YAWN", (10, 75), FONT, 0.7, ORANGE, 2, cv2.LINE_AA) if result["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) eye_label = f"eye:{pipeline.eye_classifier.name}" if pipeline.has_eye_model else "eye:geo" cv2.putText(frame, f"{_MESH_NAMES[mesh_mode]} {eye_label} FPS: {fps:.0f}", (w - 320, 28), FONT, 0.45, WHITE, 1, cv2.LINE_AA) cv2.putText(frame, "q:quit m:mesh", (w - 140, 48), FONT, 0.4, (180, 180, 180), 1, cv2.LINE_AA)
cv2.imshow("FocusGuard", frame)
121234567891011121314151612131415161718192020212122231 unmodified line25262729282930312 unmodified lines343536383738394026 unmodified lines676869717071727374777576774 unmodified lines828384888586879288899011 unmodified lines102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167# Stage 2: face mesh + head pose (S_face) + eye (geometry + optional model) -> focus
import globimport osimport sys
import numpy as npimport joblib
_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.face_mesh.face_mesh import FaceMeshDetectorfrom models.face_orientation.head_pose import HeadPoseEstimatorfrom models.eye_behaviour.eye_scorer import EyeBehaviourScorer, compute_mar, MAR_YAWN_THRESHOLDfrom models.eye_behaviour.eye_crop import extract_eye_cropsfrom models.eye_behaviour.eye_classifier import load_eye_classifier, GeometricOnlyClassifierfrom 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_mar, MAR_YAWN_THRESHOLDfrom models.cnn.eye_attention.crop import extract_eye_cropsfrom models.cnn.eye_attention.classifier import load_eye_classifier, GeometricOnlyClassifierfrom models.attention.collect_features import FEATURE_NAMES, TemporalTracker, extract_features
class FaceMeshPipeline: # frame -> face mesh -> S_face + S_eye (geo + optional YOLO/MobileNet) -> focused / not focused
def __init__( self, max_angle: float = 22.0,1 unmodified line beta: float = 0.6, threshold: float = 0.55, eye_model_path: str | None = None, eye_backend: str = "auto", eye_backend: str = "yolo", eye_blend: float = 0.5, ): self.detector = FaceMeshDetector()2 unmodified lines self.alpha = alpha self.beta = beta self.threshold = threshold self.eye_blend = eye_blend # 0.5 = 50% geo + 50% model when model loaded self.eye_blend = eye_blend
self.eye_classifier = load_eye_classifier( path=eye_model_path if eye_model_path and os.path.exists(eye_model_path) else None,26 unmodified lines if landmarks is None: return out
# Head pose -> S_face, yaw/pitch/roll angles = self.head_pose.estimate(landmarks, w, h) if angles is not None: out["yaw"], out["pitch"], out["roll"] = angles out["s_face"] = self.head_pose.score(landmarks, w, h)
# Eye: geometry (EAR + gaze) always; optional model (YOLO/MobileNet) on cropped eyes s_eye_geo = self.eye_scorer.score(landmarks) if self._has_eye_model: left_crop, right_crop, left_bbox, right_bbox = extract_eye_crops(bgr_frame, landmarks)4 unmodified lines else: out["s_eye"] = s_eye_geo
# Mouth open (MAR) -> yawn: force NOT FOCUSED when mouth open out["mar"] = compute_mar(landmarks) out["is_yawning"] = out["mar"] > MAR_YAWN_THRESHOLD
# Fusion; yawn overrides out["raw_score"] = self.alpha * out["s_face"] + self.beta * out["s_eye"] out["is_focused"] = out["raw_score"] >= self.threshold and not out["is_yawning"]
11 unmodified lines
def __exit__(self, *args): self.close()
def _latest_mlp_artifacts(model_dir): mlp_files = sorted(glob.glob(os.path.join(model_dir, "mlp_*.joblib"))) if not mlp_files: return None, None, None base = os.path.basename(mlp_files[-1]).replace("mlp_", "").replace(".joblib", "") scaler_path = os.path.join(model_dir, f"scaler_{base}.joblib") meta_path = os.path.join(model_dir, f"meta_{base}.npz") if not os.path.isfile(scaler_path) or not os.path.isfile(meta_path): return None, None, None return mlp_files[-1], scaler_path, meta_path
class MLPPipeline: def __init__(self, model_dir=None): if model_dir is None: model_dir = os.path.join(_PROJECT_ROOT, "MLP", "models") mlp_path, scaler_path, meta_path = _latest_mlp_artifacts(model_dir) if mlp_path is None: raise FileNotFoundError(f"No MLP artifacts in {model_dir}") self._mlp = joblib.load(mlp_path) self._scaler = joblib.load(scaler_path) meta = np.load(meta_path, allow_pickle=True) self._feature_names = list(meta["feature_names"]) self._detector = FaceMeshDetector() self._head_pose = HeadPoseEstimator() self._eye_scorer = EyeBehaviourScorer() self._temporal = TemporalTracker() self._indices = [FEATURE_NAMES.index(n) for n in self._feature_names] print(f"[MLP] Loaded {mlp_path} | {len(self._feature_names)} features")
def process_frame(self, bgr_frame): landmarks = self._detector.process(bgr_frame) h, w = bgr_frame.shape[:2] out = { "landmarks": landmarks, "is_focused": False, "s_face": 0.0, "s_eye": 0.0, "raw_score": 0.0, "mar": None, "yaw": None, "pitch": None, "roll": None, } if landmarks is None: return out vec = extract_features(landmarks, w, h, self._head_pose, self._eye_scorer, self._temporal) X = vec[self._indices].reshape(1, -1).astype(np.float64) X_sc = self._scaler.transform(X) pred = self._mlp.predict(X_sc) out["is_focused"] = bool(pred[0] == 1) return out
def close(self): self._detector.close()
def __enter__(self): return self
def __exit__(self, *args): self.close().gitignore-16
MLP/models/meta_20260224_024200.npzAdded+3
MLP/models/mlp_20260224_024200.joblibAdded+3
MLP/models/scaler_20260224_024200.joblibAdded+3
README.md+7-7
data_preparation/CNN/eye_crops/val/open/.gitkeepAdded+1
data_preparation/MLP/explore_collected_data.ipynbAdded
data_preparation/MLP/train_mlp.ipynbAdded
data_preparation/README.md+40-2
evaluation/README.md+1-1
models/README.md+7-5
models/attention/__init__.pyAdded+1
models/attention/classifier.pyAdded
models/attention/collect_features.pyAdded+349
models/attention/fusion.pyAdded
models/attention/train.pyAdded
models/cnn/__init__.pyAdded
models/cnn/eye_attention/__init__.pyAdded+1
models/cnn/eye_attention/classifier.pyAdded+69
models/cnn/eye_attention/crop.pyAdded+70
models/cnn/eye_attention/train.pyAdded
models/geometric/__init__.pyAdded
models/geometric/eye_behaviour/__init__.pyAdded
models/geometric/eye_behaviour/eye_scorer.pyAdded+164
models/geometric/face_orientation/__init__.pyAdded+1
models/geometric/face_orientation/head_pose.pyAdded+112
models/mlp/__init__.pyAdded
models/mlp/train.pyAdded+184
models/pretrained/__init__.pyAdded
models/pretrained/face_mesh/.gitkeepAdded
models/pretrained/face_mesh/__init__.pyAdded
models/pretrained/face_mesh/face_mesh.pyAdded+91
requirements.txt+2-2
ui/README.md+10-12
ui/live_demo.py+47-37
ui/pipeline.py+73-15