fix: move capture inference and print hook off camera thread

This commit is contained in:
2026-11-10 15:10:19 +01:00
parent 838becdd70
commit 898ceb5726
4 changed files with 170 additions and 14 deletions

View File

@@ -34,7 +34,9 @@ struct PreviewFrame {
}
fn main() -> Result<(), Box<dyn Error>> {
core::set_num_threads(2)?;
// One OpenCV inference thread per worker leaves CPU time for the camera/UI
// on the dual-core kiosk instead of each model spawning two busy threads.
core::set_num_threads(1)?;
if let Err(error) = diagnostics::log_runtime() {
eprintln!("Failed to read OpenCV runtime diagnostics: {error}");
}
@@ -46,8 +48,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let (mut cam, mut frame) = camera::wait_until_ready(&mut camera_log)?;
mirror_frame(&mut frame)?;
let mut detector = palm::Detector::new()?;
let mut face_detector = faces::Detector::new()?;
let capture_worker = capture::Worker::new();
#[cfg(debug_assertions)]
let mut live_faces = faces::LivePreview::new()?;
let ui = AppWindow::new()?;
@@ -70,6 +71,13 @@ fn main() -> Result<(), Box<dyn Error>> {
// A rendezvous channel drops samples while inference is busy, avoiding a backlog.
let (detection_tx, detection_rx) = mpsc::sync_channel::<Mat>(0);
let detection_worker = thread::spawn(move || {
let mut detector = match palm::Detector::new() {
Ok(detector) => detector,
Err(error) => {
eprintln!("[detection ERROR] Could not load palm/hand models: {error}");
return;
}
};
let mut inference_log = diagnostics::SlowLog::new(
"palm + open-hand detection",
palm::DETECTION_INTERVAL,
@@ -123,14 +131,20 @@ fn main() -> Result<(), Box<dyn Error>> {
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);
// Wait to start another countdown while a capture/print job is
// pending. The live preview and palm tracking keep running.
let take_picture = sequence.tick(
tracker_guard.complete(now) && capture_worker.is_idle(),
hand_detected,
now,
);
if take_picture {
// 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 failed: {error}");
// The snapshot is the capture: lighting can end after this copy.
// Face counting and the empty print hook run on their own worker.
let snapshot = frame.try_clone()?;
if let Err(error) = capture_worker.submit(snapshot) {
eprintln!("[capture ERROR] Capture not queued: {error}");
}
tracker_guard = tracker.lock().unwrap();
}
if sequence.tracking() {
tracker_guard.draw(&mut frame, now)?;