diff --git a/src/core/processor/pose.rs b/src/core/processor/pose.rs index d0f09402..cb36c0c5 100644 --- a/src/core/processor/pose.rs +++ b/src/core/processor/pose.rs @@ -3,6 +3,7 @@ use serde::{Deserialize, Serialize}; use std::time::Duration; use super::executor::PythonExecutor; +use super::tool_selector::ToolSelector; use crate::core::config::USE_RUST_PROCESSORS; const POSE_TIMEOUT: Duration = Duration::from_secs(7200); @@ -157,10 +158,22 @@ pub async fn process_pose( total_video_frames ); - let executor = PythonExecutor::new()?; +let executor = PythonExecutor::new()?; - // Check for MediaPipe Pose first (highest accuracy) - let mediapipe_script = executor.script_path("pose_processor_mediapipe.py"); +// Try config-driven tool selection first +if let Ok(Some(result)) = run_pose_with_config(&executor, video_path, output_path, uuid, frames).await { + tracing::info!( + "[POSE] Config-driven interpolation completed: {} frames", + result.frames.len() + ); + return Ok(result); +} + +tracing::info!("[POSE] Config-driven selection failed, falling back to hard-coded interpolation logic"); + +// Fallback to hard-coded logic +// Check for MediaPipe Pose first (highest accuracy) +let mediapipe_script = executor.script_path("pose_processor_mediapipe.py"); if mediapipe_script.exists() { tracing::info!("[POSE] Using MediaPipe Pose for interpolation (highest accuracy): {:?}", mediapipe_script); @@ -306,12 +319,25 @@ pub async fn process_pose( } } - let executor = PythonExecutor::new()?; +let executor = PythonExecutor::new()?; - tracing::info!("[POSE] Starting pose estimation: {}", video_path); +tracing::info!("[POSE] Starting pose estimation: {}", video_path); - // Check for MediaPipe Pose first (highest accuracy) - let mediapipe_script = executor.script_path("pose_processor_mediapipe.py"); +// Try config-driven tool selection first +if let Ok(Some(result)) = run_pose_with_config(&executor, video_path, output_path, uuid, frames).await { + tracing::info!( + "[POSE] Config-driven processing completed: {} frames, {} total poses", + result.frames.len(), + result.frames.iter().map(|f| f.persons.len()).sum::() + ); + return Ok(result); +} + +tracing::info!("[POSE] Config-driven selection failed, falling back to hard-coded logic"); + +// Fallback to hard-coded logic +// Check for MediaPipe Pose first (highest accuracy) +let mediapipe_script = executor.script_path("pose_processor_mediapipe.py"); if mediapipe_script.exists() { tracing::info!("[POSE] Using MediaPipe Pose (highest accuracy): {:?}", mediapipe_script); @@ -448,6 +474,114 @@ pub async fn process_pose( Ok(result) } +async fn run_pose_with_config( + executor: &PythonExecutor, + video_path: &str, + output_path: &str, + uuid: Option<&str>, + frames: Option<&[i64]>, +) -> Result> { + use super::tool_selector::ToolSelector; + + let selector = ToolSelector::new("pose"); + + if let Some(tool) = selector.select_tool(executor).await? { + tracing::info!( + "[POSE] Using tool from config: {} (priority {})", + tool.name, + tool.parameters.get("priority").and_then(|v| v.as_u64()).unwrap_or(99) + ); + + match tool.name.as_str() { + "mediapipe" => { + let model_complexity = tool.parameters + .get("model_complexity") + .and_then(|v| v.as_u64()) + .unwrap_or(2) as u8; + + tracing::info!("[POSE] MediaPipe model_complexity={}", model_complexity); + + executor + .run_with_frames( + "pose_processor_mediapipe.py", + &[video_path, output_path], + uuid, + "POSE", + Some(POSE_TIMEOUT), + frames, + ) + .await + .with_context(|| "Failed to run MediaPipe Pose")?; + + let json_str = std::fs::read_to_string(output_path) + .context("Failed to read MediaPipe POSE output")?; + let result: PoseResult = serde_json::from_str(&json_str) + .context("Failed to parse MediaPipe POSE output")?; + + return Ok(Some(result)); + } + "swift" => { + let swift_path = &tool.path; + tracing::info!("[POSE] Using Swift processor from config: {:?}", swift_path); + + let mut args = vec![video_path.to_string(), output_path.to_string()]; + if let Some(u) = uuid { + args.push("--uuid".to_string()); + args.push(u.to_string()); + } + + let output = tokio::process::Command::new(swift_path) + .args(&args) + .output() + .await + .context("Failed to execute swift_pose")?; + + if !output.status.success() { + let stderr = String::from_utf8_lossy(&output.stderr); + tracing::error!("[POSE] Swift processor failed: {}", stderr); + anyhow::bail!("Swift pose processor failed"); + } + + let json_str = std::fs::read_to_string(output_path) + .context("Failed to read Swift POSE output")?; + let result: PoseResult = serde_json::from_str(&json_str) + .context("Failed to parse Swift POSE output")?; + + return Ok(Some(result)); + } + "rust" => { + let rust_path = &tool.path; + tracing::info!("[POSE] Using Rust processor from config: {:?}", rust_path); + + executor + .run_rust_with_output_idx_and_frames( + rust_path, + &[video_path, output_path, output_path], + Some(2), + uuid, + None, + "POSE", + Some(POSE_TIMEOUT), + ) + .await + .with_context(|| format!("Failed to run Rust binary {:?}", rust_path))?; + + let json_str = std::fs::read_to_string(output_path) + .context("Failed to read POSE output")?; + let result: PoseResult = serde_json::from_str(&json_str) + .context("Failed to parse POSE output")?; + + return Ok(Some(result)); + } + _ => { + tracing::warn!("[POSE] Unknown tool from config: {}", tool.name); + } + } + } + + Ok(None) +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/core/processor/tool_selector.rs b/src/core/processor/tool_selector.rs index 075a4b5f..c722c096 100644 --- a/src/core/processor/tool_selector.rs +++ b/src/core/processor/tool_selector.rs @@ -74,7 +74,17 @@ impl ToolSelector { } } "rust" => executor.rust_binary_path("rust_pose"), - "swift" => executor.swift_binary_path("swift_pose"), + "swift" => { + // Swift binaries are in scripts/swift_processors/.build/release/ + let swift_path = std::path::Path::new( + "/Users/accusys/momentry_core/scripts/swift_processors/.build/release/swift_pose" + ); + if swift_path.exists() { + Some(swift_path.to_path_buf()) + } else { + None + } + } "yolov8" => { let script = executor.script_path("pose_processor_mps.py"); if script.exists() {