refactor: migrate POSE processor to config-driven tool selection
實施策略: - 漸進式替換(保留 hard-coded 作為備用) - 優先嘗試配置驅動 - 失敗回退到 hard-coded 新增: - run_pose_with_config() 函數 - 配置驅動的工具選擇 修改位置: - pose.rs line 161 (插值邏輯) - pose.rs line 321 (初始處理) 修正: - ToolSelector.swift 路徑查找 配置: - processor_tools.json 定義工具優先級 優勢: - ✅ 無需修改代碼即可調整優先級 - ✅ 保留 hard-coded 備用 - ✅ 向後兼容 - ✅ 配置參數可調 測試: - ✅ lib 編譯通過 - ⏳ 功能測試待執行
This commit is contained in:
+141
-7
@@ -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::<usize>()
|
||||
);
|
||||
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<Option<PoseResult>> {
|
||||
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::*;
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user