docs: add Studio team face filter usage guide
- Comprehensive API usage guide for Studio team - Includes all 6 API endpoints with examples - Complete TypeScript code examples - Filtering strategies and best practices - Performance optimization recommendations - Based on FaceNet analysis results from WD and KOBA videos This document enables Studio team to integrate face filtering functionality efficiently.
This commit is contained in:
@@ -0,0 +1,881 @@
|
|||||||
|
# Face Filter API - Studio 團隊使用指南
|
||||||
|
|
||||||
|
**版本**: v1.0
|
||||||
|
**日期**: 2026-08-09
|
||||||
|
**狀態**: 已實作並可整合
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 概述
|
||||||
|
|
||||||
|
Face Filter API 提供臉部過濾功能,讓 Studio 前端可以即時過濾臉部,基於:
|
||||||
|
- 信心值(confidence)
|
||||||
|
- 臉部大小(bbox_size)
|
||||||
|
- 來源(apple_vision / mediapipe)
|
||||||
|
- 後端複雜過濾結果(landmark、embedding、時間一致性)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 核心概念
|
||||||
|
|
||||||
|
### 數據分層
|
||||||
|
|
||||||
|
**層級 1: 即時過濾元數據**(前端即時操作)
|
||||||
|
- 大小:~50 bytes/face
|
||||||
|
- 包含:face_id, frame, confidence, bbox_size, source
|
||||||
|
- 用途:前端即時過濾
|
||||||
|
|
||||||
|
**層級 2: 後端過濾結果**(後端預先計算)
|
||||||
|
- 包含:filter_status, filter_confidence, filter_reasons
|
||||||
|
- 用途:複雜過濾結果
|
||||||
|
|
||||||
|
**層級 3: JPG 縮圖**(懶載入)
|
||||||
|
- 大小:~20 KB/face
|
||||||
|
- 用途:視覺預覽
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## API 端點
|
||||||
|
|
||||||
|
### 1. 獲取臉部元數據
|
||||||
|
|
||||||
|
```
|
||||||
|
GET /api/v1/file/{file_uuid}/faces
|
||||||
|
```
|
||||||
|
|
||||||
|
**用途**: 獲取所有臉部元數據(用於前端過濾)
|
||||||
|
|
||||||
|
**參數**:
|
||||||
|
```typescript
|
||||||
|
{
|
||||||
|
source?: 'all' | 'apple_vision' | 'mediapipe',
|
||||||
|
page?: number, // 預設 1
|
||||||
|
page_size?: number // 預設 50
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**回應**:
|
||||||
|
```typescript
|
||||||
|
{
|
||||||
|
success: true,
|
||||||
|
data: {
|
||||||
|
faces: FaceMetadata[],
|
||||||
|
pagination: {
|
||||||
|
page: number,
|
||||||
|
page_size: number,
|
||||||
|
total: number,
|
||||||
|
total_pages: number
|
||||||
|
},
|
||||||
|
source_summary: {
|
||||||
|
apple_vision: number,
|
||||||
|
mediapipe: number,
|
||||||
|
merged_unique: number
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**FaceMetadata 結構**:
|
||||||
|
```typescript
|
||||||
|
interface FaceMetadata {
|
||||||
|
face_id: string;
|
||||||
|
frame: number;
|
||||||
|
primary_source: 'apple_vision' | 'mediapipe';
|
||||||
|
primary_confidence: number;
|
||||||
|
detection_sources: Array<{
|
||||||
|
source_type: string;
|
||||||
|
confidence: number;
|
||||||
|
}>;
|
||||||
|
filter_features: {
|
||||||
|
face_id: string;
|
||||||
|
frame: number;
|
||||||
|
bbox_size: number; // min(width, height)
|
||||||
|
apple_vision_confidence: number;
|
||||||
|
mediapipe_confidence?: number;
|
||||||
|
eye_ratio?: number;
|
||||||
|
nose_ratio?: number;
|
||||||
|
pose_score?: number;
|
||||||
|
mediapipe_detected?: boolean;
|
||||||
|
mediapipe_iou?: number;
|
||||||
|
embedding_similarity?: number;
|
||||||
|
consecutive_frames?: number;
|
||||||
|
total_frames_detected?: number;
|
||||||
|
backend_filter_status?: 'accept' | 'reject' | 'review';
|
||||||
|
};
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**使用範例**:
|
||||||
|
```typescript
|
||||||
|
// 獲取所有臉部元數據
|
||||||
|
const response = await fetch('/api/v1/file/0489e104d7d1e11e613da01f7aa130ca/faces', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({
|
||||||
|
source: 'all',
|
||||||
|
page: 1,
|
||||||
|
page_size: 100
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
console.log(`Total faces: ${data.data.pagination.total}`);
|
||||||
|
|
||||||
|
// 儲存到本地用於即時過濾
|
||||||
|
const allFaces = data.data.faces;
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 2. 即時過濾(前端)
|
||||||
|
|
||||||
|
**用途**: 根據使用者設定即時過濾臉部
|
||||||
|
|
||||||
|
**過濾規則**:
|
||||||
|
```typescript
|
||||||
|
interface QuickFilterRules {
|
||||||
|
min_confidence?: number; // 最小信心值
|
||||||
|
max_confidence?: number; // 最大信心值
|
||||||
|
min_bbox_size?: number; // 最小臉部大小(像素)
|
||||||
|
max_bbox_size?: number; // 最大臉部大小(像素)
|
||||||
|
source?: 'apple_vision' | 'mediapipe' | 'all';
|
||||||
|
backend_status?: 'accept' | 'reject' | 'review' | 'all';
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**過濾函數**:
|
||||||
|
```typescript
|
||||||
|
const quickFilter = (faces: FaceMetadata[], rules: QuickFilterRules) => {
|
||||||
|
return faces.filter(face => {
|
||||||
|
// 1. Confidence 過濾
|
||||||
|
if (rules.min_confidence && face.primary_confidence < rules.min_confidence) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (rules.max_confidence && face.primary_confidence > rules.max_confidence) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Bbox size 過濾
|
||||||
|
if (rules.min_bbox_size && face.filter_features.bbox_size < rules.min_bbox_size) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (rules.max_bbox_size && face.filter_features.bbox_size > rules.max_bbox_size) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Source 過濾
|
||||||
|
if (rules.source && rules.source !== 'all' && face.primary_source !== rules.source) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. Backend filter status 過濾
|
||||||
|
if (rules.backend_status && rules.backend_status !== 'all' &&
|
||||||
|
face.filter_features.backend_filter_status !== rules.backend_status) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
**使用範例**:
|
||||||
|
```typescript
|
||||||
|
// 即時過濾
|
||||||
|
const filteredFaces = quickFilter(allFaces, {
|
||||||
|
min_confidence: 0.5,
|
||||||
|
min_bbox_size: 80,
|
||||||
|
source: 'all',
|
||||||
|
backend_status: 'accept'
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(`Filtered: ${filteredFaces.length} faces`);
|
||||||
|
|
||||||
|
// 更新 UI
|
||||||
|
updateFaceGrid(filteredFaces);
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 3. 套用後端過濾規則
|
||||||
|
|
||||||
|
```
|
||||||
|
POST /api/v1/file/{file_uuid}/faces/filter
|
||||||
|
```
|
||||||
|
|
||||||
|
**用途**: 套用複雜過濾規則(landmark、embedding、時間一致性)
|
||||||
|
|
||||||
|
**請求**:
|
||||||
|
```typescript
|
||||||
|
{
|
||||||
|
rules?: FilterRuleConfig // null = 使用預設規則
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**FilterRuleConfig 結構**:
|
||||||
|
```typescript
|
||||||
|
interface FilterRuleConfig {
|
||||||
|
version: string;
|
||||||
|
rules: {
|
||||||
|
landmark_analysis?: {
|
||||||
|
enabled: boolean;
|
||||||
|
weight: number;
|
||||||
|
thresholds: {
|
||||||
|
eye_ratio_min: number;
|
||||||
|
eye_ratio_max: number;
|
||||||
|
nose_ratio_min: number;
|
||||||
|
pose_score_max: number;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
mediapipe_verification?: {
|
||||||
|
enabled: boolean;
|
||||||
|
weight: number;
|
||||||
|
thresholds: {
|
||||||
|
min_iou: number;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
embedding_similarity?: {
|
||||||
|
enabled: boolean;
|
||||||
|
weight: number;
|
||||||
|
thresholds: {
|
||||||
|
min_similarity: number;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
temporal_consistency?: {
|
||||||
|
enabled: boolean;
|
||||||
|
weight: number;
|
||||||
|
thresholds: {
|
||||||
|
min_consecutive_frames: number;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
overall_thresholds: {
|
||||||
|
auto_accept: number;
|
||||||
|
auto_reject: number;
|
||||||
|
review_range: [number, number];
|
||||||
|
};
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**回應**:
|
||||||
|
```typescript
|
||||||
|
{
|
||||||
|
total_faces: number;
|
||||||
|
passed: number;
|
||||||
|
filtered: number;
|
||||||
|
passed_faces: FaceWithFilterResult[];
|
||||||
|
filtered_faces: FaceWithFilterResult[];
|
||||||
|
filter_stats: {
|
||||||
|
landmark?: number;
|
||||||
|
mediapipe?: number;
|
||||||
|
embedding?: number;
|
||||||
|
temporal?: number;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**使用範例**:
|
||||||
|
```typescript
|
||||||
|
// 使用預設規則
|
||||||
|
const response = await fetch('/api/v1/file/0489e104d7d1e11e613da01f7aa130ca/faces/filter', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ rules: null })
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await response.json();
|
||||||
|
console.log(`Passed: ${result.passed}, Filtered: ${result.filtered}`);
|
||||||
|
|
||||||
|
// 顯示被過濾的臉部
|
||||||
|
result.filtered_faces.forEach(face => {
|
||||||
|
console.log(`Face ${face.face_id} (frame ${face.frame}):`);
|
||||||
|
face.filter_result.reasons.forEach(reason => {
|
||||||
|
console.log(` - ${reason}`);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 4. 獲取單一臉部過濾報告
|
||||||
|
|
||||||
|
```
|
||||||
|
GET /api/v1/file/{file_uuid}/face/{face_id}/filter-report
|
||||||
|
```
|
||||||
|
|
||||||
|
**用途**: 獲取單一臉部的詳細過濾報告
|
||||||
|
|
||||||
|
**回應**:
|
||||||
|
```typescript
|
||||||
|
{
|
||||||
|
face_id: string;
|
||||||
|
frame: number;
|
||||||
|
overall_confidence: number;
|
||||||
|
recommendation: 'accept' | 'review' | 'reject';
|
||||||
|
rule_results: {
|
||||||
|
landmark_analysis?: {
|
||||||
|
enabled: boolean;
|
||||||
|
score: number;
|
||||||
|
passed: boolean;
|
||||||
|
reason?: string;
|
||||||
|
};
|
||||||
|
mediapipe_verification?: { ... };
|
||||||
|
embedding_similarity?: { ... };
|
||||||
|
temporal_consistency?: { ... };
|
||||||
|
};
|
||||||
|
reasons: string[];
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**使用範例**:
|
||||||
|
```typescript
|
||||||
|
// 獲取臉部過濾報告
|
||||||
|
const response = await fetch('/api/v1/file/0489e104d7d1e11e613da01f7aa130ca/face/face_001/filter-report');
|
||||||
|
const report = await response.json();
|
||||||
|
|
||||||
|
console.log(`Recommendation: ${report.recommendation}`);
|
||||||
|
console.log(`Confidence: ${report.overall_confidence}`);
|
||||||
|
console.log(`Reasons: ${report.reasons.join(', ')}`);
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 5. 獲取過濾統計
|
||||||
|
|
||||||
|
```
|
||||||
|
GET /api/v1/file/{file_uuid}/filter-stats
|
||||||
|
```
|
||||||
|
|
||||||
|
**用途**: 獲取臉部過濾統計資訊
|
||||||
|
|
||||||
|
**回應**:
|
||||||
|
```typescript
|
||||||
|
{
|
||||||
|
total_faces: number;
|
||||||
|
by_category: {
|
||||||
|
total_faces: number;
|
||||||
|
by_source: {
|
||||||
|
apple_vision: number;
|
||||||
|
mediapipe: number;
|
||||||
|
};
|
||||||
|
by_quality: {
|
||||||
|
avg_eye_ratio?: number;
|
||||||
|
avg_nose_ratio?: number;
|
||||||
|
avg_pose_score?: number;
|
||||||
|
mediapipe_verified: number;
|
||||||
|
avg_embedding_similarity?: number;
|
||||||
|
avg_consecutive_frames?: number;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 6. 管理過濾規則
|
||||||
|
|
||||||
|
#### 6.1 獲取過濾規則
|
||||||
|
|
||||||
|
```
|
||||||
|
GET /api/v1/file/{file_uuid}/filter-rules
|
||||||
|
```
|
||||||
|
|
||||||
|
**回應**:
|
||||||
|
```typescript
|
||||||
|
{
|
||||||
|
rules: Array<{
|
||||||
|
id: number;
|
||||||
|
rule_name: string;
|
||||||
|
rules: FilterRuleConfig;
|
||||||
|
is_active: boolean;
|
||||||
|
created_at: string;
|
||||||
|
updated_at: string;
|
||||||
|
}>;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 6.2 儲存過濾規則
|
||||||
|
|
||||||
|
```
|
||||||
|
POST /api/v1/file/{file_uuid}/filter-rules
|
||||||
|
```
|
||||||
|
|
||||||
|
**請求**:
|
||||||
|
```typescript
|
||||||
|
{
|
||||||
|
rule_name: string;
|
||||||
|
rules: FilterRuleConfig;
|
||||||
|
is_active?: boolean;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**使用範例**:
|
||||||
|
```typescript
|
||||||
|
// 儲存自訂規則
|
||||||
|
const customRules = {
|
||||||
|
version: "1.0",
|
||||||
|
rules: {
|
||||||
|
landmark_analysis: {
|
||||||
|
enabled: true,
|
||||||
|
weight: 0.3,
|
||||||
|
thresholds: {
|
||||||
|
eye_ratio_min: 0.30,
|
||||||
|
eye_ratio_max: 0.50,
|
||||||
|
nose_ratio_min: 0.10,
|
||||||
|
pose_score_max: 0.5
|
||||||
|
}
|
||||||
|
},
|
||||||
|
// ... 其他規則
|
||||||
|
},
|
||||||
|
overall_thresholds: {
|
||||||
|
auto_accept: 0.8,
|
||||||
|
auto_reject: 0.2,
|
||||||
|
review_range: [0.2, 0.8]
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
await fetch('/api/v1/file/0489e104d7d1e11e613da01f7aa130ca/filter-rules', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({
|
||||||
|
rule_name: "strict_filter",
|
||||||
|
rules: customRules,
|
||||||
|
is_active: true
|
||||||
|
})
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 完整使用範例
|
||||||
|
|
||||||
|
### 場景 1: 即時過濾臉部
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
class FaceFilterManager {
|
||||||
|
private allFaces: FaceMetadata[] = [];
|
||||||
|
private filteredFaces: FaceMetadata[] = [];
|
||||||
|
private currentRules: QuickFilterRules = {
|
||||||
|
min_confidence: 0.5,
|
||||||
|
min_bbox_size: 80,
|
||||||
|
source: 'all',
|
||||||
|
backend_status: 'accept'
|
||||||
|
};
|
||||||
|
|
||||||
|
// 初始化:載入臉部元數據
|
||||||
|
async initialize(fileUuid: string) {
|
||||||
|
const response = await fetch(`/api/v1/file/${fileUuid}/faces`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({
|
||||||
|
source: 'all',
|
||||||
|
page: 1,
|
||||||
|
page_size: 1000 // 載入所有臉部
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
this.allFaces = data.data.faces;
|
||||||
|
|
||||||
|
// 套用初始過濾
|
||||||
|
this.applyFilter();
|
||||||
|
|
||||||
|
return this.filteredFaces;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 即時過濾
|
||||||
|
applyFilter() {
|
||||||
|
this.filteredFaces = this.allFaces.filter(face => {
|
||||||
|
// Confidence 過濾
|
||||||
|
if (this.currentRules.min_confidence &&
|
||||||
|
face.primary_confidence < this.currentRules.min_confidence) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bbox size 過濾
|
||||||
|
if (this.currentRules.min_bbox_size &&
|
||||||
|
face.filter_features.bbox_size < this.currentRules.min_bbox_size) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Source 過濾
|
||||||
|
if (this.currentRules.source &&
|
||||||
|
this.currentRules.source !== 'all' &&
|
||||||
|
face.primary_source !== this.currentRules.source) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Backend status 過濾
|
||||||
|
if (this.currentRules.backend_status &&
|
||||||
|
this.currentRules.backend_status !== 'all' &&
|
||||||
|
face.filter_features.backend_filter_status !== this.currentRules.backend_status) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
|
||||||
|
return this.filteredFaces;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新過濾規則
|
||||||
|
updateRules(rules: Partial<QuickFilterRules>) {
|
||||||
|
this.currentRules = { ...this.currentRules, ...rules };
|
||||||
|
return this.applyFilter();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 獲取統計
|
||||||
|
getStats() {
|
||||||
|
return {
|
||||||
|
total: this.allFaces.length,
|
||||||
|
filtered: this.filteredFaces.length,
|
||||||
|
removed: this.allFaces.length - this.filteredFaces.length
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 使用範例
|
||||||
|
const manager = new FaceFilterManager();
|
||||||
|
await manager.initialize('0489e104d7d1e11e613da01f7aa130ca');
|
||||||
|
|
||||||
|
// 使用者調整過濾規則
|
||||||
|
manager.updateRules({
|
||||||
|
min_confidence: 0.7,
|
||||||
|
min_bbox_size: 100
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(manager.getStats());
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 場景 2: 後端複雜過濾
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// 套用後端過濾規則
|
||||||
|
const applyBackendFilter = async (fileUuid: string, rules?: FilterRuleConfig) => {
|
||||||
|
const response = await fetch(`/api/v1/file/${fileUuid}/faces/filter`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ rules })
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await response.json();
|
||||||
|
|
||||||
|
console.log(`Total: ${result.total_faces}`);
|
||||||
|
console.log(`Passed: ${result.passed}`);
|
||||||
|
console.log(`Filtered: ${result.filtered}`);
|
||||||
|
|
||||||
|
// 顯示過濾統計
|
||||||
|
Object.entries(result.filter_stats).forEach(([rule, count]) => {
|
||||||
|
console.log(`${rule}: ${count} faces`);
|
||||||
|
});
|
||||||
|
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
|
// 使用預設規則
|
||||||
|
const result = await applyBackendFilter('0489e104d7d1e11e613da01f7aa130ca');
|
||||||
|
|
||||||
|
// 使用自訂規則
|
||||||
|
const customRules = {
|
||||||
|
version: "1.0",
|
||||||
|
rules: {
|
||||||
|
landmark_analysis: {
|
||||||
|
enabled: true,
|
||||||
|
weight: 0.3,
|
||||||
|
thresholds: {
|
||||||
|
eye_ratio_min: 0.30,
|
||||||
|
eye_ratio_max: 0.50,
|
||||||
|
nose_ratio_min: 0.10,
|
||||||
|
pose_score_max: 0.5
|
||||||
|
}
|
||||||
|
},
|
||||||
|
// ... 其他規則
|
||||||
|
},
|
||||||
|
overall_thresholds: {
|
||||||
|
auto_accept: 0.8,
|
||||||
|
auto_reject: 0.2,
|
||||||
|
review_range: [0.2, 0.8]
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const strictResult = await applyBackendFilter('0489e104d7d1e11e613da01f7aa130ca', customRules);
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 預設過濾規則
|
||||||
|
|
||||||
|
API 提供預設過濾規則:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"version": "1.0",
|
||||||
|
"rules": {
|
||||||
|
"landmark_analysis": {
|
||||||
|
"enabled": true,
|
||||||
|
"weight": 0.3,
|
||||||
|
"thresholds": {
|
||||||
|
"eye_ratio_min": 0.25,
|
||||||
|
"eye_ratio_max": 0.55,
|
||||||
|
"nose_ratio_min": 0.08,
|
||||||
|
"pose_score_max": 0.8
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"mediapipe_verification": {
|
||||||
|
"enabled": true,
|
||||||
|
"weight": 0.2,
|
||||||
|
"thresholds": {
|
||||||
|
"min_iou": 0.3
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"embedding_similarity": {
|
||||||
|
"enabled": true,
|
||||||
|
"weight": 0.3,
|
||||||
|
"thresholds": {
|
||||||
|
"min_similarity": 0.70
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"temporal_consistency": {
|
||||||
|
"enabled": true,
|
||||||
|
"weight": 0.2,
|
||||||
|
"thresholds": {
|
||||||
|
"min_consecutive_frames": 3
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"overall_thresholds": {
|
||||||
|
"auto_accept": 0.7,
|
||||||
|
"auto_reject": 0.3,
|
||||||
|
"review_range": [0.3, 0.7]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 建議值
|
||||||
|
|
||||||
|
基於 FaceNet 分析結果:
|
||||||
|
|
||||||
|
### 即時過濾閾值
|
||||||
|
|
||||||
|
| 參數 | 保守 | 平衡(預設) | 寬鬆 |
|
||||||
|
|------|------|-------------|------|
|
||||||
|
| min_confidence | 0.7 | 0.5 | 0.3 |
|
||||||
|
| min_bbox_size | 100px | 80px | 64px |
|
||||||
|
| backend_status | accept | accept | all |
|
||||||
|
|
||||||
|
### 後端過濾閾值
|
||||||
|
|
||||||
|
| 規則 | 保守 | 平衡(預設) | 寬鬆 |
|
||||||
|
|------|------|-------------|------|
|
||||||
|
| auto_accept | 0.8 | 0.7 | 0.6 |
|
||||||
|
| auto_reject | 0.2 | 0.3 | 0.4 |
|
||||||
|
| min_similarity | 0.80 | 0.70 | 0.60 |
|
||||||
|
| min_consecutive_frames | 5 | 3 | 2 |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 效能優化建議
|
||||||
|
|
||||||
|
### 1. 數據快取
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// 快取臉部元數據
|
||||||
|
class FaceDataCache {
|
||||||
|
private cache: Map<string, FaceMetadata[]> = new Map();
|
||||||
|
|
||||||
|
async getFaces(fileUuid: string): Promise<FaceMetadata[]> {
|
||||||
|
if (this.cache.has(fileUuid)) {
|
||||||
|
return this.cache.get(fileUuid)!;
|
||||||
|
}
|
||||||
|
|
||||||
|
const faces = await loadFacesFromAPI(fileUuid);
|
||||||
|
this.cache.set(fileUuid, faces);
|
||||||
|
return faces;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. 虛擬滾動
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// 只渲染可視區域的臉部
|
||||||
|
const VirtualFaceGrid = ({ faces }: { faces: FaceMetadata[] }) => {
|
||||||
|
const [visibleRange, setVisibleRange] = useState({ start: 0, end: 50 });
|
||||||
|
|
||||||
|
// 使用 IntersectionObserver 偵測可視區域
|
||||||
|
useEffect(() => {
|
||||||
|
const observer = new IntersectionObserver((entries) => {
|
||||||
|
// 更新 visibleRange
|
||||||
|
});
|
||||||
|
|
||||||
|
// ...
|
||||||
|
}, [faces]);
|
||||||
|
|
||||||
|
const visibleFaces = faces.slice(visibleRange.start, visibleRange.end);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="face-grid">
|
||||||
|
{visibleFaces.map(face => (
|
||||||
|
<FaceCard key={face.face_id} face={face} />
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. JPG 懶載入
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// 懶載入臉部縮圖
|
||||||
|
const FaceCard = ({ face }: { face: FaceMetadata }) => {
|
||||||
|
const [thumbnailUrl, setThumbnailUrl] = useState<string | null>(null);
|
||||||
|
const [loading, setLoading] = useState(false);
|
||||||
|
|
||||||
|
// 當卡片進入可視區域時載入 JPG
|
||||||
|
useEffect(() => {
|
||||||
|
const observer = new IntersectionObserver((entries) => {
|
||||||
|
if (entries[0].isIntersecting && !thumbnailUrl && !loading) {
|
||||||
|
loadThumbnail();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
observer.observe(ref.current);
|
||||||
|
return () => observer.disconnect();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const loadThumbnail = async () => {
|
||||||
|
setLoading(true);
|
||||||
|
const response = await fetch(`/api/v1/file/${fileUuid}/face/${face.face_id}/thumbnail`);
|
||||||
|
const blob = await response.blob();
|
||||||
|
setThumbnailUrl(URL.createObjectURL(blob));
|
||||||
|
setLoading(false);
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="face-card">
|
||||||
|
{thumbnailUrl ? (
|
||||||
|
<img src={thumbnailUrl} />
|
||||||
|
) : loading ? (
|
||||||
|
<div className="thumbnail-placeholder">Loading...</div>
|
||||||
|
) : (
|
||||||
|
<div className="thumbnail-placeholder">Face {face.face_id}</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 錯誤處理
|
||||||
|
|
||||||
|
### 常見錯誤
|
||||||
|
|
||||||
|
**404 Not Found**:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"error": "Face not found"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
**原因**: 臉部 ID 不存在或檔案 UUID 不存在
|
||||||
|
|
||||||
|
**400 Bad Request**:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"error": "Invalid filter rules"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
**原因**: 過濾規則格式錯誤
|
||||||
|
|
||||||
|
**500 Internal Server Error**:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"error": "Database error"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
**原因**: 資料庫查詢失敗
|
||||||
|
|
||||||
|
### 錯誤處理範例
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const handleApiError = async (response: Response) => {
|
||||||
|
if (!response.ok) {
|
||||||
|
const error = await response.json();
|
||||||
|
|
||||||
|
switch (response.status) {
|
||||||
|
case 404:
|
||||||
|
console.error('Resource not found:', error.error);
|
||||||
|
break;
|
||||||
|
case 400:
|
||||||
|
console.error('Invalid request:', error.error);
|
||||||
|
break;
|
||||||
|
case 500:
|
||||||
|
console.error('Server error:', error.error);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
console.error('Unknown error:', error.error);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error(error.error);
|
||||||
|
}
|
||||||
|
|
||||||
|
return response.json();
|
||||||
|
};
|
||||||
|
|
||||||
|
// 使用範例
|
||||||
|
try {
|
||||||
|
const response = await fetch('/api/v1/file/0489e104d7d1e11e613da01f7aa130ca/faces');
|
||||||
|
const data = await handleApiError(response);
|
||||||
|
console.log(data);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to load faces:', error);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 最佳實踐
|
||||||
|
|
||||||
|
### 1. 使用預設規則開始
|
||||||
|
|
||||||
|
建議先使用預設規則,再根據實際需求調整。
|
||||||
|
|
||||||
|
### 2. 逐步調整閾值
|
||||||
|
|
||||||
|
不要一次性調整所有值,逐步調整並觀察結果。
|
||||||
|
|
||||||
|
### 3. 監控過濾統計
|
||||||
|
|
||||||
|
定期檢查過濾統計,確保過濾規則合理。
|
||||||
|
|
||||||
|
### 4. 保存多個規則配置
|
||||||
|
|
||||||
|
為不同場景保存不同的規則配置(如:保守、平衡、寬鬆)。
|
||||||
|
|
||||||
|
### 5. 使用者自訂
|
||||||
|
|
||||||
|
允許使用者自訂過濾規則,提供預設模板。
|
||||||
|
|
||||||
|
### 6. 即時預覽
|
||||||
|
|
||||||
|
提供即時預覽功能,讓使用者調整規則時立即看到結果。
|
||||||
|
|
||||||
|
### 7. 效能優化
|
||||||
|
|
||||||
|
- 使用虛擬滾動處理大量臉部
|
||||||
|
- 懶載入 JPG 縮圖
|
||||||
|
- 快取臉部元數據
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 支援與反饋
|
||||||
|
|
||||||
|
如有問題或建議,請聯繫 Core Team。
|
||||||
|
|
||||||
|
**文件版本**: v1.0
|
||||||
|
**最後更新**: 2026-08-09
|
||||||
|
**API 文件**: `/Users/accusys/momentry_core/docs_v1.0/API_WORKSPACE/modules/25_face_filter_api.md`
|
||||||
Reference in New Issue
Block a user