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

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