const BASE_URL: &str = "https://supermaker.ai/image/ai-pose-generator/";
#[derive(Debug, PartialEq)]
pub struct Pose {
pub nose_x: Option<f32>,
pub nose_y: Option<f32>,
pub left_eye_x: Option<f32>,
pub left_eye_y: Option<f32>,
pub right_eye_x: Option<f32>,
pub right_eye_y: Option<f32>,
}
impl Pose {
pub fn new() -> Self {
Pose {
nose_x: None,
nose_y: None,
left_eye_x: None,
left_eye_y: None,
right_eye_x: None,
right_eye_y: None,
}
}
}
pub fn describe_pose(pose: &Pose) -> String {
let mut description = String::new();
if pose.nose_x.is_some() && pose.nose_y.is_some() {
description.push_str(&format!(
"Nose at ({:.2}, {:.2}). ",
pose.nose_x.unwrap(),
pose.nose_y.unwrap()
));
}
if pose.left_eye_x.is_some() && pose.left_eye_y.is_some() {
description.push_str(&format!(
"Left eye at ({:.2}, {:.2}). ",
pose.left_eye_x.unwrap(),
pose.left_eye_y.unwrap()
));
}
if pose.right_eye_x.is_some() && pose.right_eye_y.is_some() {
description.push_str(&format!(
"Right eye at ({:.2}, {:.2}). ",
pose.right_eye_x.unwrap(),
pose.right_eye_y.unwrap()
));
}
if description.is_empty() {
"No pose data available.".to_string()
} else {
description
}
}
pub fn estimate_pose_from_image(_image_data: &[u8]) -> Pose {
let mut pose = Pose::new();
pose.nose_x = Some(0.5);
pose.nose_y = Some(0.6);
pose.left_eye_x = Some(0.4);
pose.left_eye_y = Some(0.5);
pose.right_eye_x = Some(0.6);
pose.right_eye_y = Some(0.5);
pose
}
pub fn get_endpoint(path: &str) -> String {
let mut url = String::from(BASE_URL);
url.push_str(path);
url
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_pose_creation() {
let pose = Pose::new();
assert_eq!(pose.nose_x, None);
assert_eq!(pose.nose_y, None);
assert_eq!(pose.left_eye_x, None);
assert_eq!(pose.left_eye_y, None);
assert_eq!(pose.right_eye_x, None);
assert_eq!(pose.right_eye_y, None);
}
#[test]
fn test_describe_pose() {
let mut pose = Pose::new();
pose.nose_x = Some(0.5);
pose.nose_y = Some(0.6);
let description = describe_pose(&pose);
assert_eq!(description, "Nose at (0.50, 0.60). ");
let empty_pose = Pose::new();
let empty_description = describe_pose(&empty_pose);
assert_eq!(empty_description, "No pose data available.");
}
#[test]
fn test_get_endpoint() {
let path = "api/v1/poses";
let expected_url = format!("{}{}", BASE_URL, path);
assert_eq!(get_endpoint(path), expected_url);
}
#[test]
fn test_estimate_pose_from_image() {
let image_data: [u8; 0] = [];
let pose = estimate_pose_from_image(&image_data);
assert_eq!(pose.nose_x, Some(0.5));
assert_eq!(pose.nose_y, Some(0.6));
assert_eq!(pose.left_eye_x, Some(0.4));
assert_eq!(pose.left_eye_y, Some(0.5));
assert_eq!(pose.right_eye_x, Some(0.6));
assert_eq!(pose.right_eye_y, Some(0.5));
}
}