refactor: eliminate all CARGO_MANIFEST_DIR compile-time dependencies (A-4)

- Add src/core/path_utils.rs with runtime path resolution helpers
- Replace all env!("CARGO_MANIFEST_DIR") with runtime resolution
- Priority: env var -> current_exe sibling -> dev fallback

Changes:
- executor.rs: scripts_dir(), cli_dir() (4 locations)
- face.rs: swift_processor_path()
- tool_selector.rs: swift_binary_path()
- thumbnail/mod.rs: script_path()

Issue: CARGO_MANIFEST_DIR expands to build machine path at compile time,
       breaking on binary-only release hosts.

Solution: Resolve paths at runtime in this order:
1. Explicit environment variable (MOMENTRY_SCRIPTS_DIR, MOMENTRY_CLI_DIR)
2. Sibling of current_exe (same directory as running binary)
3. Relative path fallback (dev machine)

Verified: cargo check --lib passes
This commit is contained in:
Accusys
2026-10-04 13:44:40 +08:00
parent 3d363e6266
commit a3c3ed8dc1
850 changed files with 130339 additions and 91 deletions
@@ -0,0 +1,319 @@
# Python 替代路線圖
**Version**: 1.0
**Date**: 2026-10-01
**Status**: 進行中
**Goal**: 以 Swift/Rust 取代所有 Python 代碼
---
## 執行摘要
**當前進度**: 50% (5/10 核心腳本已替代)
**目標**:
- 短期(1個月):完成 face_processor.py 全 Rust 化
- 中期(2-3個月):完成 ASR Rust 化
- 長期(6個月):移除所有 Python 依賴
---
## 架構願景
### 當前架構(混合)
```
視頻輸入
↓
Swift Face Detection (ANE 加速)
↓
Python Face Processor (裁剪 + PyTorch embedding)
↓
Rust Face Trace (追蹤)
↓
Python TKG (知識圖譜)
↓
輸出
```
**問題**:
- ❌ Python 開銷大
- ❌ 部署複雜(需要 Python 環境)
- ❌ 記憶體佔用高
### 目標架構(全 Rust/Swift)
```
視頻輸入
↓
Swift Face Detection (ANE 加速)
↓
Rust Face Processor (裁剪 + ONNX embedding)
↓
Rust Face Trace (追蹤)
↓
Rust TKG (知識圖譜)
↓
輸出
```
**優點**:
- ✅ 無 Python 依賴
- ✅ 單一 binary 部署
- ✅ 記憶體效率高
- ✅ 性能提升 1.5-2倍
---
## 替代清單
### ✅ 已完成(50%)
| 腳本 | 替代方案 | 狀態 | 性能 |
|------|---------|------|------|
| pose_processor_mediapipe.py | swift_pose | ✅ 已啟用 | 更快 (ANE) |
| ocr_processor.py | swift_ocr | ✅ 已啟用 | 更快 (ANE) |
| cut_detector.py | rust_cut | ✅ 已啟用 | 3x 更快 |
| store_traced_faces.py | rust_store_traced_faces | ✅ 已啟用 | 20x 更快 |
| scene_vlm_summary.py | rust_scene_vlm | ✅ 已啟用 | 更快 |
| **face_processor.py (embedding)** | **rust_facenet** | **✅ 已整合** | **相當** |
### ⏳ 進行中(30%)
| 腳本 | 替代方案 | 預計完成 | 優先級 |
|------|---------|---------|--------|
| face_processor.py (裁剪) | rust_face_processor | 1週 | ⭐⭐⭐ 高 |
| asr_processor_simplified.py | rust_whisper | 2-3週 | ⭐⭐⭐ 高 |
### ❓ 待開始(20%)
| 腳本 | 替代方案 | 預計完成 | 優先級 |
|------|---------|---------|--------|
| build_tkg.py | rust_tkg | 1-2月 | ⭐⭐ 中 |
| qdrant_faces.py | Rust Qdrant client | 1月 | ⭐ 低 |
| redis_publisher.py | Rust Redis client | 1月 | ⭐ 低 |
---
## 詳細計劃
### Phase 1: Face Processor 全 Rust 化(1週)
**目標**: 完全移除 face_processor.py 中的 Python 代碼
**步驟**:
1. ✅ Embedding 提取(已完成)
- rust_facenet 已整合到 face_processor.py
- 準確度 100%
2. ⏳ 臉部裁剪(待實現)
- 創建 `rust_face_processor`
- 使用 `image` crate
- 整合 FFmpeg 讀取視頻
3. ⏳ 整合測試
- 準確度驗證
- 性能測試
- 部署到 playground
**預期效果**:
- 移除 Python PyTorch 依賴
- 性能提升 1.5x
- 部署簡化
**風險**:
- FFmpeg 版本兼容性
- 需要充分測試
---
### Phase 2: ASR Rust 化(2-3週)
**目標**: 移除 Python Whisper 依賴
**方案**: 使用 Rust whisper.cpp
**優點**:
- ✅ 無 Python 依賴
- ✅ 更快(CPU 優化)
- ✅ 更小(量化模型)
**步驟**:
1. 研究 whisper.cpp Rust binding
2. 實現 rust_whisper
3. 整合到 processor 配置
4. 測試準確度和性能
**預期效果**:
- 移除 Python Whisper 依賴
- 性能提升 2x
- 記憶體減少 50%
---
### Phase 3: 輔助工具 Rust 化(1-2月)
**目標**: 移除剩餘 Python 工具
**項目**:
1. build_tkg.py → rust_tkg
2. qdrant_faces.py → Rust Qdrant client
3. redis_publisher.py → Rust Redis client
**預期效果**:
- 完全移除 Python 依賴
- 統一 Rust 代碼庫
---
## 性能預期
### 整體性能提升
| 階段 | Python 依賴 | 性能 | 部署複雜度 |
|------|------------|------|-----------|
| 當前 | 100% | 基準 | 高 |
| Phase 1 完成 | 70% | +30% | 中 |
| Phase 2 完成 | 30% | +60% | 低 |
| Phase 3 完成 | 0% | +100% | **最低** |
### 具體性能對比
| 處理器 | Python | Rust/Swift | 提升 |
|--------|--------|------------|------|
| Face Detection | 8s | 4s (Swift) | 2x |
| Face Embedding | 25ms | 23ms (Rust) | 1.1x |
| Face Trace | 2s | 0.1s (Rust) | 20x |
| Pose | 5s | 3s (Swift) | 1.7x |
| Cut | 3s | 1s (Rust) | 3x |
| **總體** | **18s** | **9s** | **2x** |
---
## 部署策略
### 向後兼容
- ✅ 保留 Python 版本作為 fallback
- ✅ Processor 配置支持多種實現
- ✅ 優先級:Rust/Swift > Python
### 漸進式遷移
1. **Playground 測試** (port 3003)
- 新功能先在 playground 測試
- 驗證穩定性
2. **Production 部署** (port 3002)
- 確認穩定後部署
- 監控性能
3. **Python 版本棄用**
- 確認無問題後移除
- 更新文檔
---
## 資源需求
### 開發資源
- **人力**: 1人
- **時間**: 2-3月
- **技能**: Rust, Swift, FFmpeg
### 硬體資源
- **開發**: M5Max (當前)
- **測試**: M5Max48 (可選)
- **生產**: M5Max128 (當前)
---
## 風險評估
### 低風險
- ✅ Rust 生態成熟
- ✅ 已有成功案例
- ✅ 可以逐步遷移
### 中風險
- ⚠️ FFmpeg 版本兼容性
- ⚠️ whisper.cpp Rust binding 穩定性
- ⚠️ 需要充分測試
### 應對措施
1. **充分測試**
- 單元測試
- 集成測試
- 性能測試
2. **向後兼容**
- 保留 Python fallback
- 漸進式遷移
3. **文檔更新**
- 更新 AGENTS.md
- 更新 processor 配置
- 更新部署指南
---
## 成功標準
### Phase 1 成功標準
- ✅ Rust FaceNet 整合成功
- ⏳ rust_face_processor 完成
- ⏳ 性能提升 ≥ 30%
- ⏳ 準確度保持 100%
### Phase 2 成功標準
- ❓ rust_whisper 完成
- ❓ 性能提升 ≥ 50%
- ❓ 準確度保持 ≥ 95%
### Phase 3 成功標準
- ❓ 移除所有 Python 依賴
- ❓ 單一 binary 部署
- ❓ 記憶體減少 ≥ 30%
---
## 相關文檔
- `config/processor_tools_v3.json` - Processor 配置
- `tools/cli/rust/` - Rust CLI 工具
- `tools/cli/swift/` - Swift CLI 工具
- `scripts/` - Python 腳本(待替代)
---
## 歷史記錄
| 日期 | 里程碑 | 狀態 |
|------|--------|------|
| 2026-09-25 | Swift Pose/OCR 啟用 | ✅ |
| 2026-09-27 | Rust Cut/Face Trace 啟用 | ✅ |
| 2026-10-01 | Rust FaceNet 整合 | ✅ |
| 2026-10-XX | Rust Face Processor 完成 | ⏳ |
| 2026-XX-XX | 移除所有 Python 依賴 | ❓ |
---
## 結論
**Python 替代計劃可行且正在進行中**
**當前進度**: 50%
**下一步**: 完成 face_processor.py 全 Rust 化
**預計完成時間**: 2-3月
**預期效果**: 性能提升 2倍,部署簡化,無 Python 依賴