From 3aff0ea5fa068e94e4352f64178da6bcd5d49cf6 Mon Sep 17 00:00:00 2001 From: Jules Youngberg Date: Sun, 12 Jun 2022 10:21:03 -0700 Subject: [PATCH] return specialized structs rather than landmark slices --- examples/hello.rs | 21 +++++++++----------- src/lib.rs | 50 +++++++++++++++++++++++++++++++++++------------ 2 files changed, 46 insertions(+), 25 deletions(-) diff --git a/examples/hello.rs b/examples/hello.rs index 5f48af1..92f9b68 100644 --- a/examples/hello.rs +++ b/examples/hello.rs @@ -63,13 +63,12 @@ mod examples { opencv::core::flip(&rgb_frame, &mut flip_frame, 1)?; // horizontal println!("processing"); - let data = detector.process(&flip_frame); - println!("received {} landmarks", data.len()); + let result = detector.process(&flip_frame); highgui::imshow(window, &mut flip_frame)?; - if data.len() > 0 { - println!("LANDMARK: {} {} {}", data[0].x, data[0].y, data[0].z); + if let Some(fm) = result { + println!("LANDMARK: {} {} {}", fm.data[0].x, fm.data[0].y, fm.data[0].z); } } else { println!("WARN: Skip empty frame"); @@ -111,13 +110,12 @@ mod examples { opencv::core::flip(&rgb_frame, &mut flip_frame, 1)?; // horizontal println!("processing"); - let data = detector.process(&flip_frame); - println!("received {} landmarks", data.len()); + let result = detector.process(&flip_frame); highgui::imshow(window, &mut flip_frame)?; - if data.len() > 0 { - println!("LANDMARK: {} {} {}", data[0].x, data[0].y, data[0].z); + if let Some(hands) = result { + println!("LANDMARK: {} {} {}", hands[0].data[0].x, hands[0].data[0].y, hands[0].data[0].z); } } else { println!("WARN: Skip empty frame"); @@ -159,13 +157,12 @@ mod examples { opencv::core::flip(&rgb_frame, &mut flip_frame, 1)?; // horizontal println!("processing"); - let data = detector.process(&rgb_frame); - println!("received {} landmarks", data.len()); + let result = detector.process(&rgb_frame); highgui::imshow(window, &mut rgb_frame)?; - if data.len() > 0 { - println!("LANDMARK: {} {} {}", data[0].x, data[0].y, data[0].z); + if let Some(pose) = result { + println!("LANDMARK: {} {} {}", pose.data[0].x, pose.data[0].y, pose.data[0].z); } } else { println!("WARN: Skip empty frame"); diff --git a/src/lib.rs b/src/lib.rs index 6f842b7..2ce2ba4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,7 +20,7 @@ mod bindings; pub use bindings::*; type GraphType = mediagraph_GraphType; -type Landmark = mediagraph_Landmark; +pub type Landmark = mediagraph_Landmark; impl Default for Landmark { fn default() -> Self { @@ -34,8 +34,8 @@ impl Default for Landmark { } } -struct Pose { - data: [Landmark; 33], +pub struct Pose { + pub data: [Landmark; 33], } impl Default for Pose { @@ -46,8 +46,8 @@ impl Default for Pose { } } -struct Hand { - data: [Landmark; 21], +pub struct Hand { + pub data: [Landmark; 21], } impl Default for Hand { @@ -58,8 +58,8 @@ impl Default for Hand { } } -struct FaceMesh { - data: [Landmark; 478], +pub struct FaceMesh { + pub data: [Landmark; 478], } impl Default for FaceMesh { @@ -180,9 +180,16 @@ pub mod pose { } } - pub fn process(&mut self, input: &Mat) -> &[Landmark] { + pub fn process(&mut self, input: &Mat) -> Option { let landmarks = self.graph.process(input); - landmarks + + if landmarks.is_empty() { + return None; + } + + let mut pose = Pose::default(); + pose.data.copy_from_slice(&landmarks[..]); + Some(pose) } } @@ -226,9 +233,16 @@ pub mod face_mesh { } } - pub fn process(&mut self, input: &Mat) -> &[Landmark] { + pub fn process(&mut self, input: &Mat) -> Option { let landmarks = self.graph.process(input); - landmarks + + if landmarks.is_empty() { + return None; + } + + let mut face_mesh = FaceMesh::default(); + face_mesh.data.copy_from_slice(&landmarks[..]); + Some(face_mesh) } } @@ -291,9 +305,19 @@ pub mod hands { } } - pub fn process(&mut self, input: &Mat) -> &[Landmark] { + pub fn process(&mut self, input: &Mat) -> Option<[Hand; 2]> { let landmarks = self.graph.process(input); - landmarks + + if landmarks.is_empty() { + return None; + } + + let mut lh = Hand::default(); + let mut rh = Hand::default(); + lh.data.copy_from_slice(&landmarks[0..21]); + rh.data.copy_from_slice(&landmarks[21..42]); + + Some([lh, rh]) } }