return specialized structs rather than landmark slices

This commit is contained in:
Jules Youngberg 2022-06-12 10:21:03 -07:00
parent 6d42300104
commit 3aff0ea5fa
2 changed files with 46 additions and 25 deletions

View File

@ -63,13 +63,12 @@ mod examples {
opencv::core::flip(&rgb_frame, &mut flip_frame, 1)?; // horizontal opencv::core::flip(&rgb_frame, &mut flip_frame, 1)?; // horizontal
println!("processing"); println!("processing");
let data = detector.process(&flip_frame); let result = detector.process(&flip_frame);
println!("received {} landmarks", data.len());
highgui::imshow(window, &mut flip_frame)?; highgui::imshow(window, &mut flip_frame)?;
if data.len() > 0 { if let Some(fm) = result {
println!("LANDMARK: {} {} {}", data[0].x, data[0].y, data[0].z); println!("LANDMARK: {} {} {}", fm.data[0].x, fm.data[0].y, fm.data[0].z);
} }
} else { } else {
println!("WARN: Skip empty frame"); println!("WARN: Skip empty frame");
@ -111,13 +110,12 @@ mod examples {
opencv::core::flip(&rgb_frame, &mut flip_frame, 1)?; // horizontal opencv::core::flip(&rgb_frame, &mut flip_frame, 1)?; // horizontal
println!("processing"); println!("processing");
let data = detector.process(&flip_frame); let result = detector.process(&flip_frame);
println!("received {} landmarks", data.len());
highgui::imshow(window, &mut flip_frame)?; highgui::imshow(window, &mut flip_frame)?;
if data.len() > 0 { if let Some(hands) = result {
println!("LANDMARK: {} {} {}", data[0].x, data[0].y, data[0].z); println!("LANDMARK: {} {} {}", hands[0].data[0].x, hands[0].data[0].y, hands[0].data[0].z);
} }
} else { } else {
println!("WARN: Skip empty frame"); println!("WARN: Skip empty frame");
@ -159,13 +157,12 @@ mod examples {
opencv::core::flip(&rgb_frame, &mut flip_frame, 1)?; // horizontal opencv::core::flip(&rgb_frame, &mut flip_frame, 1)?; // horizontal
println!("processing"); println!("processing");
let data = detector.process(&rgb_frame); let result = detector.process(&rgb_frame);
println!("received {} landmarks", data.len());
highgui::imshow(window, &mut rgb_frame)?; highgui::imshow(window, &mut rgb_frame)?;
if data.len() > 0 { if let Some(pose) = result {
println!("LANDMARK: {} {} {}", data[0].x, data[0].y, data[0].z); println!("LANDMARK: {} {} {}", pose.data[0].x, pose.data[0].y, pose.data[0].z);
} }
} else { } else {
println!("WARN: Skip empty frame"); println!("WARN: Skip empty frame");

View File

@ -20,7 +20,7 @@ mod bindings;
pub use bindings::*; pub use bindings::*;
type GraphType = mediagraph_GraphType; type GraphType = mediagraph_GraphType;
type Landmark = mediagraph_Landmark; pub type Landmark = mediagraph_Landmark;
impl Default for Landmark { impl Default for Landmark {
fn default() -> Self { fn default() -> Self {
@ -34,8 +34,8 @@ impl Default for Landmark {
} }
} }
struct Pose { pub struct Pose {
data: [Landmark; 33], pub data: [Landmark; 33],
} }
impl Default for Pose { impl Default for Pose {
@ -46,8 +46,8 @@ impl Default for Pose {
} }
} }
struct Hand { pub struct Hand {
data: [Landmark; 21], pub data: [Landmark; 21],
} }
impl Default for Hand { impl Default for Hand {
@ -58,8 +58,8 @@ impl Default for Hand {
} }
} }
struct FaceMesh { pub struct FaceMesh {
data: [Landmark; 478], pub data: [Landmark; 478],
} }
impl Default for FaceMesh { 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<Pose> {
let landmarks = self.graph.process(input); 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<FaceMesh> {
let landmarks = self.graph.process(input); 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); 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])
} }
} }