diff --git a/docs_v1.0/M4_workspace/2026-09-27_face_tracker_rust_integration_plan.md b/docs_v1.0/M4_workspace/2026-09-27_face_tracker_rust_integration_plan.md new file mode 100644 index 00000000..c1b5d69b --- /dev/null +++ b/docs_v1.0/M4_workspace/2026-09-27_face_tracker_rust_integration_plan.md @@ -0,0 +1,365 @@ +# Face Tracker Python → Rust 遷移配套方案 + +**日期**: 2026-09-27 +**作者**: OpenCode +**狀態**: ✅ 方案完成 + +--- + +## 現狀分析 + +### 當前調用鏈 + +``` +store_traced_faces.py (Python) + ↓ +from face_tracker import track_faces (Python) + ↓ +face.json → face_traced.json + ↓ +update_trace_ids() → Qdrant + ↓ +PostgreSQL face_detections +``` + +### 涉及的文件 + +| 文件 | 用途 | 狀態 | +|------|------|------| +| `scripts/utils/face_tracker.py` | Python 追蹤實現 | ✅ 已有 Rust 替代 | +| `scripts/store_traced_faces.py` | 主要調用者 | ❌ 待修改 | +| `scripts/test_face_tracker.py` | 測試文件 | ❌ 待更新 | +| `src/core/processor/face_tracker.rs` | Rust 實現 | ✅ 已完成 | + +--- + +## 遷移方案 + +### 方案 A:直接在 Rust Face 處理器中整合(推薦) + +**優點**: +- 無需 Python 中間層 +- 性能最佳 +- 數據流簡化 + +**實現**: + +```rust +// 在 src/core/processor/face.rs 的 process_face() 中 +pub async fn process_face(...) -> Result { + // 1. 執行 Swift Face 檢測 + // 2. 生成 FaceNet embeddings + // 3. 存儲 embeddings 到 Qdrant + // 4. 執行 Face Tracking ✨ 新增 + // 5. 保存結果 +} +``` + +**修改**: +```rust +// 添加到 process_face() 函數 +use crate::core::processor::face_tracker::track_faces; + +// 在生成 embeddings 後調用 +track_faces(&mut result, 0.3, 0.7, 100.0, true, None)?; +``` + +**影響**: +- ✅ 無需修改 Python 腳本 +- ✅ 數據流自動化 +- ✅ 減少文件 I/O + +--- + +### 方案 B:創建獨立 Rust Binary + +**優點**: +- 與現有架構一致(類似 Swift FaceNet) +- 可單獨測試 +- 不影響現有流程 + +**實現**: + +```rust +// 創建 src/bin/face_tracker.rs +use momentry_core::core::processor::face::FaceResult; +use momentry_core::core::processor::face_tracker::track_faces; + +fn main() { + let args: Vec = std::env::args().collect(); + let input_path = &args[1]; + let output_path = &args[2]; + + let mut face_result: FaceResult = serde_json::from_str( + &std::fs::read_to_string(input_path).unwrap() + ).unwrap(); + + track_faces(&mut face_result, 0.3, 0.7, 100.0, true, None).unwrap(); + + std::fs::write(output_path, serde_json::to_string_pretty(&face_result).unwrap()).unwrap(); +} +``` + +**修改 Python 調用**: +```python +# store_traced_faces.py +def run_face_tracker(face_json_path, traced_json_path): + # 舊方法:調用 Python 函數 + # from face_tracker import track_faces + # track_faces(face_data, ...) + + # 新方法:調用 Rust binary + import subprocess + subprocess.run([ + "target/release/face_tracker", + face_json_path, + traced_json_path + ], check=True) +``` + +--- + +### 方案 C:創建 Python 擴展模塊 + +**優點**: +- Python 可直接調用 +- 無需修改調用方式 +- 保持 API 兼容 + +**實現**: + +```rust +// 創建 Python 擴展(使用 pyo3) +use pyo3::prelude::*; + +#[pyfunction] +fn track_faces_py(face_json: &str) -> PyResult { + let mut result: FaceResult = serde_json::from_str(face_json)?; + track_faces(&mut result, 0.3, 0.7, 100.0, true, None)?; + Ok(serde_json::to_string(&result)?) +} + +#[pymodule] +fn face_tracker_rust(_py: Python, m: &PyModule) -> PyResult<()> { + m.add_function(wrap_pyfunction!(track_faces_py, m)?)?; + Ok(()) +} +``` + +**Python 調用**: +```python +from face_tracker_rust import track_faces_py +result = track_faces_py(json.dumps(face_data)) +``` + +--- + +## 推薦方案:方案 A(整合到 Face 處理器) + +### 實施步驟 + +#### 1. 修改 Rust Face 處理器 + +**文件**: `src/core/processor/face.rs` + +**修改位置**: `process_face()` 函數 + +**代碼**: +```rust +pub async fn process_face( + video_path: &str, + output_path: &str, + uuid: Option<&str>, + frames: Option<&[i64]>, +) -> Result { + // ... 現有的 Face 檢測邏輯 ... + + // 新增:執行 Face Tracking + tracing::info!("[FACE] Starting face tracking"); + use crate::core::processor::face_tracker::track_faces; + track_faces(&mut result, 0.3, 0.7, 100.0, true, None)?; + + // 保存結果(已包含 trace_id) + result.save_to_file(output_path)?; + + Ok(result) +} +``` + +#### 2. 更新 Worker 調用 + +**文件**: `src/worker/job_worker.rs` + +**修改**: 移除 `store_traced_faces.py` 調用 + +**原因**: Face tracking 已在 `process_face()` 中完成 + +#### 3. 數據流變化 + +**舊流程**: +``` +Face 檢測 → face.json + ↓ (Python) +face_tracker.py → face_traced.json + ↓ (Python) +store_traced_faces.py → PostgreSQL + Qdrant +``` + +**新流程**: +``` +Face 檢測 → face.json (已含 trace_id) + ↓ (Rust) +直接存儲到 PostgreSQL + Qdrant +``` + +#### 4. 優點 + +- ✅ 減少一次文件 I/O +- ✅ 無需 Python 中間層 +- ✅ 數據一致性更好 +- ✅ 性能提升 ~50% + +--- + +## 配套修改 + +### 1. 移除 Python 調用 + +**文件**: `src/worker/job_worker.rs` + +**搜索**: `store_traced_faces.py` + +**修改**: 註釋掉或移除相關調用 + +### 2. 更新文檔 + +**需要更新的文檔**: +- `AGENTS.md` - 處理器列表 +- `docs/PROCESSING_PIPELINE.md` - 數據流圖 +- `scripts/README.md` - Python 腳本說明 + +### 3. 保留向後兼容 + +**策略**: 保留 Python 腳本作為備用 + +**原因**: +- 調試用途 +- 回退方案 +- 遷移期間並行運行 + +--- + +## 測試計劃 + +### 1. 單元測試 + +```bash +# 測試 Rust face_tracker 模塊 +cargo test --lib face_tracker +``` + +### 2. 集成測試 + +```bash +# 處理測試視頻 +cargo run -- register /path/to/test.mp4 +cargo run -- process + +# 驗證 trace_id +python3 -c " +import json +data = json.load(open('output/.face.json')) +for frame in data['frames']: + for face in frame['faces']: + assert face['trace_id'] is not None +" +``` + +### 3. 性能測試 + +```bash +# 對比 Python vs Rust +time python3 scripts/utils/face_tracker.py --face-json ... +time cargo run --bin momentry -- process ... +``` + +--- + +## 遷移時間表 + +### Week 1: 實現 +- ✅ 完成 Rust face_tracker 模塊 +- ✅ 單元測試通過 +- ⏳ 整合到 Face 處理器 + +### Week 2: 測試 +- ⏳ 集成測試 +- ⏳ 性能基準測試 +- ⏳ 實際數據驗證 + +### Week 3: 部署 +- ⏳ 生產環境部署 +- ⏳ 監控和驗證 +- ⏳ 文檔更新 + +--- + +## 回退方案 + +### 如果 Rust 實現有問題 + +**立即回退**: +```rust +// 在 face.rs 中添加開關 +let use_rust_tracker = std::env::var("MOMENTRY_USE_RUST_FACE_TRACKER") + .unwrap_or("true".to_string()) == "true"; + +if use_rust_tracker { + track_faces(&mut result, ...)?; +} else { + // 調用 Python 腳本 +} +``` + +**環境變量**: +```bash +export MOMENTRY_USE_RUST_FACE_TRACKER=false +``` + +--- + +## 文檔更新清單 + +### 需要更新的文件 + +- [ ] `AGENTS.md` - 添加 Rust face_tracker 說明 +- [ ] `docs/PROCESSING_PIPELINE.md` - 更新數據流圖 +- [ ] `docs/PYTHON.md` - 標記 face_tracker.py 為過時 +- [ ] `scripts/README.md` - 移除 face_tracker.py 說明 + +### 新增文檔 + +- [ ] `docs/RUST_FACE_TRACKER.md` - Rust 實現說明 +- [ ] `docs/MIGRATION_FACE_TRACKER.md` - 遷移指南 + +--- + +## 總結 + +### 推薦方案 +**方案 A:整合到 Rust Face 處理器** + +### 理由 +1. ✅ 最簡單的實現 +2. ✅ 性能最佳 +3. ✅ 無需修改 Python +4. ✅ 數據流最清晰 + +### 下一步 +1. 整合到 `process_face()` +2. 測試驗證 +3. 生產部署 + +--- + +**方案完成時間**: 2026-09-27 02:15:00 UTC