Download calibration/face_check.py from InsightUX/capstone_testing_folder: direct link, hf CLI and curl.
- Browser
- Download file 7.12 kB
-
https://huggingface.co/InsightUX/capstone_testing_folder/resolve/main/calibration/face_check.py
- Command line
-
hf download hf://InsightUX/capstone_testing_folder/calibration/face_check.py
-
curl -L -o face_check.py https://huggingface.co/InsightUX/capstone_testing_folder/resolve/main/calibration/face_check.py
7.12 kB
| """ | |
| Shared camera / face-position check for InsightUX. | |
| One implementation used before calibration, before validation and before a | |
| live tracking session, so the user always gets the same lighting / alignment / | |
| head-pose guidance. calibrate.py also reuses get_full_feedback() per frame | |
| while a calibration dot is being collected. | |
| """ | |
| import time | |
| import cv2 | |
| from core.camera import read_camera_frame, CameraAccessError | |
| import numpy as np | |
| from preprocessing.preprocessing_pipeline import ( | |
| estimate_camera_matrix, | |
| estimate_head_pose, | |
| ) | |
| LIGHT_MIN_BRIGHTNESS = 60 | |
| LIGHT_MAX_BRIGHTNESS = 200 | |
| def check_lighting(frame): | |
| gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
| brightness = np.mean(gray) | |
| if brightness < LIGHT_MIN_BRIGHTNESS: | |
| return False, "Too dark - increase lighting" | |
| elif brightness > LIGHT_MAX_BRIGHTNESS: | |
| return False, "Too bright - reduce lighting" | |
| return True, None | |
| def check_face_center(lms, frame_shape): | |
| H, W = frame_shape[:2] | |
| xs = [lm.x * W for lm in lms] | |
| ys = [lm.y * H for lm in lms] | |
| cx = np.mean(xs) | |
| cy = np.mean(ys) | |
| if cx < W * 0.35: | |
| return False, "Move face RIGHT in frame" | |
| elif cx > W * 0.65: | |
| return False, "Move face LEFT in frame" | |
| if cy < H * 0.35: | |
| return False, "Sit closer or lower your camera" | |
| elif cy > H * 0.65: | |
| return False, "Sit further or raise your camera" | |
| return True, None | |
| def get_full_feedback(frame, lms, frame_shape, yaw, roll): | |
| msgs = [] | |
| ok = True | |
| H, W = frame_shape[:2] | |
| nose_y = lms[1].y * H | |
| forehead_y = lms[10].y * H | |
| chin_y = lms[152].y * H | |
| midpoint_y = (forehead_y + chin_y) / 2.0 | |
| face_h = chin_y - forehead_y | |
| if face_h > 1: | |
| pitch_ratio = (nose_y - midpoint_y) / face_h | |
| if pitch_ratio < -0.10: | |
| msgs.append("Lift your head UP") | |
| ok = False | |
| elif pitch_ratio > 0.15: | |
| msgs.append("Tilt your head DOWN slightly") | |
| ok = False | |
| if yaw < -15: | |
| msgs.append("Turn face slightly RIGHT") | |
| ok = False | |
| elif yaw > 15: | |
| msgs.append("Turn face slightly LEFT") | |
| ok = False | |
| if roll < -10: | |
| msgs.append("Tilt head slightly RIGHT") | |
| ok = False | |
| elif roll > 10: | |
| msgs.append("Tilt head slightly LEFT") | |
| ok = False | |
| light_ok, light_msg = check_lighting(frame) | |
| if not light_ok: | |
| msgs.append(light_msg) | |
| ok = False | |
| center_ok, center_msg = check_face_center(lms, frame_shape) | |
| if not center_ok: | |
| msgs.append(center_msg) | |
| ok = False | |
| if ok: | |
| msgs.append("Perfect! Hold still...") | |
| return ok, msgs | |
| # ============================================================================= | |
| # FACE ORIENTATION GATE | |
| # ============================================================================= | |
| def face_orientation_gate(face_mesh, cap, cam_matrix_ref, purpose="calibration", | |
| stop_event=None): | |
| """Blocking camera-check window. Returns True once the user has held a good | |
| pose for HOLD_SECONDS (or pressed Esc to skip), False if stop_event was set | |
| first (e.g. the user stopped the session while the check was open). | |
| purpose is only used for wording: "calibration", "validation", "session".""" | |
| print(f"\nChecking face setup before {purpose}...") | |
| print("Position your face straight, centred, at normal laptop distance.") | |
| HOLD_SECONDS = 2.0 | |
| good_since = None | |
| cam_matrix_loc = cam_matrix_ref[0] | |
| cv2.namedWindow("Face Check", cv2.WINDOW_NORMAL) | |
| cv2.resizeWindow("Face Check", 640, 420) | |
| while True: | |
| if stop_event is not None and stop_event.is_set(): | |
| cv2.destroyWindow("Face Check") | |
| cv2.waitKey(1) | |
| return False | |
| try: | |
| ret, frame = read_camera_frame(cap) | |
| except CameraAccessError: | |
| cv2.destroyWindow("Face Check") | |
| cv2.waitKey(1) | |
| raise | |
| if not ret: | |
| continue | |
| if cam_matrix_loc is None: | |
| cam_matrix_loc = estimate_camera_matrix(frame.shape) | |
| cam_matrix_ref[0] = cam_matrix_loc | |
| rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) | |
| results = face_mesh.process(rgb) | |
| display = frame.copy() | |
| H, W = display.shape[:2] | |
| if not results.multi_face_landmarks: | |
| good_since = None | |
| cv2.putText(display, "No face detected - look at camera", | |
| (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) | |
| else: | |
| lms = results.multi_face_landmarks[0].landmark | |
| head_pose = estimate_head_pose(lms, frame.shape, cam_matrix_loc) | |
| if head_pose is None: | |
| good_since = None | |
| cv2.putText(display, "Pose failed - move slightly", | |
| (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) | |
| else: | |
| is_good, msgs = get_full_feedback( | |
| frame, lms, frame.shape, head_pose.yaw, head_pose.roll | |
| ) | |
| if is_good: | |
| if good_since is None: | |
| good_since = time.time() | |
| elapsed = time.time() - good_since | |
| remaining = max(0, HOLD_SECONDS - elapsed) | |
| bar_w = int((elapsed / HOLD_SECONDS) * (W - 40)) | |
| bar_w = min(bar_w, W - 40) | |
| cv2.rectangle(display, (20, H-50), (W-20, H-25), (40, 40, 40), -1) | |
| cv2.rectangle(display, (20, H-50), (20+bar_w, H-25), (0, 220, 0), -1) | |
| cv2.putText(display, f"Hold still... {remaining:.1f}s", | |
| (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2) | |
| if elapsed >= HOLD_SECONDS: | |
| cv2.putText(display, f"Starting {purpose}!", | |
| (20, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2) | |
| cv2.imshow("Face Check", display) | |
| cv2.waitKey(800) | |
| cv2.destroyWindow("Face Check") | |
| cv2.waitKey(1) | |
| return True | |
| else: | |
| good_since = None | |
| for i, msg in enumerate(msgs): | |
| cv2.putText(display, msg, (20, 40 + i * 35), | |
| cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 165, 255), 2) | |
| cv2.putText(display, | |
| f"Yaw:{head_pose.yaw:+.1f} Roll:{head_pose.roll:+.1f}", | |
| (20, H - 65), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (160, 160, 160), 1) | |
| cv2.imshow("Face Check", display) | |
| if cv2.waitKey(1) & 0xFF == 27: | |
| cv2.destroyWindow("Face Check") | |
| cv2.waitKey(1) | |
| return True |