From 994c78cc2674aed6a6128f66286cbbfc07663bdd Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 10 Nov 2026 14:52:36 +0100 Subject: [PATCH] fix: accept frontal faces when eye detection is inconclusive --- models/README.md | 9 ++++++--- src/faces.rs | 38 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/models/README.md b/models/README.md index 74e701f..1415c9b 100644 --- a/models/README.md +++ b/models/README.md @@ -31,9 +31,12 @@ Each XML includes its upstream license notice. They are embedded in the binary; no model downloads or external model paths are needed at runtime. Detection runs once per capture, with the longest image edge capped at 640 px, -using a frontal-face cascade followed by a roughly level, centered pair of eyes. -Faces below 40 px and faces with closed/hidden eyes may be missed. This is a -conservative side-view filter, not an exact gaze estimator. OpenCV is limited to +using a frontal-face cascade with an optional eye-pair check. When fewer than two +eyes are detected, the frontal-face result is accepted: missing eyes are not +reliable evidence of a side view. When two or more eyes are detected, a roughly +level, centered pair is required. Debug output includes the acceptance reason. +Faces below 40 px may be missed, and some turned faces may still pass the frontal +cascade. This is a best-effort side-view filter, not an exact gaze estimator. OpenCV is limited to two threads. Validate counts and timings with representative group photos on the kiosk before deployment; no accuracy or latency guarantee is implied. diff --git a/src/faces.rs b/src/faces.rs index 3f2b209..946e39d 100644 --- a/src/faces.rs +++ b/src/faces.rs @@ -70,10 +70,12 @@ impl Detector { Size::new((face.width / 10).max(6), (face.height / 10).max(6)), Size::new(face.width / 2, face.height / 3), )?; - let accepted = frontal_eye_pair(&eyes.to_vec(), face.width); + let eye_boxes = eyes.to_vec(); + let reason = acceptance_reason(&eye_boxes, face.width); + let accepted = reason != "unbalanced_eye_pair"; count += usize::from(accepted); eprintln!( - "[capture DEBUG] face={face:?} eyes={} facing_camera={accepted}", + "[capture DEBUG] face={face:?} eyes={} facing_camera={accepted} reason={reason}", eyes.len() ); } @@ -89,6 +91,18 @@ impl Detector { } } +// Missing eyes are inconclusive: glasses, reflections and closed eyes can all +// defeat the eye cascade on an otherwise valid frontal-face detection. +fn acceptance_reason(eyes: &[Rect], width: i32) -> &'static str { + if eyes.len() < 2 { + "frontal_face_eye_check_inconclusive" + } else if frontal_eye_pair(eyes, width) { + "frontal_face_and_eye_pair" + } else { + "unbalanced_eye_pair" + } +} + // Two separated, roughly level eyes on opposite sides of the face. This is a // conservative frontal-view heuristic, not a gaze or head-pose estimator. fn frontal_eye_pair(eyes: &[Rect], width: i32) -> bool { @@ -111,6 +125,26 @@ fn frontal_eye_pair(eyes: &[Rect], width: i32) -> bool { #[cfg(test)] mod tests { use super::*; + #[test] + fn missing_eyes_do_not_discard_frontal_faces() { + assert_eq!( + acceptance_reason(&[], 164), + "frontal_face_eye_check_inconclusive" + ); + assert_eq!( + acceptance_reason(&[Rect::new(20, 22, 16, 12)], 100), + "frontal_face_eye_check_inconclusive" + ); + assert_eq!( + acceptance_reason(&[Rect::new(20, 22, 16, 12), Rect::new(64, 24, 16, 12)], 100), + "frontal_face_and_eye_pair" + ); + assert_eq!( + acceptance_reason(&[Rect::new(20, 22, 16, 12), Rect::new(64, 40, 16, 12)], 100), + "unbalanced_eye_pair" + ); + } + #[test] fn rejects_profile_duplicates_and_unbalanced_eyes() { let eye = |x, y| Rect::new(x, y, 16, 12);