refactor: migrate FACE processor to config-driven tool selection

實施策略:
- 漸進式替換(保留 hard-coded 作為備用)
- 優先嘗試配置驅動
- 失敗回退到 hard-coded

新增:
- run_face_with_config() 函數
- 配置驅動的工具選擇

修改:
- face.rs line 152 (主處理函數)
- tool_selector.rs (支持 FACE 工具)

工具優先級:
1. swift (Apple Vision) - 優先
2. python (face_processor.py) - 備用

ToolSelector 改進:
- 根據處理器名稱查找對應工具
- 支持 POSE, FACE, ASR, OCR

優勢:
- ✅ 無需修改代碼即可調整優先級
- ✅ 保留 hard-coded 備用
- ✅ 向後兼容

測試:
- ✅ lib 編譯通過
This commit is contained in:
Accusys
2026-09-28 01:47:52 +08:00
parent e0766d22fc
commit 63366e23c1
2 changed files with 170 additions and 30 deletions
+128 -3
View File
@@ -149,9 +149,22 @@ pub async fn process_face(
return Ok(result);
}
let executor = PythonExecutor::new()?;
// Check for Swift face processor first
let swift_face_path = std::path::Path::new("/Users/accusys/momentry_core/scripts/swift_processors/.build/release/swift_face");
let executor = PythonExecutor::new()?;
// Try config-driven tool selection first
if let Ok(Some(result)) = run_face_with_config(&executor, video_path, output_path, uuid).await {
tracing::info!(
"[FACE] Config-driven processing completed: {} faces",
result.total_faces
);
return Ok(result);
}
tracing::info!("[FACE] Config-driven selection failed, falling back to hard-coded logic");
// Fallback to hard-coded logic
// Check for Swift face processor first
let swift_face_path = std::path::Path::new("/Users/accusys/momentry_core/scripts/swift_processors/.build/release/swift_face");
if swift_face_path.exists() {
tracing::info!("[FACE] Using Swift processor: {:?}", swift_face_path);
@@ -323,6 +336,118 @@ result.compute_status();
Ok(result)
}
async fn run_face_with_config(
executor: &PythonExecutor,
video_path: &str,
output_path: &str,
uuid: Option<&str>,
) -> Result<Option<FaceResult>> {
use super::tool_selector::ToolSelector;
let selector = ToolSelector::new("face");
if let Some(tool) = selector.select_tool(executor).await? {
tracing::info!(
"[FACE] 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() {
"swift" => {
let swift_path = &tool.path;
tracing::info!("[FACE] 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_face")?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
tracing::error!("[FACE] Swift processor failed: {}", stderr);
anyhow::bail!("Swift face processor failed");
}
// Generate embeddings with FaceNet CoreML
let facenet_path = std::path::Path::new(
"/Users/accusys/momentry_core/scripts/swift_processors/.build/release/facenet_embedding"
);
if facenet_path.exists() {
tracing::info!("[FACE] Generating embeddings with FaceNet CoreML");
let facenet_output = format!("{}.with_embedding", output_path);
let facenet_args = vec![
video_path.to_string(),
output_path.to_string(),
facenet_output.clone(),
];
let facenet_result = tokio::process::Command::new(facenet_path)
.args(&facenet_args)
.output()
.await;
match facenet_result {
Ok(output) if output.status.success() => {
if std::path::Path::new(&facenet_output).exists() {
if let Err(e) = std::fs::copy(&facenet_output, output_path) {
tracing::warn!("[FACE] Failed to copy embedding file: {}", e);
}
}
}
_ => {
tracing::warn!("[FACE] FaceNet embedding generation failed, using detections only");
}
}
}
let json_str = std::fs::read_to_string(output_path)
.context("Failed to read Swift FACE output")?;
let result: FaceResult = serde_json::from_str(&json_str)
.context("Failed to parse Swift FACE output")?;
return Ok(Some(result));
}
"python" => {
let script_path = &tool.path;
tracing::info!("[FACE] Using Python processor from config: {:?}", script_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());
}
executor
.run("face_processor.py", &[video_path, output_path], uuid, "FACE", None)
.await
.context("Failed to run face_processor.py")?;
let json_str = std::fs::read_to_string(output_path)
.context("Failed to read Python FACE output")?;
let result: FaceResult = serde_json::from_str(&json_str)
.context("Failed to parse Python FACE output")?;
return Ok(Some(result));
}
_ => {
tracing::warn!("[FACE] Unknown tool from config: {}", tool.name);
}
}
}
Ok(None)
}
#[cfg(test)]
mod tests {
use super::*;
+42 -27
View File
@@ -64,43 +64,58 @@ impl ToolSelector {
}
fn find_tool(&self, tool_name: &str, executor: &super::PythonExecutor) -> Option<PathBuf> {
match tool_name {
"mediapipe" => {
match (self.processor_name.as_str(), tool_name) {
// POSE processor tools
("pose", "mediapipe") => {
let script = executor.script_path("pose_processor_mediapipe.py");
if script.exists() {
Some(script)
} else {
None
}
if script.exists() { Some(script) } else { None }
}
"rust" => executor.rust_binary_path("rust_pose"),
"swift" => {
// Swift binaries are in scripts/swift_processors/.build/release/
("pose", "rust") => executor.rust_binary_path("rust_pose"),
("pose", "swift") => {
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
}
if swift_path.exists() { Some(swift_path.to_path_buf()) } else { None }
}
"yolov8" => {
("pose", "yolov8") => {
let script = executor.script_path("pose_processor_mps.py");
if script.exists() {
Some(script)
} else {
None
}
if script.exists() { Some(script) } else { None }
}
"python" => {
("pose", "python") => {
let script = executor.script_path("pose_processor.py");
if script.exists() {
Some(script)
} else {
None
}
if script.exists() { Some(script) } else { None }
}
// FACE processor tools
("face", "swift") => {
let swift_path = std::path::Path::new(
"/Users/accusys/momentry_core/scripts/swift_processors/.build/release/swift_face"
);
if swift_path.exists() { Some(swift_path.to_path_buf()) } else { None }
}
("face", "python") => {
let script = executor.script_path("face_processor.py");
if script.exists() { Some(script) } else { None }
}
// ASR processor tools
("asr", "whisper") => {
let script = executor.script_path("asr_processor.py");
if script.exists() { Some(script) } else { None }
}
// OCR processor tools
("ocr", "swift") => {
let swift_path = std::path::Path::new(
"/Users/accusys/momentry_core/scripts/swift_processors/.build/release/swift_ocr"
);
if swift_path.exists() { Some(swift_path.to_path_buf()) } else { None }
}
("ocr", "python") => {
let script = executor.script_path("ocr_processor.py");
if script.exists() { Some(script) } else { None }
}
_ => None,
}
}