feat: face clustering pipeline improvements and Rust/Swift face processing
- Add Rust face processor module (detection, embedding, tracking, qdrant) - Add Facenet CoreML/ONNX conversion scripts and tests - Refactor swift_face into Sources/swift_face with ArgumentParser support - Update store_traced_faces to handle face.json correctly - Fix ASRX parsing and TKG bbox reading from Qdrant - Improve admin_config and trace_agent APIs - Add M4 workspace docs for face clustering, dedup, and Facenet validation - Fix worker heartbeat TTL (poll_interval x 3, min 60s) for offline detection
This commit is contained in:
@@ -0,0 +1,248 @@
|
||||
---
|
||||
title: Face Clustering Config Unification
|
||||
version: 1.0
|
||||
date: 2026-10-01
|
||||
author: OpenCode
|
||||
status: draft
|
||||
---
|
||||
|
||||
## 概述
|
||||
|
||||
統一 Face Clustering 的配置管理,解決當前三處配置不一致的問題。
|
||||
|
||||
---
|
||||
|
||||
## 當前問題
|
||||
|
||||
### 配置分散
|
||||
|
||||
| 位置 | 當前狀態 | 問題 |
|
||||
|------|---------|------|
|
||||
| Rust Binary | `default_value_t = 0.15` | ✅ 已修復 |
|
||||
| Python Script | `CLUSTER_THRESHOLD = 0.15` | ✅ 已修復 |
|
||||
| Rust Worker | `unwrap_or(0.85)` | ✅ 已修復 |
|
||||
|
||||
**問題**: 三處獨立維護,容易不一致
|
||||
|
||||
---
|
||||
|
||||
## 解決方案
|
||||
|
||||
### 方案 1: 統一常量 (推薦)
|
||||
|
||||
**優點**:
|
||||
- 編譯時檢查
|
||||
- 不易出錯
|
||||
- 性能好
|
||||
|
||||
**實現**:
|
||||
|
||||
```rust
|
||||
// src/core/config.rs
|
||||
|
||||
/// Face clustering threshold (similarity)
|
||||
/// Higher = more strict (require higher similarity to cluster together)
|
||||
/// Range: 0.0 - 1.0
|
||||
/// Recommended: 0.85 for general use, 0.90 for strict dedup
|
||||
pub const FACE_CLUSTER_SIMILARITY_THRESHOLD: f64 = 0.85;
|
||||
|
||||
/// Face clustering distance threshold
|
||||
/// Lower = more strict (require smaller distance to cluster together)
|
||||
/// This is 1.0 - SIMILARITY_THRESHOLD
|
||||
pub const FACE_CLUSTER_DISTANCE_THRESHOLD: f64 = 1.0 - FACE_CLUSTER_SIMILARITY_THRESHOLD;
|
||||
```
|
||||
|
||||
**使用**:
|
||||
|
||||
```rust
|
||||
// src/core/processor/face_clustering.rs
|
||||
use crate::core::config::{FACE_CLUSTER_SIMILARITY_THRESHOLD, FACE_CLUSTER_DISTANCE_THRESHOLD};
|
||||
|
||||
let threshold = similarity_threshold.unwrap_or(FACE_CLUSTER_SIMILARITY_THRESHOLD);
|
||||
let distance = 1.0 - threshold;
|
||||
```
|
||||
|
||||
```rust
|
||||
// tools/cli/rust/src/bin/rust_face_cluster.rs
|
||||
use momentry_core::config::FACE_CLUSTER_DISTANCE_THRESHOLD;
|
||||
|
||||
#[arg(long = "threshold", default_value_t = FACE_CLUSTER_DISTANCE_THRESHOLD)]
|
||||
threshold: f64,
|
||||
```
|
||||
|
||||
```python
|
||||
# scripts/face_clustering_processor.py
|
||||
import os
|
||||
|
||||
# 從環境變數讀取,如果沒有則使用 Rust 的默認值
|
||||
CLUSTER_THRESHOLD = float(os.getenv("MOMENTRY_FACE_CLUSTER_THRESHOLD", "0.15"))
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 方案 2: 環境變數
|
||||
|
||||
**優點**:
|
||||
- 可以動態調整
|
||||
- 不需要重新編譯
|
||||
|
||||
**缺點**:
|
||||
- 需要確保所有組件都讀取
|
||||
- 運行時錯誤
|
||||
|
||||
**實現**:
|
||||
|
||||
```bash
|
||||
# .env
|
||||
MOMENTRY_FACE_CLUSTER_THRESHOLD=0.15
|
||||
```
|
||||
|
||||
```rust
|
||||
// src/core/config.rs
|
||||
pub fn face_cluster_threshold() -> f64 {
|
||||
std::env::var("MOMENTRY_FACE_CLUSTER_THRESHOLD")
|
||||
.ok()
|
||||
.and_then(|s| s.parse().ok())
|
||||
.unwrap_or(0.15)
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 方案 3: 配置文件
|
||||
|
||||
**優點**:
|
||||
- 集中管理
|
||||
- 可視化
|
||||
|
||||
**缺點**:
|
||||
- 需要解析
|
||||
- 可能失敗
|
||||
|
||||
**實現**:
|
||||
|
||||
```json
|
||||
// config/face_cluster.json
|
||||
{
|
||||
"threshold": {
|
||||
"similarity": 0.85,
|
||||
"distance": 0.15
|
||||
},
|
||||
"method": "leader",
|
||||
"min_faces_per_trace": 1
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 推薦方案
|
||||
|
||||
**方案 1 (統一常量)** + **方案 2 (環境變數覆蓋)**
|
||||
|
||||
**理由**:
|
||||
1. 默認值使用常量,確保一致性
|
||||
2. 環境變數可以覆蓋,提供靈活性
|
||||
3. 不需要配置文件解析,減少失敗點
|
||||
|
||||
---
|
||||
|
||||
## 實現步驟
|
||||
|
||||
### Step 1: 添加統一常量
|
||||
|
||||
```rust
|
||||
// src/core/config.rs
|
||||
pub const FACE_CLUSTER_SIMILARITY_THRESHOLD: f64 = 0.85;
|
||||
pub const FACE_CLUSTER_DISTANCE_THRESHOLD: f64 = 0.15;
|
||||
```
|
||||
|
||||
### Step 2: 修改 Rust Worker
|
||||
|
||||
```rust
|
||||
// src/core/processor/face_clustering.rs
|
||||
use crate::core::config::FACE_CLUSTER_SIMILARITY_THRESHOLD;
|
||||
|
||||
let threshold = similarity_threshold
|
||||
.or_else(|| std::env::var("MOMENTRY_FACE_CLUSTER_THRESHOLD")
|
||||
.ok()
|
||||
.and_then(|s| s.parse().ok())
|
||||
.map(|d: f64| 1.0 - d))
|
||||
.unwrap_or(FACE_CLUSTER_SIMILARITY_THRESHOLD);
|
||||
```
|
||||
|
||||
### Step 3: 修改 Rust Binary
|
||||
|
||||
```rust
|
||||
// tools/cli/rust/src/bin/rust_face_cluster.rs
|
||||
use momentry_core::config::FACE_CLUSTER_DISTANCE_THRESHOLD;
|
||||
|
||||
#[arg(long = "threshold", default_value_t = FACE_CLUSTER_DISTANCE_THRESHOLD)]
|
||||
threshold: f64,
|
||||
```
|
||||
|
||||
### Step 4: 修改 Python Script
|
||||
|
||||
```python
|
||||
# scripts/face_clustering_processor.py
|
||||
import os
|
||||
|
||||
CLUSTER_THRESHOLD = float(os.getenv("MOMENTRY_FACE_CLUSTER_THRESHOLD", "0.15"))
|
||||
```
|
||||
|
||||
### Step 5: 添加環境變數
|
||||
|
||||
```bash
|
||||
# .env
|
||||
MOMENTRY_FACE_CLUSTER_THRESHOLD=0.15
|
||||
```
|
||||
|
||||
### Step 6: 添加文檔
|
||||
|
||||
```markdown
|
||||
# docs_v1.0/GUIDES/Face_Clustering_Configuration.md
|
||||
|
||||
## Threshold 說明
|
||||
|
||||
- **相似度閾值 (Similarity Threshold)**: 0.0 - 1.0
|
||||
- 越高越嚴格(需要更高相似度才能聚類)
|
||||
- 推薦值: 0.85 (一般用途), 0.90 (嚴格去重)
|
||||
|
||||
- **距離閾值 (Distance Threshold)**: 0.0 - 1.0
|
||||
- 越低越嚴格(需要更小距離才能聚類)
|
||||
- 計算方式: distance = 1.0 - similarity
|
||||
- 推薦值: 0.15 (一般用途), 0.10 (嚴格去重)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 驗證計劃
|
||||
|
||||
### 單元測試
|
||||
|
||||
```rust
|
||||
#[test]
|
||||
fn test_threshold_consistency() {
|
||||
assert_eq!(
|
||||
FACE_CLUSTER_DISTANCE_THRESHOLD,
|
||||
1.0 - FACE_CLUSTER_SIMILARITY_THRESHOLD
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
### 集成測試
|
||||
|
||||
```bash
|
||||
# 測試默認值
|
||||
./tools/cli/rust/target/release/rust_face_cluster --help | grep threshold
|
||||
|
||||
# 測試環境變數覆蓋
|
||||
MOMENTRY_FACE_CLUSTER_THRESHOLD=0.10 ./target/debug/momentry worker --test
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 版本歷史
|
||||
|
||||
| 版本 | 日期 | 變更 |
|
||||
|------|------|------|
|
||||
| 1.0 | 2026-10-01 | 初始版本 |
|
||||
Reference in New Issue
Block a user