feat: log slow camera and detection operations

This commit is contained in:
2026-11-08 13:33:38 +01:00
parent 93bb1e3b22
commit 60dc3a76f7
4 changed files with 140 additions and 13 deletions

View File

@@ -17,6 +17,7 @@ use slint::{Rgb8Pixel, SharedPixelBuffer};
slint::include_modules!();
mod capture;
mod diagnostics;
mod hand_pose;
mod palm;
@@ -43,8 +44,13 @@ fn main() -> Result<(), Box<dyn Error>> {
}
}
let mut camera_log = diagnostics::SlowLog::new(
"camera read",
Duration::from_millis(100),
"Check camera passthrough and camera frame rate.",
);
for _ in 0..30 {
if !cam.read(&mut frame)? || frame.empty() {
if !camera_log.run(|| cam.read(&mut frame))? || frame.empty() {
return Err("Failed to capture a frame".into());
}
}
@@ -70,26 +76,44 @@ 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 inference_log = diagnostics::SlowLog::new(
"palm + open-hand detection",
palm::DETECTION_INTERVAL,
"Detection exceeds the 250 ms sampling budget; check model-stage warnings.",
);
let mut gap_log = diagnostics::SlowLog::new(
"detection result interval",
palm::STALE_AFTER,
"Results are at least 750 ms apart: the tracker expires and the steadiness timer restarts.",
);
let mut last_result: Option<Instant> = None;
while let Ok(sample) = detection_rx.recv() {
let result = match detector.detect(&sample) {
let result = match inference_log.run(|| detector.detect(&sample)) {
Ok(palm) => palm,
Err(error) => {
eprintln!("Palm detection error: {error}");
None
}
};
detection_tracker
.lock()
.unwrap()
.update(result, Instant::now());
let now = Instant::now();
if let Some(previous) = last_result {
gap_log.record(now.duration_since(previous));
}
last_result = Some(now);
detection_tracker.lock().unwrap().update(result, now);
}
});
let worker = thread::spawn(move || -> opencv::Result<()> {
let mut processing_log = diagnostics::SlowLog::new(
"preview processing",
Duration::from_millis(100),
"Includes tracker lock, circle drawing and RGB conversion; excludes camera read.",
);
let mut sequence = capture::Sequence::default();
let mut last_detection = Instant::now() - palm::DETECTION_INTERVAL;
while worker_running.load(Ordering::Relaxed) {
if !cam.read(&mut frame)? || frame.empty() {
if !camera_log.run(|| cam.read(&mut frame))? || frame.empty() {
eprintln!("Camera stopped providing frames");
break;
}
@@ -114,6 +138,8 @@ fn main() -> Result<(), Box<dyn Error>> {
presentation: sequence.presentation(now),
};
processing_log.record(now.elapsed());
// Publish image and presentation together so a countdown frame never
// accidentally contains the palm circle from an older capture frame.
*worker_frame.lock().unwrap() = Some(preview);