Spaces:
Sleeping
Sleeping
Abdelrahman Almatrooshi commited on
Commit ·
59d3081
1
Parent(s): 4bd140c
integrated open/closed eye cnn in script
Browse filesFiles changed (15)
- models/cnn/CNN_MODEL/.claude/settings.local.json+7-7
- models/cnn/CNN_MODEL/.gitattributes+1-1
- models/cnn/CNN_MODEL/.gitignore+4-4
- models/cnn/CNN_MODEL/README.md+74-74
- models/cnn/CNN_MODEL/notebooks/eye_classifier_colab.ipynb
- models/cnn/CNN_MODEL/scripts/focus_infer.py+199-199
- models/cnn/CNN_MODEL/scripts/predict_image.py+49-49
- models/cnn/CNN_MODEL/scripts/video_infer.py+281-281
- models/cnn/CNN_MODEL/scripts/webcam_live.py+184-184
- models/cnn/eye_attention/__init__.py+1-1
- models/cnn/eye_attention/classifier.py+106-6
- models/cnn/eye_attention/crop.py+70-70
- ui/README.md+20-20
- ui/live_demo.py+224-224
- ui/pipeline.py+3
1234567{"permissions": {"allow": ["Bash(# Check Dataset_subset counts echo \"\"=== Dataset_subset/train/open ===\"\" && ls /Users/mohammedalketbi22/Downloads/GAP_Large_project-feature-dataset-model-test-92_30-clean/Dataset_subset/train/open/ | wc -l && echo \"\"=== Dataset_subset/train/closed ===\"\" && ls /Users/mohammedalketbi22/Downloads/GAP_Large_project-feature-dataset-model-test-92_30-clean/Dataset_subset/train/closed/ | wc -l && echo \"\"=== Dataset_subset/val/open ===\"\" && ls /Users/mohammedalketbi22/Downloads/GAP_Large_project-feature-dataset-model-test-92_30-clean/Dataset_subset/val/open/ | wc -l && echo \"\"=== Dataset_subset/val/closed ===\"\" && ls /Users/mohammedalketbi22/Downloads/GAP_Large_project-feature-dataset-model-test-92_30-clean/Dataset_subset/val/closed/)"]}}1234567{"permissions": {"allow": ["Bash(# Check Dataset_subset counts echo \"\"=== Dataset_subset/train/open ===\"\" && ls /Users/mohammedalketbi22/Downloads/GAP_Large_project-feature-dataset-model-test-92_30-clean/Dataset_subset/train/open/ | wc -l && echo \"\"=== Dataset_subset/train/closed ===\"\" && ls /Users/mohammedalketbi22/Downloads/GAP_Large_project-feature-dataset-model-test-92_30-clean/Dataset_subset/train/closed/ | wc -l && echo \"\"=== Dataset_subset/val/open ===\"\" && ls /Users/mohammedalketbi22/Downloads/GAP_Large_project-feature-dataset-model-test-92_30-clean/Dataset_subset/val/open/ | wc -l && echo \"\"=== Dataset_subset/val/closed ===\"\" && ls /Users/mohammedalketbi22/Downloads/GAP_Large_project-feature-dataset-model-test-92_30-clean/Dataset_subset/val/closed/)"]}}
1DATA/** filter=lfs diff=lfs merge=lfs -text1DATA/** filter=lfs diff=lfs merge=lfs -text
1234Dataset/train/Dataset/val/Dataset/test/.DS_Store1234Dataset/train/Dataset/val/Dataset/test/.DS_Store
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374# Eye Open / Closed Classifier (YOLOv11-CLS)Binary classifier: **open** vs **closed** eyes.Used as a baseline for eye-tracking, drowsiness, or focus detection.---## Model team task- **Train** the YOLOv11s-cls eye classifier in a **separate notebook** (data split, epochs, GPU, export `best.pt`).- Provide **trained weights** (`best.pt`) for this repo’s evaluation and inference scripts.---## Repo contents- **notebooks/eye_classifier_colab.ipynb** — Data download (Kaggle), clean, split, undersample, **evaluate** (needs `best.pt` from model team), export.- **scripts/predict_image.py** — Run classifier on single images (needs `best.pt`).- **scripts/webcam_live.py** — Live webcam open/closed (needs `best.pt` + optional `weights/face_landmarker.task`).- **scripts/video_infer.py** — Run on video files.- **scripts/focus_infer.py** — Focus/attention inference.- **weights/** — Put `best.pt` here; `face_landmarker.task` is downloaded on first webcam run if missing.- **docs/** — Extra docs (e.g. UNNECESSARY_FILES.md if present).---## Dataset- **Source:** [Kaggle — open/closed eyes](https://www.kaggle.com/datasets/sehriyarmemmedli/open-closed-eyes-dataset)- The Colab notebook downloads it via `kagglehub`; no local copy in repo.---## Weights- Put **best.pt** from the model team in **weights/best.pt** (or `runs/classify/runs_cls/eye_open_closed_cpu/weights/best.pt`).- For webcam: **face_landmarker.task** is downloaded into **weights/** on first run if missing.---## Local setup```bashpip install ultralytics opencv-python mediapipe "numpy<2"```Optional: use a venv. From repo root:- `python scripts/predict_image.py <image.png>`- `python scripts/webcam_live.py`- `python scripts/video_infer.py` (expects 1.mp4 / 2.mp4 in repo root or set `VIDEOS` env)- `python scripts/focus_infer.py`---## Project structure```├── notebooks/│ └── eye_classifier_colab.ipynb # Data + eval (no training)├── scripts/│ ├── predict_image.py│ ├── webcam_live.py│ ├── video_infer.py│ └── focus_infer.py├── weights/ # best.pt, face_landmarker.task├── docs/ # extra docs├── README.md└── venv/ # optional```Training and weight generation: **model team, separate notebook.**1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374# Eye Open / Closed Classifier (YOLOv11-CLS)Binary classifier: **open** vs **closed** eyes.Used as a baseline for eye-tracking, drowsiness, or focus detection.---## Model team task- **Train** the YOLOv11s-cls eye classifier in a **separate notebook** (data split, epochs, GPU, export `best.pt`).- Provide **trained weights** (`best.pt`) for this repo’s evaluation and inference scripts.---## Repo contents- **notebooks/eye_classifier_colab.ipynb** — Data download (Kaggle), clean, split, undersample, **evaluate** (needs `best.pt` from model team), export.- **scripts/predict_image.py** — Run classifier on single images (needs `best.pt`).- **scripts/webcam_live.py** — Live webcam open/closed (needs `best.pt` + optional `weights/face_landmarker.task`).- **scripts/video_infer.py** — Run on video files.- **scripts/focus_infer.py** — Focus/attention inference.- **weights/** — Put `best.pt` here; `face_landmarker.task` is downloaded on first webcam run if missing.- **docs/** — Extra docs (e.g. UNNECESSARY_FILES.md if present).---## Dataset- **Source:** [Kaggle — open/closed eyes](https://www.kaggle.com/datasets/sehriyarmemmedli/open-closed-eyes-dataset)- The Colab notebook downloads it via `kagglehub`; no local copy in repo.---## Weights- Put **best.pt** from the model team in **weights/best.pt** (or `runs/classify/runs_cls/eye_open_closed_cpu/weights/best.pt`).- For webcam: **face_landmarker.task** is downloaded into **weights/** on first run if missing.---## Local setup```bashpip install ultralytics opencv-python mediapipe "numpy<2"```Optional: use a venv. From repo root:- `python scripts/predict_image.py <image.png>`- `python scripts/webcam_live.py`- `python scripts/video_infer.py` (expects 1.mp4 / 2.mp4 in repo root or set `VIDEOS` env)- `python scripts/focus_infer.py`---## Project structure```├── notebooks/│ └── eye_classifier_colab.ipynb # Data + eval (no training)├── scripts/│ ├── predict_image.py│ ├── webcam_live.py│ ├── video_infer.py│ └── focus_infer.py├── weights/ # best.pt, face_landmarker.task├── docs/ # extra docs├── README.md└── venv/ # optional```Training and weight generation: **model team, separate notebook.**
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199from __future__ import annotationsfrom pathlib import Pathimport osimport cv2import numpy as npfrom ultralytics import YOLOdef list_images(folder: Path):exts = {".png", ".jpg", ".jpeg", ".bmp", ".webp"}return sorted([p for p in folder.iterdir() if p.suffix.lower() in exts])def find_weights(project_root: Path) -> Path | None:candidates = [project_root / "weights" / "best.pt",project_root / "runs" / "classify" / "runs_cls" / "eye_open_closed_cpu" / "weights" / "best.pt",project_root / "runs" / "classify" / "runs_cls" / "eye_open_closed_cpu" / "weights" / "last.pt",project_root / "runs_cls" / "eye_open_closed_cpu" / "weights" / "best.pt",project_root / "runs_cls" / "eye_open_closed_cpu" / "weights" / "last.pt",]return next((p for p in candidates if p.is_file()), None)def detect_eyelid_boundary(gray: np.ndarray) -> np.ndarray | None:"""Returns an ellipse fit to the largest contour near the eye boundary.Output format: (center(x,y), (axis1, axis2), angle) or None."""blur = cv2.GaussianBlur(gray, (5, 5), 0)edges = cv2.Canny(blur, 40, 120)edges = cv2.dilate(edges, np.ones((3, 3), np.uint8), iterations=1)contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)if not contours:return Nonecontours = sorted(contours, key=cv2.contourArea, reverse=True)for c in contours:if len(c) >= 5 and cv2.contourArea(c) > 50:return cv2.fitEllipse(c)return Nonedef detect_pupil_center(gray: np.ndarray) -> tuple[int, int] | None:"""More robust pupil detection:- enhance contrast (CLAHE)- find dark blobs- score by circularity and proximity to center"""h, w = gray.shapeclahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))eq = clahe.apply(gray)blur = cv2.GaussianBlur(eq, (7, 7), 0)# Focus on the central region to avoid eyelashes/edgescx, cy = w // 2, h // 2rx, ry = int(w * 0.3), int(h * 0.3)x0, x1 = max(cx - rx, 0), min(cx + rx, w)y0, y1 = max(cy - ry, 0), min(cy + ry, h)roi = blur[y0:y1, x0:x1]# Inverted threshold to capture dark pupil_, thresh = cv2.threshold(roi, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)thresh = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, np.ones((3, 3), np.uint8), iterations=2)thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, np.ones((5, 5), np.uint8), iterations=1)contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)if not contours:return Nonebest = Nonebest_score = -1.0for c in contours:area = cv2.contourArea(c)if area < 15:continueperimeter = cv2.arcLength(c, True)if perimeter <= 0:continuecircularity = 4 * np.pi * (area / (perimeter * perimeter))if circularity < 0.3:continuem = cv2.moments(c)if m["m00"] == 0:continuepx = int(m["m10"] / m["m00"]) + x0py = int(m["m01"] / m["m00"]) + y0# Score by circularity and distance to centerdist = np.hypot(px - cx, py - cy) / max(w, h)score = circularity - distif score > best_score:best_score = scorebest = (px, py)return bestdef is_focused(pupil_center: tuple[int, int], img_shape: tuple[int, int]) -> bool:"""Decide focus based on pupil offset from image center."""h, w = img_shapecx, cy = w // 2, h // 2px, py = pupil_centerdx = abs(px - cx) / max(w, 1)dy = abs(py - cy) / max(h, 1)return (dx < 0.12) and (dy < 0.12)def annotate(img_bgr: np.ndarray, ellipse, pupil_center, focused: bool, cls_label: str, conf: float):out = img_bgr.copy()if ellipse is not None:cv2.ellipse(out, ellipse, (0, 255, 255), 2)if pupil_center is not None:cv2.circle(out, pupil_center, 4, (0, 0, 255), -1)label = f"{cls_label} ({conf:.2f}) | focused={int(focused)}"cv2.putText(out, label, (8, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)return outdef main():project_root = Path(__file__).resolve().parent.parentdata_dir = project_root / "Dataset"alt_data_dir = project_root / "DATA"out_dir = project_root / "runs_focus"out_dir.mkdir(parents=True, exist_ok=True)weights = find_weights(project_root)if weights is None:print("Weights not found. Train first.")return# Support both Dataset/test/{open,closed} and Dataset/{open,closed}def resolve_test_dirs(root: Path):test_open = root / "test" / "open"test_closed = root / "test" / "closed"if test_open.exists() and test_closed.exists():return test_open, test_closedtest_open = root / "open"test_closed = root / "closed"if test_open.exists() and test_closed.exists():return test_open, test_closedalt_closed = root / "close"if test_open.exists() and alt_closed.exists():return test_open, alt_closedreturn None, Nonetest_open, test_closed = resolve_test_dirs(data_dir)if (test_open is None or test_closed is None) and alt_data_dir.exists():test_open, test_closed = resolve_test_dirs(alt_data_dir)if not test_open.exists() or not test_closed.exists():print("Test folders missing. Expected:")print(test_open)print(test_closed)returntest_files = list_images(test_open) + list_images(test_closed)print("Total test images:", len(test_files))max_images = int(os.getenv("MAX_IMAGES", "0"))if max_images > 0:test_files = test_files[:max_images]print("Limiting to MAX_IMAGES:", max_images)model = YOLO(str(weights))results = model.predict(test_files, imgsz=224, device="cpu", verbose=False)names = model.namesfor r in results:probs = r.probstop_idx = int(probs.top1)top_conf = float(probs.top1conf)pred_label = names[top_idx]img = cv2.imread(r.path)if img is None:continuegray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)ellipse = detect_eyelid_boundary(gray)pupil_center = detect_pupil_center(gray)focused = Falseif pred_label.lower() == "open" and pupil_center is not None:focused = is_focused(pupil_center, gray.shape)annotated = annotate(img, ellipse, pupil_center, focused, pred_label, top_conf)out_path = out_dir / (Path(r.path).stem + "_annotated.jpg")cv2.imwrite(str(out_path), annotated)print(f"{Path(r.path).name}: pred={pred_label} conf={top_conf:.3f} focused={focused}")print(f"\nAnnotated outputs saved to: {out_dir}")if __name__ == "__main__":main()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199from __future__ import annotationsfrom pathlib import Pathimport osimport cv2import numpy as npfrom ultralytics import YOLOdef list_images(folder: Path):exts = {".png", ".jpg", ".jpeg", ".bmp", ".webp"}return sorted([p for p in folder.iterdir() if p.suffix.lower() in exts])def find_weights(project_root: Path) -> Path | None:candidates = [project_root / "weights" / "best.pt",project_root / "runs" / "classify" / "runs_cls" / "eye_open_closed_cpu" / "weights" / "best.pt",project_root / "runs" / "classify" / "runs_cls" / "eye_open_closed_cpu" / "weights" / "last.pt",project_root / "runs_cls" / "eye_open_closed_cpu" / "weights" / "best.pt",project_root / "runs_cls" / "eye_open_closed_cpu" / "weights" / "last.pt",]return next((p for p in candidates if p.is_file()), None)def detect_eyelid_boundary(gray: np.ndarray) -> np.ndarray | None:"""Returns an ellipse fit to the largest contour near the eye boundary.Output format: (center(x,y), (axis1, axis2), angle) or None."""blur = cv2.GaussianBlur(gray, (5, 5), 0)edges = cv2.Canny(blur, 40, 120)edges = cv2.dilate(edges, np.ones((3, 3), np.uint8), iterations=1)contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)if not contours:return Nonecontours = sorted(contours, key=cv2.contourArea, reverse=True)for c in contours:if len(c) >= 5 and cv2.contourArea(c) > 50:return cv2.fitEllipse(c)return Nonedef detect_pupil_center(gray: np.ndarray) -> tuple[int, int] | None:"""More robust pupil detection:- enhance contrast (CLAHE)- find dark blobs- score by circularity and proximity to center"""h, w = gray.shapeclahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))eq = clahe.apply(gray)blur = cv2.GaussianBlur(eq, (7, 7), 0)# Focus on the central region to avoid eyelashes/edgescx, cy = w // 2, h // 2rx, ry = int(w * 0.3), int(h * 0.3)x0, x1 = max(cx - rx, 0), min(cx + rx, w)y0, y1 = max(cy - ry, 0), min(cy + ry, h)roi = blur[y0:y1, x0:x1]# Inverted threshold to capture dark pupil_, thresh = cv2.threshold(roi, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)thresh = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, np.ones((3, 3), np.uint8), iterations=2)thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, np.ones((5, 5), np.uint8), iterations=1)contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)if not contours:return Nonebest = Nonebest_score = -1.0for c in contours:area = cv2.contourArea(c)if area < 15:continueperimeter = cv2.arcLength(c, True)if perimeter <= 0:continuecircularity = 4 * np.pi * (area / (perimeter * perimeter))if circularity < 0.3:continuem = cv2.moments(c)if m["m00"] == 0:continuepx = int(m["m10"] / m["m00"]) + x0py = int(m["m01"] / m["m00"]) + y0# Score by circularity and distance to centerdist = np.hypot(px - cx, py - cy) / max(w, h)score = circularity - distif score > best_score:best_score = scorebest = (px, py)return bestdef is_focused(pupil_center: tuple[int, int], img_shape: tuple[int, int]) -> bool:"""Decide focus based on pupil offset from image center."""h, w = img_shapecx, cy = w // 2, h // 2px, py = pupil_centerdx = abs(px - cx) / max(w, 1)dy = abs(py - cy) / max(h, 1)return (dx < 0.12) and (dy < 0.12)def annotate(img_bgr: np.ndarray, ellipse, pupil_center, focused: bool, cls_label: str, conf: float):out = img_bgr.copy()if ellipse is not None:cv2.ellipse(out, ellipse, (0, 255, 255), 2)if pupil_center is not None:cv2.circle(out, pupil_center, 4, (0, 0, 255), -1)label = f"{cls_label} ({conf:.2f}) | focused={int(focused)}"cv2.putText(out, label, (8, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)return outdef main():project_root = Path(__file__).resolve().parent.parentdata_dir = project_root / "Dataset"alt_data_dir = project_root / "DATA"out_dir = project_root / "runs_focus"out_dir.mkdir(parents=True, exist_ok=True)weights = find_weights(project_root)if weights is None:print("Weights not found. Train first.")return# Support both Dataset/test/{open,closed} and Dataset/{open,closed}def resolve_test_dirs(root: Path):test_open = root / "test" / "open"test_closed = root / "test" / "closed"if test_open.exists() and test_closed.exists():return test_open, test_closedtest_open = root / "open"test_closed = root / "closed"if test_open.exists() and test_closed.exists():return test_open, test_closedalt_closed = root / "close"if test_open.exists() and alt_closed.exists():return test_open, alt_closedreturn None, Nonetest_open, test_closed = resolve_test_dirs(data_dir)if (test_open is None or test_closed is None) and alt_data_dir.exists():test_open, test_closed = resolve_test_dirs(alt_data_dir)if not test_open.exists() or not test_closed.exists():print("Test folders missing. Expected:")print(test_open)print(test_closed)returntest_files = list_images(test_open) + list_images(test_closed)print("Total test images:", len(test_files))max_images = int(os.getenv("MAX_IMAGES", "0"))if max_images > 0:test_files = test_files[:max_images]print("Limiting to MAX_IMAGES:", max_images)model = YOLO(str(weights))results = model.predict(test_files, imgsz=224, device="cpu", verbose=False)names = model.namesfor r in results:probs = r.probstop_idx = int(probs.top1)top_conf = float(probs.top1conf)pred_label = names[top_idx]img = cv2.imread(r.path)if img is None:continuegray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)ellipse = detect_eyelid_boundary(gray)pupil_center = detect_pupil_center(gray)focused = Falseif pred_label.lower() == "open" and pupil_center is not None:focused = is_focused(pupil_center, gray.shape)annotated = annotate(img, ellipse, pupil_center, focused, pred_label, top_conf)out_path = out_dir / (Path(r.path).stem + "_annotated.jpg")cv2.imwrite(str(out_path), annotated)print(f"{Path(r.path).name}: pred={pred_label} conf={top_conf:.3f} focused={focused}")print(f"\nAnnotated outputs saved to: {out_dir}")if __name__ == "__main__":main()
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849"""Run the eye open/closed model on one or more images."""import sysfrom pathlib import Pathfrom ultralytics import YOLOdef main():project_root = Path(__file__).resolve().parent.parentweight_candidates = [project_root / "weights" / "best.pt",project_root / "runs" / "classify" / "runs_cls" / "eye_open_closed_cpu" / "weights" / "best.pt",project_root / "runs" / "classify" / "runs_cls" / "eye_open_closed_cpu" / "weights" / "last.pt",]weights = next((p for p in weight_candidates if p.is_file()), None)if weights is None:print("Weights not found. Put best.pt in weights/ or runs/.../weights/ (from model team).")sys.exit(1)if len(sys.argv) < 2:print("Usage: python scripts/predict_image.py <image1> [image2 ...]")print("Example: python scripts/predict_image.py path/to/image.png")sys.exit(0)model = YOLO(str(weights))names = model.namesfor path in sys.argv[1:]:p = Path(path)if not p.is_file():print(p, "- file not found")continuetry:results = model.predict(str(p), imgsz=224, device="cpu", verbose=False)except Exception as e:print(p, "- error:", e)continueif not results:print(p, "- no result")continuer = results[0]top_idx = int(r.probs.top1)conf = float(r.probs.top1conf)label = names[top_idx]print(f"{p.name}: {label} ({conf:.2%})")if __name__ == "__main__":main()12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849"""Run the eye open/closed model on one or more images."""import sysfrom pathlib import Pathfrom ultralytics import YOLOdef main():project_root = Path(__file__).resolve().parent.parentweight_candidates = [project_root / "weights" / "best.pt",project_root / "runs" / "classify" / "runs_cls" / "eye_open_closed_cpu" / "weights" / "best.pt",project_root / "runs" / "classify" / "runs_cls" / "eye_open_closed_cpu" / "weights" / "last.pt",]weights = next((p for p in weight_candidates if p.is_file()), None)if weights is None:print("Weights not found. Put best.pt in weights/ or runs/.../weights/ (from model team).")sys.exit(1)if len(sys.argv) < 2:print("Usage: python scripts/predict_image.py <image1> [image2 ...]")print("Example: python scripts/predict_image.py path/to/image.png")sys.exit(0)model = YOLO(str(weights))names = model.namesfor path in sys.argv[1:]:p = Path(path)if not p.is_file():print(p, "- file not found")continuetry:results = model.predict(str(p), imgsz=224, device="cpu", verbose=False)except Exception as e:print(p, "- error:", e)continueif not results:print(p, "- no result")continuer = results[0]top_idx = int(r.probs.top1)conf = float(r.probs.top1conf)label = names[top_idx]print(f"{p.name}: {label} ({conf:.2%})")if __name__ == "__main__":main()
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281from __future__ import annotationsimport osfrom pathlib import Pathimport cv2import numpy as npfrom ultralytics import YOLOtry:import mediapipe as mpexcept Exception: # pragma: no covermp = Nonedef find_weights(project_root: Path) -> Path | None:candidates = [project_root / "weights" / "best.pt",project_root / "runs" / "classify" / "runs_cls" / "eye_open_closed_cpu" / "weights" / "best.pt",project_root / "runs" / "classify" / "runs_cls" / "eye_open_closed_cpu" / "weights" / "last.pt",project_root / "runs_cls" / "eye_open_closed_cpu" / "weights" / "best.pt",project_root / "runs_cls" / "eye_open_closed_cpu" / "weights" / "last.pt",]return next((p for p in candidates if p.is_file()), None)def detect_pupil_center(gray: np.ndarray) -> tuple[int, int] | None:h, w = gray.shapeclahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))eq = clahe.apply(gray)blur = cv2.GaussianBlur(eq, (7, 7), 0)cx, cy = w // 2, h // 2rx, ry = int(w * 0.3), int(h * 0.3)x0, x1 = max(cx - rx, 0), min(cx + rx, w)y0, y1 = max(cy - ry, 0), min(cy + ry, h)roi = blur[y0:y1, x0:x1]_, thresh = cv2.threshold(roi, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)thresh = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, np.ones((3, 3), np.uint8), iterations=2)thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, np.ones((5, 5), np.uint8), iterations=1)contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)if not contours:return Nonebest = Nonebest_score = -1.0for c in contours:area = cv2.contourArea(c)if area < 15:continueperimeter = cv2.arcLength(c, True)if perimeter <= 0:continuecircularity = 4 * np.pi * (area / (perimeter * perimeter))if circularity < 0.3:continuem = cv2.moments(c)if m["m00"] == 0:continuepx = int(m["m10"] / m["m00"]) + x0py = int(m["m01"] / m["m00"]) + y0dist = np.hypot(px - cx, py - cy) / max(w, h)score = circularity - distif score > best_score:best_score = scorebest = (px, py)return bestdef is_focused(pupil_center: tuple[int, int], img_shape: tuple[int, int]) -> bool:h, w = img_shapecx = w // 2px, _ = pupil_centerdx = abs(px - cx) / max(w, 1)return dx < 0.12def classify_frame(model: YOLO, frame: np.ndarray) -> tuple[str, float]:# Use classifier directly on frame (assumes frame is eye crop)results = model.predict(frame, imgsz=224, device="cpu", verbose=False)r = results[0]probs = r.probstop_idx = int(probs.top1)top_conf = float(probs.top1conf)pred_label = model.names[top_idx]return pred_label, top_confdef annotate_frame(frame: np.ndarray, label: str, focused: bool, conf: float, time_sec: float):out = frame.copy()text = f"{label} | focused={int(focused)} | conf={conf:.2f} | t={time_sec:.2f}s"cv2.putText(out, text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2)return outdef write_segments(path: Path, segments: list[tuple[float, float, str]]):with path.open("w") as f:for start, end, label in segments:f.write(f"{start:.2f},{end:.2f},{label}\n")def process_video(video_path: Path, model: YOLO | None):cap = cv2.VideoCapture(str(video_path))if not cap.isOpened():print(f"Failed to open {video_path}")returnfps = cap.get(cv2.CAP_PROP_FPS) or 30.0width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))out_path = video_path.with_name(video_path.stem + "_pred.mp4")fourcc = cv2.VideoWriter_fourcc(*"mp4v")writer = cv2.VideoWriter(str(out_path), fourcc, fps, (width, height))csv_path = video_path.with_name(video_path.stem + "_predictions.csv")seg_path = video_path.with_name(video_path.stem + "_segments.txt")frame_idx = 0last_label = Noneseg_start = 0.0segments: list[tuple[float, float, str]] = []with csv_path.open("w") as fcsv:fcsv.write("time_sec,label,focused,conf\n")if mp is None:print("mediapipe is not installed. Falling back to classifier-only mode.")use_mp = mp is not Noneif use_mp:mp_face_mesh = mp.solutions.face_meshface_mesh = mp_face_mesh.FaceMesh(static_image_mode=False,max_num_faces=1,refine_landmarks=True,min_detection_confidence=0.5,min_tracking_confidence=0.5,)while True:ret, frame = cap.read()if not ret:breaktime_sec = frame_idx / fpsconf = 0.0pred_label = "open"focused = Falseif use_mp:rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)res = face_mesh.process(rgb)if res.multi_face_landmarks:lm = res.multi_face_landmarks[0].landmarkh, w = frame.shape[:2]# Eye landmarks (MediaPipe FaceMesh)left_eye = [33, 160, 158, 133, 153, 144]right_eye = [362, 385, 387, 263, 373, 380]left_iris = [468, 469, 470, 471]right_iris = [473, 474, 475, 476]def pts(idxs):return np.array([(int(lm[i].x * w), int(lm[i].y * h)) for i in idxs])def ear(eye_pts):# EAR using 6 pointsp1, p2, p3, p4, p5, p6 = eye_ptsv1 = np.linalg.norm(p2 - p6)v2 = np.linalg.norm(p3 - p5)h1 = np.linalg.norm(p1 - p4)return (v1 + v2) / (2.0 * h1 + 1e-6)le = pts(left_eye)re = pts(right_eye)le_ear = ear(le)re_ear = ear(re)ear_avg = (le_ear + re_ear) / 2.0# openness thresholdpred_label = "open" if ear_avg > 0.22 else "closed"# iris centersli = pts(left_iris)ri = pts(right_iris)li_c = li.mean(axis=0).astype(int)ri_c = ri.mean(axis=0).astype(int)# eye centers (midpoint of corners)le_c = ((le[0] + le[3]) / 2).astype(int)re_c = ((re[0] + re[3]) / 2).astype(int)# focus = iris close to eye center horizontally for both eyesle_dx = abs(li_c[0] - le_c[0]) / max(np.linalg.norm(le[0] - le[3]), 1)re_dx = abs(ri_c[0] - re_c[0]) / max(np.linalg.norm(re[0] - re[3]), 1)focused = (pred_label == "open") and (le_dx < 0.18) and (re_dx < 0.18)# draw eye boundariescv2.polylines(frame, [le], True, (0, 255, 255), 1)cv2.polylines(frame, [re], True, (0, 255, 255), 1)# draw iris centerscv2.circle(frame, tuple(li_c), 3, (0, 0, 255), -1)cv2.circle(frame, tuple(ri_c), 3, (0, 0, 255), -1)else:pred_label = "closed"focused = Falseelse:if model is not None:pred_label, conf = classify_frame(model, frame)gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)pupil_center = detect_pupil_center(gray) if pred_label.lower() == "open" else Nonefocused = Falseif pred_label.lower() == "open" and pupil_center is not None:focused = is_focused(pupil_center, gray.shape)if pred_label.lower() != "open":focused = Falselabel = "open_focused" if (pred_label.lower() == "open" and focused) else "open_not_focused"if pred_label.lower() != "open":label = "closed_not_focused"fcsv.write(f"{time_sec:.2f},{label},{int(focused)},{conf:.4f}\n")if last_label is None:last_label = labelseg_start = time_secelif label != last_label:segments.append((seg_start, time_sec, last_label))seg_start = time_seclast_label = labelannotated = annotate_frame(frame, label, focused, conf, time_sec)writer.write(annotated)frame_idx += 1if last_label is not None:end_time = frame_idx / fpssegments.append((seg_start, end_time, last_label))write_segments(seg_path, segments)cap.release()writer.release()print(f"Saved: {out_path}")print(f"CSV: {csv_path}")print(f"Segments: {seg_path}")def main():project_root = Path(__file__).resolve().parent.parentweights = find_weights(project_root)model = YOLO(str(weights)) if weights is not None else None# Default to 1.mp4 and 2.mp4 in project rootvideos = []for name in ["1.mp4", "2.mp4"]:p = project_root / nameif p.exists():videos.append(p)# Also allow passing paths via env varextra = os.getenv("VIDEOS", "")for v in [x.strip() for x in extra.split(",") if x.strip()]:vp = Path(v)if not vp.is_absolute():vp = project_root / vpif vp.exists():videos.append(vp)if not videos:print("No videos found. Expected 1.mp4 / 2.mp4 in project root.")returnfor v in videos:process_video(v, model)if __name__ == "__main__":main()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281from __future__ import annotationsimport osfrom pathlib import Pathimport cv2import numpy as npfrom ultralytics import YOLOtry:import mediapipe as mpexcept Exception: # pragma: no covermp = Nonedef find_weights(project_root: Path) -> Path | None:candidates = [project_root / "weights" / "best.pt",project_root / "runs" / "classify" / "runs_cls" / "eye_open_closed_cpu" / "weights" / "best.pt",project_root / "runs" / "classify" / "runs_cls" / "eye_open_closed_cpu" / "weights" / "last.pt",project_root / "runs_cls" / "eye_open_closed_cpu" / "weights" / "best.pt",project_root / "runs_cls" / "eye_open_closed_cpu" / "weights" / "last.pt",]return next((p for p in candidates if p.is_file()), None)def detect_pupil_center(gray: np.ndarray) -> tuple[int, int] | None:h, w = gray.shapeclahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))eq = clahe.apply(gray)blur = cv2.GaussianBlur(eq, (7, 7), 0)cx, cy = w // 2, h // 2rx, ry = int(w * 0.3), int(h * 0.3)x0, x1 = max(cx - rx, 0), min(cx + rx, w)y0, y1 = max(cy - ry, 0), min(cy + ry, h)roi = blur[y0:y1, x0:x1]_, thresh = cv2.threshold(roi, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)thresh = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, np.ones((3, 3), np.uint8), iterations=2)thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, np.ones((5, 5), np.uint8), iterations=1)contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)if not contours:return Nonebest = Nonebest_score = -1.0for c in contours:area = cv2.contourArea(c)if area < 15:continueperimeter = cv2.arcLength(c, True)if perimeter <= 0:continuecircularity = 4 * np.pi * (area / (perimeter * perimeter))if circularity < 0.3:continuem = cv2.moments(c)if m["m00"] == 0:continuepx = int(m["m10"] / m["m00"]) + x0py = int(m["m01"] / m["m00"]) + y0dist = np.hypot(px - cx, py - cy) / max(w, h)score = circularity - distif score > best_score:best_score = scorebest = (px, py)return bestdef is_focused(pupil_center: tuple[int, int], img_shape: tuple[int, int]) -> bool:h, w = img_shapecx = w // 2px, _ = pupil_centerdx = abs(px - cx) / max(w, 1)return dx < 0.12def classify_frame(model: YOLO, frame: np.ndarray) -> tuple[str, float]:# Use classifier directly on frame (assumes frame is eye crop)results = model.predict(frame, imgsz=224, device="cpu", verbose=False)r = results[0]probs = r.probstop_idx = int(probs.top1)top_conf = float(probs.top1conf)pred_label = model.names[top_idx]return pred_label, top_confdef annotate_frame(frame: np.ndarray, label: str, focused: bool, conf: float, time_sec: float):out = frame.copy()text = f"{label} | focused={int(focused)} | conf={conf:.2f} | t={time_sec:.2f}s"cv2.putText(out, text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2)return outdef write_segments(path: Path, segments: list[tuple[float, float, str]]):with path.open("w") as f:for start, end, label in segments:f.write(f"{start:.2f},{end:.2f},{label}\n")def process_video(video_path: Path, model: YOLO | None):cap = cv2.VideoCapture(str(video_path))if not cap.isOpened():print(f"Failed to open {video_path}")returnfps = cap.get(cv2.CAP_PROP_FPS) or 30.0width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))out_path = video_path.with_name(video_path.stem + "_pred.mp4")fourcc = cv2.VideoWriter_fourcc(*"mp4v")writer = cv2.VideoWriter(str(out_path), fourcc, fps, (width, height))csv_path = video_path.with_name(video_path.stem + "_predictions.csv")seg_path = video_path.with_name(video_path.stem + "_segments.txt")frame_idx = 0last_label = Noneseg_start = 0.0segments: list[tuple[float, float, str]] = []with csv_path.open("w") as fcsv:fcsv.write("time_sec,label,focused,conf\n")if mp is None:print("mediapipe is not installed. Falling back to classifier-only mode.")use_mp = mp is not Noneif use_mp:mp_face_mesh = mp.solutions.face_meshface_mesh = mp_face_mesh.FaceMesh(static_image_mode=False,max_num_faces=1,refine_landmarks=True,min_detection_confidence=0.5,min_tracking_confidence=0.5,)while True:ret, frame = cap.read()if not ret:breaktime_sec = frame_idx / fpsconf = 0.0pred_label = "open"focused = Falseif use_mp:rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)res = face_mesh.process(rgb)if res.multi_face_landmarks:lm = res.multi_face_landmarks[0].landmarkh, w = frame.shape[:2]# Eye landmarks (MediaPipe FaceMesh)left_eye = [33, 160, 158, 133, 153, 144]right_eye = [362, 385, 387, 263, 373, 380]left_iris = [468, 469, 470, 471]right_iris = [473, 474, 475, 476]def pts(idxs):return np.array([(int(lm[i].x * w), int(lm[i].y * h)) for i in idxs])def ear(eye_pts):# EAR using 6 pointsp1, p2, p3, p4, p5, p6 = eye_ptsv1 = np.linalg.norm(p2 - p6)v2 = np.linalg.norm(p3 - p5)h1 = np.linalg.norm(p1 - p4)return (v1 + v2) / (2.0 * h1 + 1e-6)le = pts(left_eye)re = pts(right_eye)le_ear = ear(le)re_ear = ear(re)ear_avg = (le_ear + re_ear) / 2.0# openness thresholdpred_label = "open" if ear_avg > 0.22 else "closed"# iris centersli = pts(left_iris)ri = pts(right_iris)li_c = li.mean(axis=0).astype(int)ri_c = ri.mean(axis=0).astype(int)# eye centers (midpoint of corners)le_c = ((le[0] + le[3]) / 2).astype(int)re_c = ((re[0] + re[3]) / 2).astype(int)# focus = iris close to eye center horizontally for both eyesle_dx = abs(li_c[0] - le_c[0]) / max(np.linalg.norm(le[0] - le[3]), 1)re_dx = abs(ri_c[0] - re_c[0]) / max(np.linalg.norm(re[0] - re[3]), 1)focused = (pred_label == "open") and (le_dx < 0.18) and (re_dx < 0.18)# draw eye boundariescv2.polylines(frame, [le], True, (0, 255, 255), 1)cv2.polylines(frame, [re], True, (0, 255, 255), 1)# draw iris centerscv2.circle(frame, tuple(li_c), 3, (0, 0, 255), -1)cv2.circle(frame, tuple(ri_c), 3, (0, 0, 255), -1)else:pred_label = "closed"focused = Falseelse:if model is not None:pred_label, conf = classify_frame(model, frame)gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)pupil_center = detect_pupil_center(gray) if pred_label.lower() == "open" else Nonefocused = Falseif pred_label.lower() == "open" and pupil_center is not None:focused = is_focused(pupil_center, gray.shape)if pred_label.lower() != "open":focused = Falselabel = "open_focused" if (pred_label.lower() == "open" and focused) else "open_not_focused"if pred_label.lower() != "open":label = "closed_not_focused"fcsv.write(f"{time_sec:.2f},{label},{int(focused)},{conf:.4f}\n")if last_label is None:last_label = labelseg_start = time_secelif label != last_label:segments.append((seg_start, time_sec, last_label))seg_start = time_seclast_label = labelannotated = annotate_frame(frame, label, focused, conf, time_sec)writer.write(annotated)frame_idx += 1if last_label is not None:end_time = frame_idx / fpssegments.append((seg_start, end_time, last_label))write_segments(seg_path, segments)cap.release()writer.release()print(f"Saved: {out_path}")print(f"CSV: {csv_path}")print(f"Segments: {seg_path}")def main():project_root = Path(__file__).resolve().parent.parentweights = find_weights(project_root)model = YOLO(str(weights)) if weights is not None else None# Default to 1.mp4 and 2.mp4 in project rootvideos = []for name in ["1.mp4", "2.mp4"]:p = project_root / nameif p.exists():videos.append(p)# Also allow passing paths via env varextra = os.getenv("VIDEOS", "")for v in [x.strip() for x in extra.split(",") if x.strip()]:vp = Path(v)if not vp.is_absolute():vp = project_root / vpif vp.exists():videos.append(vp)if not videos:print("No videos found. Expected 1.mp4 / 2.mp4 in project root.")returnfor v in videos:process_video(v, model)if __name__ == "__main__":main()
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184"""Live webcam: detect face, crop each eye, run open/closed classifier, show on screen.Requires: opencv-python, ultralytics, mediapipe (pip install mediapipe).Press 'q' to quit."""import urllib.requestfrom pathlib import Pathimport cv2import numpy as npfrom ultralytics import YOLOtry:import mediapipe as mp_mp_has_solutions = hasattr(mp, "solutions")except ImportError:mp = None_mp_has_solutions = False# New MediaPipe Tasks API (Face Landmarker) eye indicesLEFT_EYE_INDICES_NEW = [263, 249, 390, 373, 374, 380, 381, 382, 362, 466, 388, 387, 386, 385, 384, 398]RIGHT_EYE_INDICES_NEW = [33, 7, 163, 144, 145, 153, 154, 155, 133, 246, 161, 160, 159, 158, 157, 173]# Old Face Mesh (solutions) indicesLEFT_EYE_INDICES_OLD = [33, 160, 158, 133, 153, 144]RIGHT_EYE_INDICES_OLD = [362, 385, 387, 263, 373, 380]EYE_PADDING = 0.35def find_weights(project_root: Path) -> Path | None:candidates = [project_root / "weights" / "best.pt",project_root / "runs" / "classify" / "runs_cls" / "eye_open_closed_cpu" / "weights" / "best.pt",project_root / "runs" / "classify" / "runs_cls" / "eye_open_closed_cpu" / "weights" / "last.pt",]return next((p for p in candidates if p.is_file()), None)def get_eye_roi(frame: np.ndarray, landmarks, indices: list[int]) -> np.ndarray | None:h, w = frame.shape[:2]pts = np.array([(int(landmarks[i].x * w), int(landmarks[i].y * h)) for i in indices])x_min, y_min = pts.min(axis=0)x_max, y_max = pts.max(axis=0)dx = max(int((x_max - x_min) * EYE_PADDING), 8)dy = max(int((y_max - y_min) * EYE_PADDING), 8)x0 = max(0, x_min - dx)y0 = max(0, y_min - dy)x1 = min(w, x_max + dx)y1 = min(h, y_max + dy)if x1 <= x0 or y1 <= y0:return Nonereturn frame[y0:y1, x0:x1].copy()def _run_with_solutions(mp, model, cap):face_mesh = mp.solutions.face_mesh.FaceMesh(static_image_mode=False,max_num_faces=1,refine_landmarks=True,min_detection_confidence=0.5,min_tracking_confidence=0.5,)while True:ret, frame = cap.read()if not ret:breakrgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)results = face_mesh.process(rgb)left_label, left_conf = "—", 0.0right_label, right_conf = "—", 0.0if results.multi_face_landmarks:lm = results.multi_face_landmarks[0].landmarkfor roi, indices, side in [(get_eye_roi(frame, lm, LEFT_EYE_INDICES_OLD), LEFT_EYE_INDICES_OLD, "left"),(get_eye_roi(frame, lm, RIGHT_EYE_INDICES_OLD), RIGHT_EYE_INDICES_OLD, "right"),]:if roi is not None and roi.size > 0:try:pred = model.predict(roi, imgsz=224, device="cpu", verbose=False)if pred:r = pred[0]label = model.names[int(r.probs.top1)]conf = float(r.probs.top1conf)if side == "left":left_label, left_conf = label, confelse:right_label, right_conf = label, confexcept Exception:passcv2.putText(frame, f"L: {left_label} ({left_conf:.0%})", (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)cv2.putText(frame, f"R: {right_label} ({right_conf:.0%})", (20, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)cv2.imshow("Eye open/closed (q to quit)", frame)if cv2.waitKey(1) & 0xFF == ord("q"):breakdef _run_with_tasks(project_root: Path, model, cap):from mediapipe.tasks.python import BaseOptionsfrom mediapipe.tasks.python.vision import FaceLandmarker, FaceLandmarkerOptionsfrom mediapipe.tasks.python.vision.core import vision_task_running_mode as running_modefrom mediapipe.tasks.python.vision.core import image as image_libmodel_path = project_root / "weights" / "face_landmarker.task"if not model_path.is_file():print("Downloading face_landmarker.task ...")url = "https://storage.googleapis.com/mediapipe-models/face_landmarker/face_landmarker/float16/1/face_landmarker.task"urllib.request.urlretrieve(url, model_path)print("Done.")options = FaceLandmarkerOptions(base_options=BaseOptions(model_asset_path=str(model_path)),running_mode=running_mode.VisionTaskRunningMode.IMAGE,num_faces=1,)face_landmarker = FaceLandmarker.create_from_options(options)ImageFormat = image_lib.ImageFormatwhile True:ret, frame = cap.read()if not ret:breakleft_label, left_conf = "—", 0.0right_label, right_conf = "—", 0.0rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)rgb_contiguous = np.ascontiguousarray(rgb)mp_image = image_lib.Image(ImageFormat.SRGB, rgb_contiguous)result = face_landmarker.detect(mp_image)if result.face_landmarks:lm = result.face_landmarks[0]for roi, side in [(get_eye_roi(frame, lm, LEFT_EYE_INDICES_NEW), "left"),(get_eye_roi(frame, lm, RIGHT_EYE_INDICES_NEW), "right"),]:if roi is not None and roi.size > 0:try:pred = model.predict(roi, imgsz=224, device="cpu", verbose=False)if pred:r = pred[0]label = model.names[int(r.probs.top1)]conf = float(r.probs.top1conf)if side == "left":left_label, left_conf = label, confelse:right_label, right_conf = label, confexcept Exception:passcv2.putText(frame, f"L: {left_label} ({left_conf:.0%})", (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)cv2.putText(frame, f"R: {right_label} ({right_conf:.0%})", (20, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)cv2.imshow("Eye open/closed (q to quit)", frame)if cv2.waitKey(1) & 0xFF == ord("q"):breakdef main():project_root = Path(__file__).resolve().parent.parentweights = find_weights(project_root)if weights is None:print("Weights not found. Put best.pt in weights/ or runs/.../weights/ (from model team).")returnif mp is None:print("MediaPipe required. Install: pip install mediapipe")returnmodel = YOLO(str(weights))cap = cv2.VideoCapture(0)if not cap.isOpened():print("Could not open webcam.")returnprint("Live eye open/closed on your face. Press 'q' to quit.")try:if _mp_has_solutions:_run_with_solutions(mp, model, cap)else:_run_with_tasks(project_root, model, cap)finally:cap.release()cv2.destroyAllWindows()if __name__ == "__main__":main()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184"""Live webcam: detect face, crop each eye, run open/closed classifier, show on screen.Requires: opencv-python, ultralytics, mediapipe (pip install mediapipe).Press 'q' to quit."""import urllib.requestfrom pathlib import Pathimport cv2import numpy as npfrom ultralytics import YOLOtry:import mediapipe as mp_mp_has_solutions = hasattr(mp, "solutions")except ImportError:mp = None_mp_has_solutions = False# New MediaPipe Tasks API (Face Landmarker) eye indicesLEFT_EYE_INDICES_NEW = [263, 249, 390, 373, 374, 380, 381, 382, 362, 466, 388, 387, 386, 385, 384, 398]RIGHT_EYE_INDICES_NEW = [33, 7, 163, 144, 145, 153, 154, 155, 133, 246, 161, 160, 159, 158, 157, 173]# Old Face Mesh (solutions) indicesLEFT_EYE_INDICES_OLD = [33, 160, 158, 133, 153, 144]RIGHT_EYE_INDICES_OLD = [362, 385, 387, 263, 373, 380]EYE_PADDING = 0.35def find_weights(project_root: Path) -> Path | None:candidates = [project_root / "weights" / "best.pt",project_root / "runs" / "classify" / "runs_cls" / "eye_open_closed_cpu" / "weights" / "best.pt",project_root / "runs" / "classify" / "runs_cls" / "eye_open_closed_cpu" / "weights" / "last.pt",]return next((p for p in candidates if p.is_file()), None)def get_eye_roi(frame: np.ndarray, landmarks, indices: list[int]) -> np.ndarray | None:h, w = frame.shape[:2]pts = np.array([(int(landmarks[i].x * w), int(landmarks[i].y * h)) for i in indices])x_min, y_min = pts.min(axis=0)x_max, y_max = pts.max(axis=0)dx = max(int((x_max - x_min) * EYE_PADDING), 8)dy = max(int((y_max - y_min) * EYE_PADDING), 8)x0 = max(0, x_min - dx)y0 = max(0, y_min - dy)x1 = min(w, x_max + dx)y1 = min(h, y_max + dy)if x1 <= x0 or y1 <= y0:return Nonereturn frame[y0:y1, x0:x1].copy()def _run_with_solutions(mp, model, cap):face_mesh = mp.solutions.face_mesh.FaceMesh(static_image_mode=False,max_num_faces=1,refine_landmarks=True,min_detection_confidence=0.5,min_tracking_confidence=0.5,)while True:ret, frame = cap.read()if not ret:breakrgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)results = face_mesh.process(rgb)left_label, left_conf = "—", 0.0right_label, right_conf = "—", 0.0if results.multi_face_landmarks:lm = results.multi_face_landmarks[0].landmarkfor roi, indices, side in [(get_eye_roi(frame, lm, LEFT_EYE_INDICES_OLD), LEFT_EYE_INDICES_OLD, "left"),(get_eye_roi(frame, lm, RIGHT_EYE_INDICES_OLD), RIGHT_EYE_INDICES_OLD, "right"),]:if roi is not None and roi.size > 0:try:pred = model.predict(roi, imgsz=224, device="cpu", verbose=False)if pred:r = pred[0]label = model.names[int(r.probs.top1)]conf = float(r.probs.top1conf)if side == "left":left_label, left_conf = label, confelse:right_label, right_conf = label, confexcept Exception:passcv2.putText(frame, f"L: {left_label} ({left_conf:.0%})", (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)cv2.putText(frame, f"R: {right_label} ({right_conf:.0%})", (20, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)cv2.imshow("Eye open/closed (q to quit)", frame)if cv2.waitKey(1) & 0xFF == ord("q"):breakdef _run_with_tasks(project_root: Path, model, cap):from mediapipe.tasks.python import BaseOptionsfrom mediapipe.tasks.python.vision import FaceLandmarker, FaceLandmarkerOptionsfrom mediapipe.tasks.python.vision.core import vision_task_running_mode as running_modefrom mediapipe.tasks.python.vision.core import image as image_libmodel_path = project_root / "weights" / "face_landmarker.task"if not model_path.is_file():print("Downloading face_landmarker.task ...")url = "https://storage.googleapis.com/mediapipe-models/face_landmarker/face_landmarker/float16/1/face_landmarker.task"urllib.request.urlretrieve(url, model_path)print("Done.")options = FaceLandmarkerOptions(base_options=BaseOptions(model_asset_path=str(model_path)),running_mode=running_mode.VisionTaskRunningMode.IMAGE,num_faces=1,)face_landmarker = FaceLandmarker.create_from_options(options)ImageFormat = image_lib.ImageFormatwhile True:ret, frame = cap.read()if not ret:breakleft_label, left_conf = "—", 0.0right_label, right_conf = "—", 0.0rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)rgb_contiguous = np.ascontiguousarray(rgb)mp_image = image_lib.Image(ImageFormat.SRGB, rgb_contiguous)result = face_landmarker.detect(mp_image)if result.face_landmarks:lm = result.face_landmarks[0]for roi, side in [(get_eye_roi(frame, lm, LEFT_EYE_INDICES_NEW), "left"),(get_eye_roi(frame, lm, RIGHT_EYE_INDICES_NEW), "right"),]:if roi is not None and roi.size > 0:try:pred = model.predict(roi, imgsz=224, device="cpu", verbose=False)if pred:r = pred[0]label = model.names[int(r.probs.top1)]conf = float(r.probs.top1conf)if side == "left":left_label, left_conf = label, confelse:right_label, right_conf = label, confexcept Exception:passcv2.putText(frame, f"L: {left_label} ({left_conf:.0%})", (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)cv2.putText(frame, f"R: {right_label} ({right_conf:.0%})", (20, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)cv2.imshow("Eye open/closed (q to quit)", frame)if cv2.waitKey(1) & 0xFF == ord("q"):breakdef main():project_root = Path(__file__).resolve().parent.parentweights = find_weights(project_root)if weights is None:print("Weights not found. Put best.pt in weights/ or runs/.../weights/ (from model team).")returnif mp is None:print("MediaPipe required. Install: pip install mediapipe")returnmodel = YOLO(str(weights))cap = cv2.VideoCapture(0)if not cap.isOpened():print("Could not open webcam.")returnprint("Live eye open/closed on your face. Press 'q' to quit.")try:if _mp_has_solutions:_run_with_solutions(mp, model, cap)else:_run_with_tasks(project_root, model, cap)finally:cap.release()cv2.destroyAllWindows()if __name__ == "__main__":main()
11
1234548 unmodified lines54555657585960616263646566676869from __future__ import annotationsfrom abc import ABC, abstractmethodimport numpy as np48 unmodified linesreturn sum(scores) / len(scores) if scores else 1.0def 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")raise12345648 unmodified lines555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169from __future__ import annotationsimport osfrom abc import ABC, abstractmethodimport numpy as np48 unmodified linesreturn sum(scores) / len(scores) if scores else 1.0class EyeCNNClassifier(EyeClassifier):"""Loader for the custom PyTorch EyeCNN (trained on Kaggle eye crops)."""def __init__(self, checkpoint_path: str, device: str = "cpu"):import torchimport torch.nn as nnclass EyeCNN(nn.Module):def __init__(self, num_classes=2, dropout_rate=0.3):super().__init__()self.conv_layers = nn.Sequential(nn.Conv2d(3, 32, 3, 1, 1), nn.BatchNorm2d(32), nn.ReLU(), nn.MaxPool2d(2, 2),nn.Conv2d(32, 64, 3, 1, 1), nn.BatchNorm2d(64), nn.ReLU(), nn.MaxPool2d(2, 2),nn.Conv2d(64, 128, 3, 1, 1), nn.BatchNorm2d(128), nn.ReLU(), nn.MaxPool2d(2, 2),nn.Conv2d(128, 256, 3, 1, 1), nn.BatchNorm2d(256), nn.ReLU(), nn.MaxPool2d(2, 2),)self.fc_layers = nn.Sequential(nn.AdaptiveAvgPool2d((1, 1)), nn.Flatten(),nn.Linear(256, 512), nn.ReLU(), nn.Dropout(dropout_rate),nn.Linear(512, num_classes),)def forward(self, x):return self.fc_layers(self.conv_layers(x))self._device = torch.device(device)checkpoint = torch.load(checkpoint_path, map_location=self._device, weights_only=False)dropout_rate = checkpoint.get("config", {}).get("dropout_rate", 0.35)self._model = EyeCNN(num_classes=2, dropout_rate=dropout_rate)self._model.load_state_dict(checkpoint["model_state_dict"])self._model.to(self._device)self._model.eval()self._transform = None # built lazilydef _get_transform(self):if self._transform is None:from torchvision import transformsself._transform = transforms.Compose([transforms.ToPILImage(),transforms.Resize((96, 96)),transforms.ToTensor(),transforms.Normalize(mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225],),])return self._transform@propertydef name(self) -> str:return "eye_cnn"def predict_score(self, crops_bgr: list[np.ndarray]) -> float:if not crops_bgr:return 1.0import torchimport cv2transform = self._get_transform()scores = []for crop in crops_bgr:if crop is None or crop.size == 0:scores.append(1.0)continuergb = cv2.cvtColor(crop, cv2.COLOR_BGR2RGB)tensor = transform(rgb).unsqueeze(0).to(self._device)with torch.no_grad():output = self._model(tensor)prob = torch.softmax(output, dim=1)[0, 1].item() # prob of "open"scores.append(prob)return sum(scores) / len(scores)_EXT_TO_BACKEND = {".pth": "cnn", ".pt": "yolo"}def load_eye_classifier(path: str | None = None,backend: str = "yolo",device: str = "cpu",) -> EyeClassifier:if backend == "geometric":return GeometricOnlyClassifier()if path is None:print(f"[CLASSIFIER] No model path for backend {backend!r}, falling back to geometric")return GeometricOnlyClassifier()ext = os.path.splitext(path)[1].lower()inferred = _EXT_TO_BACKEND.get(ext)if inferred and inferred != backend:print(f"[CLASSIFIER] File extension {ext!r} implies backend {inferred!r}, "f"overriding requested {backend!r}")backend = inferredprint(f"[CLASSIFIER] backend={backend!r}, path={path!r}")if backend == "cnn":return EyeCNNClassifier(path, device=device)if backend == "yolo":try:return YOLOv11Classifier(path, device=device)except ImportError:print("[CLASSIFIER] ultralytics required for YOLO. pip install ultralytics")raiseraise ValueError(f"Unknown eye backend {backend!r}. Choose from: yolo, cnn, geometric")
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970import cv2import numpy as npfrom models.pretrained.face_mesh.face_mesh import FaceMeshDetectorLEFT_EYE_CONTOUR = FaceMeshDetector.LEFT_EYE_INDICESRIGHT_EYE_CONTOUR = FaceMeshDetector.RIGHT_EYE_INDICESIMAGENET_MEAN = (0.485, 0.456, 0.406)IMAGENET_STD = (0.229, 0.224, 0.225)CROP_SIZE = 96def _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_wpy = pts[:, 1] * frame_hx_min, x_max = px.min(), px.max()y_min, y_max = py.min(), py.max()w = x_max - x_minh = y_max - y_mincx = (x_min + x_max) / 2cy = (y_min + y_max) / 2size = max(w, h) * (1 + expand)half = size / 2x1 = 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, y2def 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_bboxdef crop_to_tensor(crop_bgr: np.ndarray):import torchrgb = cv2.cvtColor(crop_bgr, cv2.COLOR_BGR2RGB).astype(np.float32) / 255.0for c in range(3):rgb[:, :, c] = (rgb[:, :, c] - IMAGENET_MEAN[c]) / IMAGENET_STD[c]return torch.from_numpy(rgb.transpose(2, 0, 1))12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970import cv2import numpy as npfrom models.pretrained.face_mesh.face_mesh import FaceMeshDetectorLEFT_EYE_CONTOUR = FaceMeshDetector.LEFT_EYE_INDICESRIGHT_EYE_CONTOUR = FaceMeshDetector.RIGHT_EYE_INDICESIMAGENET_MEAN = (0.485, 0.456, 0.406)IMAGENET_STD = (0.229, 0.224, 0.225)CROP_SIZE = 96def _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_wpy = pts[:, 1] * frame_hx_min, x_max = px.min(), px.max()y_min, y_max = py.min(), py.max()w = x_max - x_minh = y_max - y_mincx = (x_min + x_max) / 2cy = (y_min + y_max) / 2size = max(w, h) * (1 + expand)half = size / 2x1 = 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, y2def 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_bboxdef crop_to_tensor(crop_bgr: np.ndarray):import torchrgb = cv2.cvtColor(crop_bgr, cv2.COLOR_BGR2RGB).astype(np.float32) / 255.0for c in range(3):rgb[:, :, c] = (rgb[:, :, c] - IMAGENET_MEAN[c]) / IMAGENET_STD[c]return torch.from_numpy(rgb.transpose(2, 0, 1))
1234567891011121314151617181920# ui- **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 FOCUSEDFrom repo root:```bashpython ui/live_demo.py```MLP only (no head/eye fusion, just your trained MLP):```bashpython ui/live_demo.py --mlp```With YOLO eye model: `python ui/live_demo.py --eye-model path/to/yolo.pt``q` quit, `m` cycle mesh (full / contours / off).1234567891011121314151617181920# ui- **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 FOCUSEDFrom repo root:```bashpython ui/live_demo.py```MLP only (no head/eye fusion, just your trained MLP):```bashpython ui/live_demo.py --mlp```With YOLO eye model: `python ui/live_demo.py --eye-model path/to/yolo.pt``q` quit, `m` cycle mesh (full / contours / off).
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224import argparseimport osimport sysimport timeimport cv2import numpy as npfrom mediapipe.tasks.python.vision import FaceLandmarksConnections_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 ui.pipeline import FaceMeshPipeline, MLPPipelinefrom models.pretrained.face_mesh.face_mesh import FaceMeshDetectorFONT = cv2.FONT_HERSHEY_SIMPLEXCYAN = (255, 255, 0)GREEN = (0, 255, 0)MAGENTA = (255, 0, 255)ORANGE = (0, 165, 255)RED = (0, 0, 255)WHITE = (255, 255, 255)YELLOW = (0, 255, 255)LIGHT_GREEN = (144, 238, 144)_TESSELATION = [(c.start, c.end) for c in FaceLandmarksConnections.FACE_LANDMARKS_TESSELATION]_CONTOURS = [(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]MESH_FULL = 0MESH_CONTOURS = 1MESH_OFF = 2_MESH_NAMES = ["FULL MESH", "CONTOURS", "MESH OFF"]def _lm_to_px(landmarks, idx, w, h):return (int(landmarks[idx, 0] * w), int(landmarks[idx, 1] * h))def draw_tessellation(frame, landmarks, w, h):overlay = frame.copy()for conn in _TESSELATION:pt1 = _lm_to_px(landmarks, conn[0], w, h)pt2 = _lm_to_px(landmarks, conn[1], w, h)cv2.line(overlay, pt1, pt2, (200, 200, 200), 1, cv2.LINE_AA)cv2.addWeighted(overlay, 0.3, frame, 0.7, 0, frame)def draw_contours(frame, landmarks, w, h):for conn in _CONTOURS:pt1 = _lm_to_px(landmarks, conn[0], w, h)pt2 = _lm_to_px(landmarks, conn[1], w, h)cv2.line(frame, pt1, pt2, CYAN, 1, cv2.LINE_AA)for indices in [_LEFT_EYEBROW, _RIGHT_EYEBROW]:for i in range(len(indices) - 1):pt1 = _lm_to_px(landmarks, indices[i], w, h)pt2 = _lm_to_px(landmarks, indices[i + 1], w, h)cv2.line(frame, pt1, pt2, LIGHT_GREEN, 2, cv2.LINE_AA)for i in range(len(_NOSE_BRIDGE) - 1):pt1 = _lm_to_px(landmarks, _NOSE_BRIDGE[i], w, h)pt2 = _lm_to_px(landmarks, _NOSE_BRIDGE[i + 1], w, h)cv2.line(frame, pt1, pt2, ORANGE, 1, cv2.LINE_AA)for i in range(len(_LIPS_OUTER) - 1):pt1 = _lm_to_px(landmarks, _LIPS_OUTER[i], w, h)pt2 = _lm_to_px(landmarks, _LIPS_OUTER[i + 1], w, h)cv2.line(frame, pt1, pt2, MAGENTA, 1, cv2.LINE_AA)for i in range(len(_LIPS_INNER) - 1):pt1 = _lm_to_px(landmarks, _LIPS_INNER[i], w, h)pt2 = _lm_to_px(landmarks, _LIPS_INNER[i + 1], w, h)cv2.line(frame, pt1, pt2, (200, 0, 200), 1, cv2.LINE_AA)def draw_eyes_and_irises(frame, landmarks, w, h):left_pts = np.array([_lm_to_px(landmarks, 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_to_px(landmarks, i, w, h) for i in FaceMeshDetector.RIGHT_EYE_INDICES],dtype=np.int32,)cv2.polylines(frame, [right_pts], True, GREEN, 2, cv2.LINE_AA)for indices in [_LEFT_EAR_POINTS, _RIGHT_EAR_POINTS]:for idx in indices:pt = _lm_to_px(landmarks, idx, w, h)cv2.circle(frame, pt, 3, YELLOW, -1, cv2.LINE_AA)for iris_indices, eye_inner, eye_outer in [(FaceMeshDetector.LEFT_IRIS_INDICES, 133, 33),(FaceMeshDetector.RIGHT_IRIS_INDICES, 362, 263),]:iris_pts = np.array([_lm_to_px(landmarks, i, w, h) for i in iris_indices],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_center_x = (landmarks[eye_inner, 0] + landmarks[eye_outer, 0]) / 2.0eye_center_y = (landmarks[eye_inner, 1] + landmarks[eye_outer, 1]) / 2.0eye_center = (int(eye_center_x * w), int(eye_center_y * h))dx = center[0] - eye_center[0]dy = center[1] - eye_center[1]gaze_end = (int(center[0] + dx * 3), int(center[1] + dy * 3))cv2.line(frame, tuple(center), gaze_end, RED, 1, cv2.LINE_AA)def main():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.mlpif 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,)cap = cv2.VideoCapture(args.camera)if not cap.isOpened():print("[DEMO] ERROR: Cannot open camera")returnprint("[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.0mesh_mode = MESH_FULLtry:while True:ret, frame = cap.read()if not ret:breakresult = pipeline.process_frame(frame)now = time.time()fps = 0.9 * fps + 0.1 * (1.0 / max(now - prev_time, 1e-6))prev_time = nowh, w = frame.shape[:2]if result["landmarks"] is not None:lm = result["landmarks"]if mesh_mode == MESH_FULL:draw_tessellation(frame, lm, w, h)draw_contours(frame, lm, w, h)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)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 = "FOCUSED" if result["is_focused"] else "NOT FOCUSED"status_color = GREEN if result["is_focused"] else REDcv2.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)cv2.imshow("FocusGuard", frame)key = cv2.waitKey(1) & 0xFFif key == ord("q"):breakelif key == ord("m"):mesh_mode = (mesh_mode + 1) % 3print(f"[DEMO] Mesh: {_MESH_NAMES[mesh_mode]}")finally:cap.release()cv2.destroyAllWindows()pipeline.close()print("[DEMO] Done")if __name__ == "__main__":main()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224import argparseimport osimport sysimport timeimport cv2import numpy as npfrom mediapipe.tasks.python.vision import FaceLandmarksConnections_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 ui.pipeline import FaceMeshPipeline, MLPPipelinefrom models.pretrained.face_mesh.face_mesh import FaceMeshDetectorFONT = cv2.FONT_HERSHEY_SIMPLEXCYAN = (255, 255, 0)GREEN = (0, 255, 0)MAGENTA = (255, 0, 255)ORANGE = (0, 165, 255)RED = (0, 0, 255)WHITE = (255, 255, 255)YELLOW = (0, 255, 255)LIGHT_GREEN = (144, 238, 144)_TESSELATION = [(c.start, c.end) for c in FaceLandmarksConnections.FACE_LANDMARKS_TESSELATION]_CONTOURS = [(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]MESH_FULL = 0MESH_CONTOURS = 1MESH_OFF = 2_MESH_NAMES = ["FULL MESH", "CONTOURS", "MESH OFF"]def _lm_to_px(landmarks, idx, w, h):return (int(landmarks[idx, 0] * w), int(landmarks[idx, 1] * h))def draw_tessellation(frame, landmarks, w, h):overlay = frame.copy()for conn in _TESSELATION:pt1 = _lm_to_px(landmarks, conn[0], w, h)pt2 = _lm_to_px(landmarks, conn[1], w, h)cv2.line(overlay, pt1, pt2, (200, 200, 200), 1, cv2.LINE_AA)cv2.addWeighted(overlay, 0.3, frame, 0.7, 0, frame)def draw_contours(frame, landmarks, w, h):for conn in _CONTOURS:pt1 = _lm_to_px(landmarks, conn[0], w, h)pt2 = _lm_to_px(landmarks, conn[1], w, h)cv2.line(frame, pt1, pt2, CYAN, 1, cv2.LINE_AA)for indices in [_LEFT_EYEBROW, _RIGHT_EYEBROW]:for i in range(len(indices) - 1):pt1 = _lm_to_px(landmarks, indices[i], w, h)pt2 = _lm_to_px(landmarks, indices[i + 1], w, h)cv2.line(frame, pt1, pt2, LIGHT_GREEN, 2, cv2.LINE_AA)for i in range(len(_NOSE_BRIDGE) - 1):pt1 = _lm_to_px(landmarks, _NOSE_BRIDGE[i], w, h)pt2 = _lm_to_px(landmarks, _NOSE_BRIDGE[i + 1], w, h)cv2.line(frame, pt1, pt2, ORANGE, 1, cv2.LINE_AA)for i in range(len(_LIPS_OUTER) - 1):pt1 = _lm_to_px(landmarks, _LIPS_OUTER[i], w, h)pt2 = _lm_to_px(landmarks, _LIPS_OUTER[i + 1], w, h)cv2.line(frame, pt1, pt2, MAGENTA, 1, cv2.LINE_AA)for i in range(len(_LIPS_INNER) - 1):pt1 = _lm_to_px(landmarks, _LIPS_INNER[i], w, h)pt2 = _lm_to_px(landmarks, _LIPS_INNER[i + 1], w, h)cv2.line(frame, pt1, pt2, (200, 0, 200), 1, cv2.LINE_AA)def draw_eyes_and_irises(frame, landmarks, w, h):left_pts = np.array([_lm_to_px(landmarks, 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_to_px(landmarks, i, w, h) for i in FaceMeshDetector.RIGHT_EYE_INDICES],dtype=np.int32,)cv2.polylines(frame, [right_pts], True, GREEN, 2, cv2.LINE_AA)for indices in [_LEFT_EAR_POINTS, _RIGHT_EAR_POINTS]:for idx in indices:pt = _lm_to_px(landmarks, idx, w, h)cv2.circle(frame, pt, 3, YELLOW, -1, cv2.LINE_AA)for iris_indices, eye_inner, eye_outer in [(FaceMeshDetector.LEFT_IRIS_INDICES, 133, 33),(FaceMeshDetector.RIGHT_IRIS_INDICES, 362, 263),]:iris_pts = np.array([_lm_to_px(landmarks, i, w, h) for i in iris_indices],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_center_x = (landmarks[eye_inner, 0] + landmarks[eye_outer, 0]) / 2.0eye_center_y = (landmarks[eye_inner, 1] + landmarks[eye_outer, 1]) / 2.0eye_center = (int(eye_center_x * w), int(eye_center_y * h))dx = center[0] - eye_center[0]dy = center[1] - eye_center[1]gaze_end = (int(center[0] + dx * 3), int(center[1] + dy * 3))cv2.line(frame, tuple(center), gaze_end, RED, 1, cv2.LINE_AA)def main():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", "cnn"])parser.add_argument("--eye-blend", type=float, default=0.5)args = parser.parse_args()use_mlp_only = args.mlpif 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,)cap = cv2.VideoCapture(args.camera)if not cap.isOpened():print("[DEMO] ERROR: Cannot open camera")returnprint("[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.0mesh_mode = MESH_FULLtry:while True:ret, frame = cap.read()if not ret:breakresult = pipeline.process_frame(frame)now = time.time()fps = 0.9 * fps + 0.1 * (1.0 / max(now - prev_time, 1e-6))prev_time = nowh, w = frame.shape[:2]if result["landmarks"] is not None:lm = result["landmarks"]if mesh_mode == MESH_FULL:draw_tessellation(frame, lm, w, h)draw_contours(frame, lm, w, h)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)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 = "FOCUSED" if result["is_focused"] else "NOT FOCUSED"status_color = GREEN if result["is_focused"] else REDcv2.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)cv2.imshow("FocusGuard", frame)key = cv2.waitKey(1) & 0xFFif key == ord("q"):breakelif key == ord("m"):mesh_mode = (mesh_mode + 1) % 3print(f"[DEMO] Mesh: {_MESH_NAMES[mesh_mode]}")finally:cap.release()cv2.destroyAllWindows()pipeline.close()print("[DEMO] Done")if __name__ == "__main__":main()
35 unmodified lines36373839404135 unmodified linesself.threshold = thresholdself.eye_blend = eye_blendself.eye_classifier = load_eye_classifier(path=eye_model_path if eye_model_path and os.path.exists(eye_model_path) else None,backend=eye_backend,35 unmodified lines36373839404142434435 unmodified linesself.threshold = thresholdself.eye_blend = eye_blendif eye_model_path and not os.path.exists(eye_model_path):print(f"[PIPELINE] WARNING: eye model file not found: {eye_model_path}")self.eye_classifier = load_eye_classifier(path=eye_model_path if eye_model_path and os.path.exists(eye_model_path) else None,backend=eye_backend,
models/cnn/CNN_MODEL/.claude/settings.local.json+7-7
models/cnn/CNN_MODEL/.gitattributes+1-1
models/cnn/CNN_MODEL/.gitignore+4-4
models/cnn/CNN_MODEL/README.md+74-74
models/cnn/CNN_MODEL/notebooks/eye_classifier_colab.ipynb
models/cnn/CNN_MODEL/scripts/focus_infer.py+199-199
models/cnn/CNN_MODEL/scripts/predict_image.py+49-49
models/cnn/CNN_MODEL/scripts/video_infer.py+281-281
models/cnn/CNN_MODEL/scripts/webcam_live.py+184-184
models/cnn/eye_attention/__init__.py+1-1
models/cnn/eye_attention/classifier.py+106-6
models/cnn/eye_attention/crop.py+70-70
ui/README.md+20-20
ui/live_demo.py+224-224
ui/pipeline.py+3