31d1b4050f
- Changed min_confidence from 50.0 to 0.5 (Vision uses 0.0-1.0 range) - Changed min_size from 32 to 1 (was filtering out all small faces) - Changed min_capture_quality from 25.0 to 0.25 - Fixes issue where all videos showed 0 faces detected Root cause: admin_config.rs used wrong defaults (50.0/32) instead of config.rs constants (0.5/1), causing Vision framework to filter out ALL faces because confidence threshold exceeded valid range. Verified: IMG_9449 now correctly detects 191 faces (was 0).
11 KiB
11 KiB
document_type, service, title, date, version, status, owner, created_by, tags, ai_query_hints, related_documents
| document_type | service | title | date | version | status | owner | created_by | tags | ai_query_hints | related_documents | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| design | MOMENTRY_CORE | Python → Swift Processor 遷移可行性評估 | 2026-09-15 | V1.0 | active | M5 | OpenCode |
|
|
|
Python → Swift Processor 遷移可行性評估
| 項目 | 內容 |
|---|---|
| 建立者 | OpenCode |
| 建立時間 | 2026-09-15 |
| 文件版本 | V1.0 |
| 目標讀者 | developer, Core team |
| Scope | 現行 Python processor scripts 改寫為 Swift 的可行性評估與分階段建議 |
版本歷史
| 版本 | 日期 | 目的 | 操作人 |
|---|---|---|---|
| V1.0 | 2026-09-15 | 建立 Python → Swift 遷移可行性設計文件 | OpenCode |
1. 執行摘要
Momentry Core 目前有 12 個 Rust processor 入口(位於 /Users/accusys/momentry_core/src/core/processor/),其中大部分透過 PythonExecutor 呼叫 /Users/accusys/momentry_core/scripts/ 下的 Python scripts 執行模型推理。
重要發現:OCR、Hand、Pose、Face detection 四條管線已經在呼叫 Swift binary(swift_ocr、swift_hand、swift_pose、swift_face),Python 檔案只是薄包裝(thin wrapper)+ fallback。真正的缺口在於:
- ASRX speaker diarization — Apple Speech framework 無法提供真實 diarization
- FaceNet 512D embedding — 仍由 Python(cv2 crop + coremltools)執行
- Pose keypoint interpolation(補插值到全 FPS)— 由 Python cv2/numpy 執行
- Face clustering(sklearn AgglomerativeClustering)— 無 Swift 原生替代
- CLIP / Caption(HuggingFace transformers / Moondream2 VLM)— 無原生替代
結論:建議採分階段遷移。OCR/Hand/Pose/Face 已完成(Phase A);scene classifier 走 CoreML 與 pose interpolation 移到 Rust/Swift 屬可立即推進(Phase B);其餘為高風險或保留 Python(Phase C/D)。
2. 現行管線對照表
Active 實作位於 src/core/processor/(src/processing/modules/ 屬 dead code,未在 lib.rs 中宣告,且 src/processing/handlers.rs 為 TODO stub)。
| Rust Processor | 呼叫的 Python script | 關鍵實作 | Swift 現況 |
|---|---|---|---|
| ASR | asr_processor.py |
faster-whisper (small, int8, CPU) |
asr_swift POC(Apple Speech) |
| ASRX | asrx_processor.py |
HSD 混合管線(whisper + speechbrain + sklearn) | asrx_swift POC |
| OCR | ocr_processor.py |
薄包裝 → swift_ocr,PaddleOCR fallback |
✅ 已完成 |
| Face | face_processor.py |
薄包裝 → swift_face(bbox/landmarks/pose)+ CoreML FaceNet 512D embedding |
⚠️ detection 完成,embedding 仍在 Python |
| Pose | pose_processor.py |
薄包裝 → swift_pose + cv2/numpy 補插值 + YOLOv8 fallback |
⚠️ detection 完成,interpolation 仍在 Python |
| Hand | hand_processor.py |
薄包裝 → swift_hand |
✅ 已完成 |
| Appearance | appearance_processor.py |
純 cv2/numpy HSV 顏色分析 | ✅ swift_appearance_expansion 已存在 |
| Cut | cut_processor.py |
先原生 ffmpeg select=gt(scene,0.3),Python 僅 fallback(PySceneDetect) |
✅ 幾乎原生(POC: swift_cut_test) |
| Face Cluster | fast_face_clustering_processor.py |
sklearn AgglomerativeClustering(single-stage) | ❌ 無 |
| Scene | scene_classifier.py |
CoreML Places365 / torch MPS | ⚠️ model 已是 .mlpackage(resnet18_places365.mlpackage) |
| Caption | caption_processor.py |
Moondream2(local VLM) | ❌ 無 |
| CLIP | clip_classifier.py |
HuggingFace transformers(CLIP ViT-B/32) | ❌ 無 |
| Face Recognition | face_recognition_processor.py / face_registration.py |
FaceNet 比對/註冊 | ⚠️ 視 FaceNet 遷移而定 |
| TKG | (純 Rust,無 Python) | Qdrant _faces scroll |
n/a |
| VLM Summary | (純 Rust,無 Python) | 呼叫 LLM vision function calling | n/a |
關鍵檔案位置
- Rust 入口:
/Users/accusys/momentry_core/src/core/processor/{asr,asrx,ocr,face,pose,hand,appearance,cut,face_clustering,scene_classification,caption,clip}.rs - Worker 排程:
/Users/accusys/momentry_core/src/worker/processor.rs:664-1111 - Python scripts:
/Users/accusys/momentry_core/scripts/*_processor.py - Swift package:
/Users/accusys/momentry_core/scripts/swift_processors/Package.swift(17 個 executable targets,僅依賴 swift-argument-parser) - Swift binaries 已 build:
scripts/swift_processors/.build/debug/與.build/release/
3. 逐管線可行性評估
| 管線 | 現行機制 | Swift 方案 | 判定 | 說明 |
|---|---|---|---|---|
| OCR | wrapper → swift_ocr |
已完成 | ✅ 已完成 | Vision framework OCR,無需 Python |
| Hand | wrapper → swift_hand |
已完成 | ✅ 已完成 | Vision VNDetectHumanHandPoseRequest |
| Pose detection | wrapper → swift_pose |
已完成 | ✅ 已完成 | Vision VNDetectHumanBodyPoseRequest(ANE 加速) |
| Pose interpolation | cv2/numpy 補插值到全 FPS | 需自行移植 | ⚠️ 中等 | 純數學運算,可在 Rust/Swift 實作(見 pose_processor.py:141-258) |
| Cut | ffmpeg select=scene |
原生已達成 | ✅ 可行 | ffmpeg scene filter 已是第一優先;Python 為 fallback |
| Appearance | cv2/numpy HSV 分析 | swift_appearance_expansion |
✅ 可行 | 已有現成 Swift 實作 |
| Scene classification | CoreML Places365 / torch MPS | CoreML 直接載入 | ✅ 可行 | model 為 .mlpackage,可在 Swift 用 MLModel 直接推理 |
| Face detection | wrapper → swift_face |
已完成 | ✅ 已完成 | Vision bbox/landmarks/pose |
| Face embedding | cv2 crop + CoreML FaceNet 512D | 需 CoreML Swift 移植 | ⚠️ 中等 | models/facenet512.mlpackage 為現成 .mlpackage;需實驗 MLModel 直接輸出 embedding |
| Face clustering | sklearn AgglomerativeClustering | 無原生替代 | ❌ 困難 | 需自行實作 linkage 演算法或保留 Python |
| ASR | faster-whisper (small/int8) |
asr_swift(Apple Speech) |
⚠️ 有取捨 | Apple Speech 準確度與多語系能力不及 faster-whisper;asr_processor.py:4-8 即因準確度改用 whisper |
| ASRX | HSD(whisper + speechbrain + sklearn) | asrx_swift POC |
❌ 困難 | Apple Speech 無真實 diarization;asrx_swift.swift:9-10 已自行承認此限制 |
| CLIP | HF transformers CLIP ViT-B/32 | 需導出 CoreML CLIP | ⚠️ 有條件 | 需將 CLIP model 轉 .mlpackage,工程量大 |
| Caption | Moondream2(local VLM) | 無 Swift-native VLM | ❌ 不建議 | 此規模的本地 VLM 無 Swift 原生替代 |
| TKG / VLM Summary | 純 Rust | 不需 Python | ✅ n/a | 與 Python 無關 |
4. 遷移階段建議
Phase A(已完成):Vision 偵測類 → Swift
| 管線 | 狀態 |
|---|---|
| OCR | ✅ swift_ocr |
| Hand | ✅ swift_hand |
| Pose detection | ✅ swift_pose(需保留 interpolation 回 Python) |
| Face detection | ✅ swift_face(需保留 embedding 回 Python) |
Phase B(建議下一步,風險低)
| 項目 | 作法 | 預期效益 |
|---|---|---|
| Scene classification 走 CoreML | Python scene_classifier.py 改為直接呼叫 Swift 載入 resnet18_places365.mlpackage(或直接由 Rust 以 CoreML MLModel 執行) |
移除 torch/torchvision MPS 依賴 |
| Pose interpolation 移到 Rust | 把 pose_processor.py:141-258 的 cv2/numpy 補插值邏輯改寫成 Rust 或 Swift |
移除 cv2/numpy 依賴,減少 wrapper 轉換成本 |
| Appearance 切到 Swift | 使用現成 swift_appearance_expansion,替換 appearance_processor.py |
移除 cv2/numpy 依賴 |
| 移除高成熟度管線的 Python fallback | OCR/Hand/Pose 偵測若 Swift 穩定,移除 PaddleOCR / YOLO fallback | 減少 Python 環境依賴 |
Phase C(高風險/選配)
| 項目 | 作法 | 風險 |
|---|---|---|
| FaceNet embedding 走 CoreML | 實驗以 Swift MLModel 直接對 cropped face 產生 512D embedding,取代 face_processor.py 的 cv2+coremltools |
CoreML input/output 格式需對齊 FaceNet512 原有前處理(歸一化、Crop) |
| Face clustering 自建 | 在 Rust/Swift 實作 AgglomerativeClustering(或改用固定閾值 greedy grouping) | 數值結果需與 sklearn 對齊,驗證成本高 |
Phase D(不建議)
| 管線 | 原因 |
|---|---|
| ASRX diarization | Apple Speech framework 無真實 diarization;現行 HSD 管線涉及 whisper + speechbrain + sklearn,移植至 Swift 需重新設計 |
| CLIP | 需自行導出 CoreML CLIP,且容量與精度不確定 |
| Caption | Moondream2 等級的本地 VLM 無 Swift 原生替代 |
| ASR 主路徑 | faster-whisper 準確度/多語系優於 Apple Speech;除非接受準確度下降否則保留 Python |
5. 風險與取捨
| 面向 | 風險 | 因應 |
|---|---|---|
| 模型準確度 | ASR/ASRX 改用 Apple Speech 後準確度下降 | 主路徑保留 faster-whisper;asr_swift/asrx_swift 僅做 POC 或輕量用途 |
| checksums 驗證 | PythonExecutor 在每次執行前做 SHA256 檢查(checksums.sha256) |
若新增 Swift binary 路徑,需納入 checksum 或明確標示 verify_script_integrity 適用範圍 |
| subprocess env 繼承 | Executor 傳入 9 個 env vars(MOMENTRY_OUTPUT_DIR、DATABASE_SCHEMA、MOMENTRY_REDIS_PREFIX、QDRANT_URL 等) |
Swift subprocess 需比照 Python wrapper 讀取相同 env vars |
| Swift binary 建置時機 | Swift package 需預先 build(debug/release)才能被呼叫 | 沿用 scripts/setup/upgrade_momentry.sh:285 的 swift_processors 複製機制,release/deploy 時一併建置 |
| 平台綁定 | Swift processors 依賴 macOS Vision/AVFoundation | 目前部署環境即 macOS(M5Max128),無跨平台需求 |
6. 參考文件
| 文件 | 用途 |
|---|---|
REFERENCE/history/PROCESSORS/SPEECH/ASRX_SELF_IMPLEMENTATION_FEASIBILITY.md |
ASRX Swift 自實作可行性(既有) |
DESIGN/PROCESSOR_MECHANISMS_REVIEW.md |
Processor 產出機制檢討(中斷接續/補充/糾錯) |
DESIGN/Processor_Refactoring_Assessment.md |
Processor 重構評估 |
DESIGN/Processor_Module_V1.0.md |
Processor 模組規格 |
DESIGN/Processor_State_Machine_V1.0.md |
Processor 狀態機設計 |