design: Swift Face Trace with Apple Vision + ANE

目標:
- 實現 Swift 版本的 Face Trace
- 使用 Apple Vision + ANE 加速
- 統一所有 face 工具使用 Swift

技術方案:
1. VNTrackFaceLandmarksRequest (推薦)
   - 原生追蹤 API
   - ANE 加速
   - 自動 face ID 分配

2. VNDetectFaceLandmarksRequest + 自定義追蹤
   - 更靈活
   - 需要自己實現追蹤邏輯

當前狀態:
- FACE: Swift ✅ (default)
- FACE_TRACE: Rust (因為沒有 Swift 版本)

設計內容:
- API 選擇 (VNTrackFaceLandmarksRequest)
- 架構設計
- 性能預期
- 實現步驟
- 風險評估

實現計劃:
- Phase 1: 原型 (2-3 天)
- Phase 2: 優化 (1-2 天)
- Phase 3: 集成 (1 天)

配置更新:
- face_trace: Swift (priority 0) > Rust (priority 1)

預期收益:
- Apple Vision + ANE 加速
- 統一 face 工具架構
- 可能的性能提升

風險:
- ⚠️ Apple Vision API 穩定性
- ⚠️ faceID 跨幀連續性

建議:
- 短期: 繼續使用 Rust (已驗證)
- 中期: 實現 Swift 原型
- 長期: 根據性能決定
This commit is contained in:
Accusys
2026-09-28 16:36:48 +08:00
parent 0180661438
commit 6c705df0d9
+294
View File
@@ -0,0 +1,294 @@
---
title: Swift Face Trace Design
version: 1.0
date: 2026-09-28
author: OpenCode
status: design
---
## 目標
實現 Swift 版本的 Face Trace,使用 Apple Vision + ANE 加速。
---
## 背景
### 當前狀態
**FACE_TRACE**: 使用 Rust (rust_store_traced_faces)
- 性能: 20x 更快 (vs Python)
- 工具: rust_store_traced_faces
### 用戶需求
**所有 face 相關工具應該使用 Swift + Apple Vision**
---
## Apple Vision API
### VNTrackFaceLandmarksRequest
**功能**: 在視頻中跟蹤人臉特徵點
**API**:
```swift
import Vision
// 創建跟蹤請求
let trackRequest = VNTrackFaceLandmarksRequest()
// 處理視頻幀
let handler = VNSequenceRequestHandler()
try handler.perform([trackRequest], on: pixelBuffer)
// 獲取結果
if let observation = trackRequest.results?.first as? VNFaceObservation {
// 獲取 face ID (用於追蹤)
let faceID = observation.faceID
// 獲取特徵點
let landmarks = observation.landmarks
}
```
---
### VNDetectFaceLandmarksRequest
**功能**: 檢測單幀中的人臉特徵點
**API**:
```swift
let detectRequest = VNDetectFaceLandmarksRequest()
let handler = VNImageRequestHandler(cvPixelBuffer: pixelBuffer)
try handler.perform([detectRequest])
if let results = detectRequest.results {
for observation in results {
let faceID = observation.faceID // 自動分配的 ID
let landmarks = observation.landmarks
}
}
```
---
## 實現方案
### 方案 A: VNTrackFaceLandmarksRequest (推薦)
**優勢**:
- ✅ 原生追蹤 API
- ✅ ANE 加速
- ✅ 自動 face ID 分配
- ✅ 跨幀追蹤
**實現**:
```swift
import Vision
import AVFoundation
func traceFaces(videoPath: String) throws -> [FaceTrace] {
let asset = AVAsset(url: URL(fileURLWithPath: videoPath))
let reader = try AVAssetReader(asset: asset)
// 創建追蹤請求
let trackRequest = VNTrackFaceLandmarksRequest()
// 讀取視頻幀
// 對每個關鍵幀執行追蹤
// 收集 face ID 和位置
}
```
---
### 方案 B: VNDetectFaceLandmarksRequest + 自定義追蹤
**優勢**:
- ✅ 更靈活
- ✅ 可自定義追蹤邏輯
**缺點**:
- ⚠️ 需要自己實現追蹤邏輯
- ⚠️ 開發時間更長
---
## 架構設計
### swift_face_trace
**輸入**: 視頻文件路徑
**輸出**: face_traced.json
```json
{
"traces": [
{
"trace_id": 1,
"total_appearances": 140,
"avg_confidence": 0.85,
"frames": [
{"frame": 0, "bbox": {...}, "confidence": 0.90},
{"frame": 3, "bbox": {...}, "confidence": 0.88}
]
}
]
}
```
---
### 核心邏輯
```swift
func detectFacesInFrame(_ buffer: CVPixelBuffer) -> [FaceDetection] {
let request = VNDetectFaceLandmarksRequest()
let handler = VNImageRequestHandler(cvPixelBuffer: buffer)
try handler.perform([request])
var faces: [FaceDetection] = []
for observation in request.results ?? [] {
let face = FaceDetection(
faceID: observation.faceID,
bbox: observation.boundingBox,
confidence: observation.confidence
)
faces.append(face)
}
return faces
}
func assignTraceIDs(_ frames: [[FaceDetection]]) -> [FaceTrace] {
// 基於 faceID 和空間位置分配 trace_id
// 確保同一個人在整個視頻中有相同的 trace_id
}
```
---
## 性能預期
### 對比 Rust
| 方法 | 性能 | 理由 |
|------|------|------|
| Rust | 0.1s | CPU 優化 |
| Swift (ANE) | ? | 待測試 |
**預期**:
- 如果 ANE 加速明顯 → Swift 更快
- 如果 ANE 開銷大 → Rust 更快
---
## 實現步驟
### Phase 1: 原型 (2-3 天)
1. 實現基本 face detection
2. 實現 trace ID 分配
3. 生成 JSON 輸出
4. 單元測試
---
### Phase 2: 性能優化 (1-2 天)
1. 降低幀處理頻率
2. 優化內存使用
3. 性能基準測試
---
### Phase 3: 集成 (1 天)
1. 更新配置文件
2. 性能對比測試
3. 文檔更新
---
## 配置更新
### 當前配置
```json
{
"face_trace": {
"implementations": [
{"tool": "rust", "priority": 0, "enabled": true}
],
"default": {"tool": "rust"}
}
}
```
---
### 新配置
```json
{
"face_trace": {
"implementations": [
{"tool": "swift", "priority": 0, "enabled": true},
{"tool": "rust", "priority": 1, "enabled": true}
],
"default": {"tool": "swift"}
}
}
```
---
## 風險
### 技術風險
- ⚠️ VNTrackFaceLandmarksRequest 可能不穩定
- ⚠️ faceID 可能跨幀不連續
### 時間風險
- ⚠️ 需要調試 Apple Vision API
- ⚠️ 性能優化可能需要更多時間
---
## 總結
### ✅ 可行
使用 Swift + Apple Vision 實現 face_trace **技術可行**。
### 📊 預期收益
1. **Apple Vision + ANE**: 硬件加速
2. **統一架構**: 所有 face 工具使用 Swift
3. **性能提升**: 可能超越 Rust
### 🎯 建議
**短期**: 繼續使用 Rust (已驗證,20x 更快)
**中期**: 實現 Swift 版本原型
**長期**: 根據性能測試結果決定
---
**下一步**: 創建 swift_face_trace 原型,驗證 Apple Vision API
---
**設計日期**: 2026-09-28
**狀態**: 📝 **設計完成,待實現**