feat: use YuNet face detection with debug-only live boxes

This commit is contained in:
2026-11-10 15:03:18 +01:00
parent 9cf760e7a6
commit 838becdd70
7 changed files with 237 additions and 43510 deletions

View File

@@ -48,6 +48,8 @@ fn main() -> Result<(), Box<dyn Error>> {
let mut detector = palm::Detector::new()?;
let mut face_detector = faces::Detector::new()?;
#[cfg(debug_assertions)]
let mut live_faces = faces::LivePreview::new()?;
let ui = AppWindow::new()?;
ui.window()
@@ -104,8 +106,6 @@ fn main() -> Result<(), Box<dyn Error>> {
);
let mut sequence = capture::Sequence::default();
let mut last_detection = Instant::now() - palm::DETECTION_INTERVAL;
let mut last_faces = Instant::now() - faces::PREVIEW_INTERVAL;
let mut live_faces = Vec::new();
while worker_running.load(Ordering::Relaxed) {
if !camera_log.run(|| cam.read(&mut frame))? || frame.empty() {
eprintln!("Camera stopped providing frames");
@@ -119,26 +119,16 @@ fn main() -> Result<(), Box<dyn Error>> {
let _ = detection_tx.try_send(frame.try_clone()?);
last_detection = now;
}
// Run face analysis at most twice per second on the clean selfie frame.
// Cache rectangles between samples; replace them even on empty results.
if now.duration_since(last_faces) >= faces::PREVIEW_INTERVAL {
live_faces = match face_detector.detect(&frame, false) {
Ok(faces) => faces,
Err(error) => {
eprintln!("[faces ERROR] Live detection failed: {error}");
Vec::new()
}
};
last_faces = Instant::now();
}
#[cfg(debug_assertions)]
live_faces.sample(&frame)?;
let mut tracker_guard = tracker.lock().unwrap();
let hand_detected = tracker_guard.has_palm(now);
let take_picture = sequence.tick(tracker_guard.complete(now), hand_detected, now);
if take_picture {
// Detection must not hold the palm tracker lock while processing or spooling.
// Release the palm tracker lock while analyzing the captured frame.
drop(tracker_guard);
if let Err(error) = capture::take_picture(&frame, &mut face_detector) {
eprintln!("[capture ERROR] Face counting / printing failed: {error}");
eprintln!("[capture ERROR] Face counting failed: {error}");
}
tracker_guard = tracker.lock().unwrap();
}
@@ -148,7 +138,8 @@ fn main() -> Result<(), Box<dyn Error>> {
drop(tracker_guard);
// Draw only after capture and palm sampling so printed photos and
// detector inputs never contain debug rectangles.
faces::draw(&mut frame, &live_faces)?;
#[cfg(debug_assertions)]
live_faces.draw(&mut frame)?;
let preview = PreviewFrame {
buffer: frame_to_buffer(&frame)?,
hand_detected,