Spaces:
Sleeping
Sleeping
Saba Al-Gafri commited on
Commit ·
29b7d67
1
Parent(s): b720052
Replace CNN_MODEL submodule with actual files
Browse filesFiles changed (11)
- models/cnn/CNN_MODELDeleted-1
- models/cnn/CNN_MODEL/.claude/settings.local.jsonAdded+7
- models/cnn/CNN_MODEL/.gitattributesAdded+1
- models/cnn/CNN_MODEL/.gitignoreAdded+4
- models/cnn/CNN_MODEL/README.mdAdded+74
- models/cnn/CNN_MODEL/notebooks/eye_classifier_colab.ipynbAdded
- models/cnn/CNN_MODEL/scripts/focus_infer.pyAdded+199
- models/cnn/CNN_MODEL/scripts/predict_image.pyAdded+49
- models/cnn/CNN_MODEL/scripts/video_infer.pyAdded+281
- models/cnn/CNN_MODEL/scripts/webcam_live.pyAdded+184
- models/cnn/CNN_MODEL/weights/yolo11s-cls.ptAdded+3
1Subproject commit 330d93a3bc5c419c55e7ee18c6e47e2320030b471234567{ "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 -text1234Dataset/train/Dataset/val/Dataset/test/.DS_Store1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374# 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 annotations
from pathlib import Pathimport os
import cv2import numpy as npfrom ultralytics import YOLO
def 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 None contours = 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 None
def 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.shape clahe = 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/edges cx, cy = w // 2, h // 2 rx, 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 None
best = None best_score = -1.0 for c in contours: area = cv2.contourArea(c) if area < 15: continue perimeter = cv2.arcLength(c, True) if perimeter <= 0: continue circularity = 4 * np.pi * (area / (perimeter * perimeter)) if circularity < 0.3: continue m = cv2.moments(c) if m["m00"] == 0: continue px = int(m["m10"] / m["m00"]) + x0 py = int(m["m01"] / m["m00"]) + y0
# Score by circularity and distance to center dist = np.hypot(px - cx, py - cy) / max(w, h) score = circularity - dist if score > best_score: best_score = score best = (px, py)
return best
def 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_shape cx, cy = w // 2, h // 2 px, py = pupil_center dx = 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 out
def main(): project_root = Path(__file__).resolve().parent.parent data_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_closed test_open = root / "open" test_closed = root / "closed" if test_open.exists() and test_closed.exists(): return test_open, test_closed alt_closed = root / "close" if test_open.exists() and alt_closed.exists(): return test_open, alt_closed return None, None
test_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) return
test_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.names for r in results: probs = r.probs top_idx = int(probs.top1) top_conf = float(probs.top1conf) pred_label = names[top_idx]
img = cv2.imread(r.path) if img is None: continue gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ellipse = detect_eyelid_boundary(gray) pupil_center = detect_pupil_center(gray) focused = False if 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 Path
from ultralytics import YOLO
def main(): project_root = Path(__file__).resolve().parent.parent weight_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.names
for path in sys.argv[1:]: p = Path(path) if not p.is_file(): print(p, "- file not found") continue try: results = model.predict(str(p), imgsz=224, device="cpu", verbose=False) except Exception as e: print(p, "- error:", e) continue if not results: print(p, "- no result") continue r = 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 annotations
import osfrom pathlib import Path
import cv2import numpy as npfrom ultralytics import YOLO
try: import mediapipe as mpexcept Exception: # pragma: no cover mp = None
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_pupil_center(gray: np.ndarray) -> tuple[int, int] | None: h, w = gray.shape clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8)) eq = clahe.apply(gray) blur = cv2.GaussianBlur(eq, (7, 7), 0)
cx, cy = w // 2, h // 2 rx, 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 None
best = None best_score = -1.0 for c in contours: area = cv2.contourArea(c) if area < 15: continue perimeter = cv2.arcLength(c, True) if perimeter <= 0: continue circularity = 4 * np.pi * (area / (perimeter * perimeter)) if circularity < 0.3: continue m = cv2.moments(c) if m["m00"] == 0: continue px = int(m["m10"] / m["m00"]) + x0 py = int(m["m01"] / m["m00"]) + y0
dist = np.hypot(px - cx, py - cy) / max(w, h) score = circularity - dist if score > best_score: best_score = score best = (px, py)
return best
def is_focused(pupil_center: tuple[int, int], img_shape: tuple[int, int]) -> bool: h, w = img_shape cx = w // 2 px, _ = pupil_center dx = abs(px - cx) / max(w, 1) return dx < 0.12
def 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.probs top_idx = int(probs.top1) top_conf = float(probs.top1conf) pred_label = model.names[top_idx] return pred_label, top_conf
def 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 out
def 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}") return
fps = cap.get(cv2.CAP_PROP_FPS) or 30.0 width = 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 = 0 last_label = None seg_start = 0.0 segments: 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 None if use_mp: mp_face_mesh = mp.solutions.face_mesh face_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: break time_sec = frame_idx / fps conf = 0.0 pred_label = "open" focused = False
if 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].landmark h, 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 points p1, p2, p3, p4, p5, p6 = eye_pts v1 = 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 threshold pred_label = "open" if ear_avg > 0.22 else "closed"
# iris centers li = 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 eyes le_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 boundaries cv2.polylines(frame, [le], True, (0, 255, 255), 1) cv2.polylines(frame, [re], True, (0, 255, 255), 1) # draw iris centers cv2.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 = False else: 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 None focused = False if pred_label.lower() == "open" and pupil_center is not None: focused = is_focused(pupil_center, gray.shape)
if pred_label.lower() != "open": focused = False
label = "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 = label seg_start = time_sec elif label != last_label: segments.append((seg_start, time_sec, last_label)) seg_start = time_sec last_label = label
annotated = annotate_frame(frame, label, focused, conf, time_sec) writer.write(annotated) frame_idx += 1
if last_label is not None: end_time = frame_idx / fps segments.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.parent weights = find_weights(project_root) model = YOLO(str(weights)) if weights is not None else None
# Default to 1.mp4 and 2.mp4 in project root videos = [] for name in ["1.mp4", "2.mp4"]: p = project_root / name if p.exists(): videos.append(p)
# Also allow passing paths via env var extra = 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 / vp if vp.exists(): videos.append(vp)
if not videos: print("No videos found. Expected 1.mp4 / 2.mp4 in project root.") return
for 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 Path
import cv2import numpy as npfrom ultralytics import YOLO
try: 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.35
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", ] 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 None return 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: break rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) results = face_mesh.process(rgb) left_label, left_conf = "—", 0.0 right_label, right_conf = "—", 0.0 if results.multi_face_landmarks: lm = results.multi_face_landmarks[0].landmark for 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, conf else: right_label, right_conf = label, conf except Exception: pass cv2.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"): break
def _run_with_tasks(project_root: Path, model, cap): from mediapipe.tasks.python import BaseOptions from mediapipe.tasks.python.vision import FaceLandmarker, FaceLandmarkerOptions from mediapipe.tasks.python.vision.core import vision_task_running_mode as running_mode from mediapipe.tasks.python.vision.core import image as image_lib
model_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.ImageFormat
while True: ret, frame = cap.read() if not ret: break left_label, left_conf = "—", 0.0 right_label, right_conf = "—", 0.0
rgb = 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, conf else: right_label, right_conf = label, conf except Exception: pass
cv2.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"): break
def main(): project_root = Path(__file__).resolve().parent.parent weights = find_weights(project_root) if weights is None: print("Weights not found. Put best.pt in weights/ or runs/.../weights/ (from model team).") return if mp is None: print("MediaPipe required. Install: pip install mediapipe") return
model = YOLO(str(weights)) cap = cv2.VideoCapture(0) if not cap.isOpened(): print("Could not open webcam.") return
print("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()123version https://git-lfs.github.com/spec/v1oid sha256:e2b605d1c8c212b434a75a32759a6f7adf1d2b29c35f76bdccd4c794cb653cf2size 13630112models/cnn/CNN_MODELDeleted-1
models/cnn/CNN_MODEL/.claude/settings.local.jsonAdded+7
models/cnn/CNN_MODEL/.gitattributesAdded+1
models/cnn/CNN_MODEL/.gitignoreAdded+4
models/cnn/CNN_MODEL/README.mdAdded+74
models/cnn/CNN_MODEL/notebooks/eye_classifier_colab.ipynbAdded
models/cnn/CNN_MODEL/scripts/focus_infer.pyAdded+199
models/cnn/CNN_MODEL/scripts/predict_image.pyAdded+49
models/cnn/CNN_MODEL/scripts/video_infer.pyAdded+281
models/cnn/CNN_MODEL/scripts/webcam_live.pyAdded+184
models/cnn/CNN_MODEL/weights/yolo11s-cls.ptAdded+3