feat: add palm detection and open-hand verification
This commit is contained in:
60
src/main.rs
60
src/main.rs
@@ -3,11 +3,12 @@
|
||||
use std::{
|
||||
error::Error,
|
||||
sync::{
|
||||
atomic::{AtomicBool, Ordering},
|
||||
Arc, Mutex,
|
||||
atomic::{AtomicBool, Ordering},
|
||||
mpsc,
|
||||
},
|
||||
thread,
|
||||
time::Duration,
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
|
||||
use opencv::{core::Mat, imgproc, prelude::*, videoio};
|
||||
@@ -15,8 +16,12 @@ use slint::{Rgb8Pixel, SharedPixelBuffer};
|
||||
|
||||
slint::include_modules!();
|
||||
|
||||
mod hand_pose;
|
||||
mod palm;
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
let mut cam = videoio::VideoCapture::new(0, videoio::CAP_V4L2).expect("Failed to get video capture");
|
||||
let mut cam =
|
||||
videoio::VideoCapture::new(0, videoio::CAP_V4L2).expect("Failed to get video capture");
|
||||
let mut frame = Mat::default();
|
||||
|
||||
let codec = videoio::VideoWriter::fourcc('M', 'J', 'P', 'G')?;
|
||||
@@ -37,13 +42,11 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
let mut detector = palm::Detector::new()?;
|
||||
let ui = AppWindow::new()?;
|
||||
|
||||
ui.window().on_close_requested(|| {
|
||||
slint::CloseRequestResponse::KeepWindowShown
|
||||
});
|
||||
ui.window()
|
||||
.on_close_requested(|| slint::CloseRequestResponse::KeepWindowShown);
|
||||
|
||||
let latest_frame = Arc::new(Mutex::new(Some(frame_to_buffer(&frame)?)));
|
||||
let running = Arc::new(AtomicBool::new(true));
|
||||
@@ -51,13 +54,40 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
let worker_frame = Arc::clone(&latest_frame);
|
||||
let worker_running = Arc::clone(&running);
|
||||
|
||||
let tracker = Arc::new(Mutex::new(palm::Tracker::default()));
|
||||
let detection_tracker = Arc::clone(&tracker);
|
||||
// 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 || {
|
||||
while let Ok(sample) = detection_rx.recv() {
|
||||
let result = match detector.detect(&sample) {
|
||||
Ok(palm) => palm,
|
||||
Err(error) => {
|
||||
eprintln!("Palm detection error: {error}");
|
||||
None
|
||||
}
|
||||
};
|
||||
detection_tracker
|
||||
.lock()
|
||||
.unwrap()
|
||||
.update(result, Instant::now());
|
||||
}
|
||||
});
|
||||
|
||||
let worker = thread::spawn(move || -> opencv::Result<()> {
|
||||
let mut last_detection = Instant::now() - palm::DETECTION_INTERVAL;
|
||||
while worker_running.load(Ordering::Relaxed) {
|
||||
if !cam.read(&mut frame)? || frame.empty() {
|
||||
eprintln!("Camera stopped providing frames");
|
||||
break;
|
||||
}
|
||||
|
||||
let now = Instant::now();
|
||||
if now.duration_since(last_detection) >= palm::DETECTION_INTERVAL {
|
||||
let _ = detection_tx.try_send(frame.try_clone()?);
|
||||
last_detection = now;
|
||||
}
|
||||
tracker.lock().unwrap().draw(&mut frame, now)?;
|
||||
let buffer = frame_to_buffer(&frame)?;
|
||||
|
||||
// Replace any undisplayed frame with the newest one.
|
||||
@@ -95,20 +125,18 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
Err(_) => eprintln!("Capture thread panicked"),
|
||||
}
|
||||
|
||||
if detection_worker.join().is_err() {
|
||||
eprintln!("Palm detection thread panicked");
|
||||
}
|
||||
|
||||
result?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn frame_to_buffer(
|
||||
frame: &Mat,
|
||||
) -> opencv::Result<SharedPixelBuffer<Rgb8Pixel>> {
|
||||
fn frame_to_buffer(frame: &Mat) -> opencv::Result<SharedPixelBuffer<Rgb8Pixel>> {
|
||||
let mut rgb = Mat::default();
|
||||
|
||||
imgproc::cvt_color_def(
|
||||
frame,
|
||||
&mut rgb,
|
||||
imgproc::COLOR_BGR2RGB,
|
||||
)?;
|
||||
imgproc::cvt_color_def(frame, &mut rgb, imgproc::COLOR_BGR2RGB)?;
|
||||
|
||||
// Ensure the bytes are contiguous.
|
||||
let rgb = rgb.try_clone()?;
|
||||
|
||||
Reference in New Issue
Block a user