docs: add Face Tracing improvement plan

Comprehensive plan to fix Face Tracing quality issues:

Problem:
- Python version: low thresholds (0.7, fallback 0.65)
- Rust version: incomplete implementation (bbox only, no embedding check)
- Result: 16 traces for 2 persons, internal similarity as low as 0.18

Solution:
- Improve Rust version to use embedding similarity (threshold=0.85)
- Add Qdrant integration to read embeddings
- Add quality control (trace internal similarity check)
- Expected: 2-4 traces, similarity > 0.85

Timeline: 2-3 days
Priority: P0 (core feature defect)
This commit is contained in:
Accusys
2026-10-16 15:42:27 +08:00
parent 4dbaf69a3e
commit dbd9efc8f5
@@ -0,0 +1,357 @@
# Face Tracing 改進計畫
**日期:** 2026-10-16
**優先級:** P0(核心功能缺陷)
**狀態:** 待決策
**責任團隊:** Core Team
---
## 🎯 問題摘要
**責任歸屬確認:**
- ✅ Face Tracing 是 Core Team 的責任
- ✅ Studio Team 無自己的實現
- ✅ 應該使用 Rust 版本,不是 Python
**根本問題:**
1. **Python 版本閾值問題**(`face_tracker.py`):
```python
# Line 132: 默認閾值
similarity_threshold: float = 0.7 # ← 太低
# Line 266: Fallback 條件
elif iou > 0.5 and similarity > 0.65: # ← 更低!
# Line 268: 完全不檢查相似度
elif iou > 0.35 and distance < distance_threshold:
```
2. **Rust 版本實現不完整**(`rust_store_traced_faces.rs`):
```rust
// Line 145: 只使用 bbox 距離
let distance_threshold = 100.0; // 像素距離
// Line 165-180: 沒有 embedding 相似度檢查
// 只檢查前一幀的 bbox 距離
if distance < best_distance && distance < distance_threshold {
best_match = Some((prev_trace_id, distance));
}
```
---
## 📊 問題數據
**測試影片:** `Accusys-WD_FilmRiot_test.mp4`
**實際:** 2 人(1 男 speaker,1 女性在顯示器)
**產生:** 16 個 trace(過度分割 + 內部混合)
**Trace 品質分析:**
| Trace ID | Faces | Internal Similarity | 狀態 |
|----------|-------|---------------------|------|
| trace_5 | 25 | **0.18** | ❌ 極度混合 |
| trace_9 | 15 | **0.34** | ❌ 極度混合 |
| trace_1 | 30 | **0.75** | ❌ 混合 |
| trace_3 | 20 | **0.78** | ❌ 混合 |
| trace_7 | 18 | **0.77** | ❌ 混合 |
| trace_14 | 22 | **0.76** | ❌ 混合 |
| 其他 | - | - | ⚠️ 品質參差 |
**Face Clustering 已改善但治標不治本:**
- Person_1 min similarity: 0.74 → 0.82 ✅
- 但 trace 品質仍然很差 ❌
---
## 🔧 技術分析
### 當前架構
```
Video Input
↓
Face Detection (Swift/Python)
↓
Embedding Extraction (Rust facenet)
↓
Store to Qdrant (_faces collection)
↓
Face Tracing ← 問題點
├─ Python 版本:閾值過低
└─ Rust 版本:沒有 embedding 檢查
↓
Face Clustering (Rust, 已改善)
```
### Python 版本問題詳解
**位置:** `/Users/accusys/momentry_core/tools/script/utils/face_tracker.py`
**問題代碼:**
```python
# Line 132: 默認閾值太低
similarity_threshold: float = 0.7
# Line 266: Fallback 條件太寬鬆
elif iou > 0.5 and similarity > 0.65: # 允許 0.65 的低相似度
# 匹配成功
# Line 268: 完全不檢查相似度
elif iou > 0.35 and distance < distance_threshold:
# 只要有 IOU 和距離就匹配
```
**影響:**
- 低相似度的 face 被錯誤匹配
- Trace 內部混合嚴重(最差 0.18)
- 過度分割(16 traces for 2 persons)
### Rust 版本問題詳解
**位置:** `/Users/accusys/momentry_core/tools/cli/rust/src/bin/rust_store_traced_faces.rs`
**問題代碼:**
```rust
// Line 145: 只用 bbox 距離
let distance_threshold = 100.0; // 像素距離
// Line 165-180: 沒有 embedding 檢查
if prev_frame.frame == current_frame_num - 3 || prev_frame.frame == current_frame_num - 1 {
for prev_face in &prev_frame.faces {
if let Some(prev_trace_id) = prev_face.trace_id {
let distance = calculate_bbox_distance(&current_face.bbox, &prev_face.bbox);
if distance < best_distance && distance < distance_threshold {
best_match = Some((prev_trace_id, distance));
}
}
}
}
```
**問題:**
- 完全沒有使用 embedding 信息
- 只依賴 bbox 位置,不夠可靠
- 當人移動或轉頭時容易出錯
- 可能導致更嚴重的混合問題
---
## ✅ 改進方案
### 方案 1:改進 Rust Face Tracing(推薦)
**目標:**
- 從 Qdrant 讀取 embeddings
- 加入 embedding 相似度檢查
- 加入 IOU 檢查
- 移除過於寬鬆的 fallback 條件
**實現步驟:**
1. **修改 `rust_store_traced_faces.rs`:**
```rust
// 加入 embedding 相似度檢查
let similarity = calculate_cosine_similarity(
&current_face.embedding,
&prev_face.embedding
);
// 閾值設置
let similarity_threshold = 0.85; // 更嚴格
// 匹配邏輯
if distance < distance_threshold
&& similarity > similarity_threshold {
best_match = Some((prev_trace_id, distance));
}
```
2. **從 Qdrant 讀取 embeddings:**
```rust
// 初始化 Qdrant client
let qdrant = QdrantFaceStore::new().await?;
// 讀取所有 faces with embeddings
let faces_with_embeddings = qdrant.scroll_all_faces(&file_uuid).await?;
```
3. **加入品質控制:**
```rust
// Trace 內部相似度檢查
let internal_similarity = calculate_internal_similarity(&trace_faces);
if internal_similarity < 0.80 {
trace.quality = "low";
// 可以選擇拆分或標記
}
```
4. **更新配置:**
```json
{
"name": "face_trace",
"processor_type": "face_trace",
"tool": {
"name": "rust_store_traced_faces",
"priority": 0,
"parameters": {
"similarity_threshold": 0.85,
"distance_threshold": 100.0,
"min_trace_len": 3,
"min_confidence": 0.5
}
}
}
```
**預期效果:**
- Trace 數量:16 → 2-4(更符合實際)
- Trace 內部相似度:> 0.85(高品質)
- 減少錯誤匹配
**開發時間:** 1-2 天
---
### 方案 2:修復 Python 版本(不推薦)
**修改 `face_tracker.py`:**
```python
# Line 132: 提高閾值
similarity_threshold: float = 0.85 # 0.7 → 0.85
# Line 266: 收緊 fallback 條件
elif iou > 0.5 and similarity > 0.80: # 0.65 → 0.80
# 匹配成功
# Line 268: 加入相似度檢查
elif iou > 0.35 and distance < distance_threshold and similarity > 0.85:
# 必須同時滿足三個條件
```
**缺點:**
- 違反「應該使用 Rust 版本」的原則
- Python 性能較差
- 維護兩個版本
---
## 📋 實現計畫
### Phase 1: Rust Face Tracing 改進(1-2 天)
**任務:**
- [ ] 修改 `rust_store_traced_faces.rs`
- [ ] 加入 Qdrant client
- [ ] 從 Qdrant 讀取 embeddings
- [ ] 實現 cosine similarity 計算
- [ ] 加入 embedding 相似度檢查(threshold=0.85)
- [ ] 加入 IOU 檢查
- [ ] 加入品質控制(trace 內部相似度)
- [ ] 更新配置
- [ ] `pipeline_flows.json` 加入 tool 配置
- [ ] 設定正確的參數
- [ ] 測試驗證
- [ ] 單元測試
- [ ] 集成測試
- [ ] 使用測試影片驗證
### Phase 2: Pipeline 重跑(半天)
**任務:**
- [ ] 重新運行 Face Tracing
- [ ] 重新運行 Face Clustering
- [ ] 驗證結果
- [ ] 更新文檔
### Phase 3: 部署(半天)
**任務:**
- [ ] 更新 production binary
- [ ] 重啟服務
- [ ] 監控日誌
- [ ] 確認穩定
---
## 🎯 預期成果
**改進前:**
- 16 traces(過度分割)
- Trace 內部相似度 0.18-0.78(品質差)
- Person_1 min similarity 0.82(勉強及格)
**改進後:**
- 2-4 traces(符合實際)
- Trace 內部相似度 > 0.85(高品質)
- Person_1 min similarity > 0.90(優秀)
---
## 🚨 風險評估
| 風險 | 影響 | 緩解措施 |
|------|------|----------|
| 改動幅度大 | 中 | 充分測試,逐步部署 |
| 影響其他影片 | 低 | 只重跑測試影片 |
| 性能問題 | 低 | Rust 性能優異 |
| 向後兼容 | 低 | 新版本不影響舊數據 |
---
## 📞 相關資源
**相關文件:**
- `/Users/accusys/momentry_core/tools/script/utils/face_tracker.py` - Python 版本
- `/Users/accusys/momentry_core/tools/cli/rust/src/bin/rust_store_traced_faces.rs` - Rust 版本
- `/Users/accusys/momentry_core/tools/cli/rust/src/bin/face_processor/` - Face processing 模塊
**配置文件:**
- `/Users/accusys/momentry_core/config/pipeline_flows.json` - Pipeline 配置
**測試數據:**
- File UUID: `7a1523df54e2161a54e4577d1835acfd`
- Qdrant collection: `_faces`
---
## ✅ 決策建議
**推薦方案:** 方案 1(改進 Rust Face Tracing)
**理由:**
1. ✅ 符合「使用 Rust 版本」的原則
2. ✅ 從根源解決問題
3. ✅ 提高 trace 品質
4. ✅ 性能更好
5. ✅ 更易維護
**不推薦方案:** 方案 2(修復 Python 版本)
**理由:**
1. ❌ 違反「使用 Rust 版本」的原則
2. ❌ Python 性能較差
3. ❌ 需要維護兩個版本
---
## 📅 時程
**預估總時間:** 2-3 天
- Day 1: Rust Face Tracing 改進
- Day 2: 測試驗證
- Day 3: Pipeline 重跑 + 部署
---
## Version History
| Version | Date | Changes |
|---------|------|---------|
| 1.0 | 2026-10-16 | 初始計畫文檔 |