feat: print captured photos per detected frontal face
This commit is contained in:
132
src/faces.rs
Normal file
132
src/faces.rs
Normal file
@@ -0,0 +1,132 @@
|
||||
//! Capture-only CPU detection; no inference or allocations on preview frames.
|
||||
use opencv::{
|
||||
core::{self, Mat, Rect, Size, Vector},
|
||||
imgproc, objdetect,
|
||||
prelude::*,
|
||||
};
|
||||
use std::time::Instant;
|
||||
|
||||
pub struct Detector {
|
||||
faces: objdetect::CascadeClassifier,
|
||||
eyes: objdetect::CascadeClassifier,
|
||||
}
|
||||
|
||||
fn cascade(xml: &str) -> opencv::Result<objdetect::CascadeClassifier> {
|
||||
let storage = core::FileStorage::new(xml, core::FileStorage_MEMORY, "")?;
|
||||
let mut classifier = objdetect::CascadeClassifier::default()?;
|
||||
classifier.read(&storage.get_first_top_level_node()?)?;
|
||||
Ok(classifier)
|
||||
}
|
||||
|
||||
impl Detector {
|
||||
pub fn new() -> opencv::Result<Self> {
|
||||
Ok(Self {
|
||||
faces: cascade(include_str!("../models/haarcascade_frontalface_alt2.xml"))?,
|
||||
eyes: cascade(include_str!(
|
||||
"../models/haarcascade_eye_tree_eyeglasses.xml"
|
||||
))?,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn count(&mut self, frame: &Mat) -> opencv::Result<usize> {
|
||||
let start = Instant::now();
|
||||
let mut small = Mat::default();
|
||||
let scale = (640.0 / frame.cols().max(frame.rows()) as f64).min(1.0);
|
||||
imgproc::resize(
|
||||
frame,
|
||||
&mut small,
|
||||
Size::default(),
|
||||
scale,
|
||||
scale,
|
||||
imgproc::INTER_AREA,
|
||||
)?;
|
||||
let mut gray = Mat::default();
|
||||
imgproc::cvt_color_def(&small, &mut gray, imgproc::COLOR_BGR2GRAY)?;
|
||||
let mut equalized = Mat::default();
|
||||
imgproc::equalize_hist(&gray, &mut equalized)?;
|
||||
let mut faces = Vector::<Rect>::new();
|
||||
self.faces.detect_multi_scale(
|
||||
&equalized,
|
||||
&mut faces,
|
||||
1.15,
|
||||
4,
|
||||
0,
|
||||
Size::new(40, 40),
|
||||
Size::default(),
|
||||
)?;
|
||||
let mut count = 0;
|
||||
for face in faces.iter() {
|
||||
let upper = Mat::roi(
|
||||
&equalized,
|
||||
Rect::new(face.x, face.y, face.width, face.height * 3 / 5),
|
||||
)?;
|
||||
let mut eyes = Vector::<Rect>::new();
|
||||
self.eyes.detect_multi_scale(
|
||||
&upper,
|
||||
&mut eyes,
|
||||
1.1,
|
||||
3,
|
||||
0,
|
||||
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);
|
||||
count += usize::from(accepted);
|
||||
eprintln!(
|
||||
"[capture DEBUG] face={face:?} eyes={} facing_camera={accepted}",
|
||||
eyes.len()
|
||||
);
|
||||
}
|
||||
eprintln!(
|
||||
"[capture DEBUG] candidates={} faces={count} rejected={} copies={count} detection_ms={} analysis={}x{}",
|
||||
faces.len(),
|
||||
faces.len() - count,
|
||||
start.elapsed().as_millis(),
|
||||
equalized.cols(),
|
||||
equalized.rows()
|
||||
);
|
||||
Ok(count)
|
||||
}
|
||||
}
|
||||
|
||||
// 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 {
|
||||
eyes.iter().any(|left| {
|
||||
eyes.iter().any(|right| {
|
||||
let lx = left.x + left.width / 2;
|
||||
let rx = right.x + right.width / 2;
|
||||
let ly = left.y + left.height / 2;
|
||||
let ry = right.y + right.height / 2;
|
||||
lx < width / 2
|
||||
&& rx > width / 2
|
||||
&& rx - lx >= width / 4
|
||||
&& rx - lx <= width * 7 / 10
|
||||
&& (ly - ry).abs() <= width / 8
|
||||
&& (lx + rx - width).abs() <= width / 5
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn rejects_profile_duplicates_and_unbalanced_eyes() {
|
||||
let eye = |x, y| Rect::new(x, y, 16, 12);
|
||||
assert!(frontal_eye_pair(&[eye(20, 22), eye(64, 24)], 100));
|
||||
assert!(!frontal_eye_pair(&[], 100));
|
||||
assert!(!frontal_eye_pair(&[eye(20, 22)], 100));
|
||||
assert!(!frontal_eye_pair(&[eye(20, 22), eye(23, 22)], 100));
|
||||
assert!(!frontal_eye_pair(&[eye(20, 22), eye(64, 40)], 100));
|
||||
assert!(!frontal_eye_pair(&[eye(0, 22), eye(45, 22)], 100));
|
||||
}
|
||||
#[test]
|
||||
fn bundled_cascades_run_on_blank_capture() -> opencv::Result<()> {
|
||||
let mut detector = Detector::new()?;
|
||||
let frame =
|
||||
Mat::new_rows_cols_with_default(480, 640, core::CV_8UC3, core::Scalar::all(128.0))?;
|
||||
assert_eq!(detector.count(&frame)?, 0);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user