fix: accept frontal faces when eye detection is inconclusive

This commit is contained in:
2026-11-10 14:52:36 +01:00
parent 2e21946300
commit 994c78cc26
2 changed files with 42 additions and 5 deletions

View File

@@ -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.

View File

@@ -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);