feat: add bbox_size field for frontend filtering
- Database: Added bbox_size column (min(width, height)) to face_filter_features - API: Updated all endpoints to include bbox_size in responses - Documentation: Added bbox_size explanation and frontend filtering examples - Maintains current Swift implementation calculation method - Enables efficient frontend filtering without backend threshold This allows the frontend to do real-time filtering using bbox_size while the backend pre-computes and stores the value.
This commit is contained in:
@@ -53,6 +53,7 @@ Face Filter API 提供臉部過濾功能,讓 Studio 前端可以:
|
|||||||
"face_id": "face_001",
|
"face_id": "face_001",
|
||||||
"file_uuid": "0489e104d7d1e11e613da01f7aa130ca",
|
"file_uuid": "0489e104d7d1e11e613da01f7aa130ca",
|
||||||
"frame": 671,
|
"frame": 671,
|
||||||
|
"bbox_size": 336, // 新增:min(width, height)
|
||||||
"eye_ratio": 0.33,
|
"eye_ratio": 0.33,
|
||||||
"nose_ratio": 0.02,
|
"nose_ratio": 0.02,
|
||||||
"pose_score": 0.08,
|
"pose_score": 0.08,
|
||||||
@@ -485,6 +486,47 @@ console.log(`Saved rule: ${savedRule.rule_name} (id: ${savedRule.id})`);
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
### Bbox Size 說明
|
||||||
|
|
||||||
|
**欄位**: `bbox_size`
|
||||||
|
|
||||||
|
**計算方式**: `min(width, height)`
|
||||||
|
|
||||||
|
**說明**:
|
||||||
|
- 預先計算並儲存於後端
|
||||||
|
- 用於前端即時過濾
|
||||||
|
- 維持當前 Swift 實作的計算方式
|
||||||
|
|
||||||
|
**前端過濾範例**:
|
||||||
|
```typescript
|
||||||
|
// 前端即時過濾
|
||||||
|
const quickFilter = (faces, rules) => {
|
||||||
|
return faces.filter(face => {
|
||||||
|
// Bbox size 過濾
|
||||||
|
if (rules.min_bbox_size && face.filter_features.bbox_size < rules.min_bbox_size) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
// 使用範例
|
||||||
|
const filtered = quickFilter(allFaces, {
|
||||||
|
min_bbox_size: 80 // 最小 80 像素
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
**建議閾值**:
|
||||||
|
- 保守: 80 像素
|
||||||
|
- 平衡: 64 像素(當前預設)
|
||||||
|
- 寬鬆: 48 像素
|
||||||
|
|
||||||
|
**未來擴展**:
|
||||||
|
- 可考慮增加 `max(bbox.width, bbox.height)` 選項
|
||||||
|
- 可考慮增加 `bbox.width * bbox.height` (面積) 選項
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## 預設配置
|
## 預設配置
|
||||||
|
|
||||||
API 提供預設過濾規則,可直接使用:
|
API 提供預設過濾規則,可直接使用:
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
-- Migration: Add bbox_size to face_filter_features table
|
||||||
|
|
||||||
|
-- Add bbox_size column
|
||||||
|
ALTER TABLE face_filter_features
|
||||||
|
ADD COLUMN IF NOT EXISTS bbox_size INTEGER;
|
||||||
|
|
||||||
|
-- Add index for bbox_size filtering
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_face_filter_bbox_size ON face_filter_features(bbox_size);
|
||||||
|
|
||||||
|
-- Comment: bbox_size is pre-computed as min(width, height) for efficient frontend filtering
|
||||||
|
COMMENT ON COLUMN face_filter_features.bbox_size IS 'Pre-computed bbox size: min(width, height) in pixels';
|
||||||
+37
-33
@@ -155,7 +155,7 @@ pub async fn get_faces(
|
|||||||
// Get faces with filter features
|
// Get faces with filter features
|
||||||
let faces_query = format!(
|
let faces_query = format!(
|
||||||
"SELECT face_id, frame, primary_source, apple_vision_confidence, mediapipe_confidence,
|
"SELECT face_id, frame, primary_source, apple_vision_confidence, mediapipe_confidence,
|
||||||
eye_ratio, nose_ratio, pose_score, mediapipe_detected, mediapipe_iou,
|
bbox_size, eye_ratio, nose_ratio, pose_score, mediapipe_detected, mediapipe_iou,
|
||||||
embedding_similarity, consecutive_frames, total_frames_detected
|
embedding_similarity, consecutive_frames, total_frames_detected
|
||||||
FROM {}
|
FROM {}
|
||||||
WHERE file_uuid = $1
|
WHERE file_uuid = $1
|
||||||
@@ -181,6 +181,7 @@ pub async fn get_faces(
|
|||||||
let primary_source: Option<String> = row.get(2);
|
let primary_source: Option<String> = row.get(2);
|
||||||
let av_conf: Option<f64> = row.get(3);
|
let av_conf: Option<f64> = row.get(3);
|
||||||
let mp_conf: Option<f64> = row.get(4);
|
let mp_conf: Option<f64> = row.get(4);
|
||||||
|
let bbox_size: Option<i32> = row.get(5);
|
||||||
|
|
||||||
FaceInfo {
|
FaceInfo {
|
||||||
face_id: face_id.clone(),
|
face_id: face_id.clone(),
|
||||||
@@ -201,14 +202,15 @@ pub async fn get_faces(
|
|||||||
face_id,
|
face_id,
|
||||||
file_uuid: file_uuid.clone(),
|
file_uuid: file_uuid.clone(),
|
||||||
frame,
|
frame,
|
||||||
eye_ratio: row.get(5),
|
bbox_size,
|
||||||
nose_ratio: row.get(6),
|
eye_ratio: row.get(6),
|
||||||
pose_score: row.get(7),
|
nose_ratio: row.get(7),
|
||||||
mediapipe_detected: row.get(8),
|
pose_score: row.get(8),
|
||||||
mediapipe_iou: row.get(9),
|
mediapipe_detected: row.get(9),
|
||||||
embedding_similarity: row.get(10),
|
mediapipe_iou: row.get(10),
|
||||||
consecutive_frames: row.get(11),
|
embedding_similarity: row.get(11),
|
||||||
total_frames_detected: row.get(12),
|
consecutive_frames: row.get(12),
|
||||||
|
total_frames_detected: row.get(13),
|
||||||
apple_vision_confidence: av_conf,
|
apple_vision_confidence: av_conf,
|
||||||
mediapipe_confidence: mp_conf,
|
mediapipe_confidence: mp_conf,
|
||||||
primary_source,
|
primary_source,
|
||||||
@@ -264,7 +266,7 @@ pub async fn get_face_filter_report(
|
|||||||
let filter_table = schema::table_name("face_filter_features");
|
let filter_table = schema::table_name("face_filter_features");
|
||||||
|
|
||||||
let row = sqlx::query(&format!(
|
let row = sqlx::query(&format!(
|
||||||
"SELECT face_id, frame, eye_ratio, nose_ratio, pose_score,
|
"SELECT face_id, frame, bbox_size, eye_ratio, nose_ratio, pose_score,
|
||||||
mediapipe_detected, mediapipe_iou, embedding_similarity,
|
mediapipe_detected, mediapipe_iou, embedding_similarity,
|
||||||
consecutive_frames, total_frames_detected,
|
consecutive_frames, total_frames_detected,
|
||||||
apple_vision_confidence, mediapipe_confidence, primary_source
|
apple_vision_confidence, mediapipe_confidence, primary_source
|
||||||
@@ -284,17 +286,18 @@ pub async fn get_face_filter_report(
|
|||||||
face_id: row.get(0),
|
face_id: row.get(0),
|
||||||
file_uuid: file_uuid.clone(),
|
file_uuid: file_uuid.clone(),
|
||||||
frame: row.get(1),
|
frame: row.get(1),
|
||||||
eye_ratio: row.get(2),
|
bbox_size: row.get(2),
|
||||||
nose_ratio: row.get(3),
|
eye_ratio: row.get(3),
|
||||||
pose_score: row.get(4),
|
nose_ratio: row.get(4),
|
||||||
mediapipe_detected: row.get(5),
|
pose_score: row.get(5),
|
||||||
mediapipe_iou: row.get(6),
|
mediapipe_detected: row.get(6),
|
||||||
embedding_similarity: row.get(7),
|
mediapipe_iou: row.get(7),
|
||||||
consecutive_frames: row.get(8),
|
embedding_similarity: row.get(8),
|
||||||
total_frames_detected: row.get(9),
|
consecutive_frames: row.get(9),
|
||||||
apple_vision_confidence: row.get(10),
|
total_frames_detected: row.get(10),
|
||||||
mediapipe_confidence: row.get(11),
|
apple_vision_confidence: row.get(11),
|
||||||
primary_source: row.get(12),
|
mediapipe_confidence: row.get(12),
|
||||||
|
primary_source: row.get(13),
|
||||||
};
|
};
|
||||||
|
|
||||||
// Apply default filter rules
|
// Apply default filter rules
|
||||||
@@ -319,7 +322,7 @@ pub async fn filter_faces(
|
|||||||
|
|
||||||
// Get all faces for this file
|
// Get all faces for this file
|
||||||
let rows = sqlx::query(&format!(
|
let rows = sqlx::query(&format!(
|
||||||
"SELECT face_id, frame, eye_ratio, nose_ratio, pose_score,
|
"SELECT face_id, frame, bbox_size, eye_ratio, nose_ratio, pose_score,
|
||||||
mediapipe_detected, mediapipe_iou, embedding_similarity,
|
mediapipe_detected, mediapipe_iou, embedding_similarity,
|
||||||
consecutive_frames, total_frames_detected,
|
consecutive_frames, total_frames_detected,
|
||||||
apple_vision_confidence, mediapipe_confidence, primary_source
|
apple_vision_confidence, mediapipe_confidence, primary_source
|
||||||
@@ -349,17 +352,18 @@ pub async fn filter_faces(
|
|||||||
face_id: row.get(0),
|
face_id: row.get(0),
|
||||||
file_uuid: file_uuid.clone(),
|
file_uuid: file_uuid.clone(),
|
||||||
frame: row.get(1),
|
frame: row.get(1),
|
||||||
eye_ratio: row.get(2),
|
bbox_size: row.get(2),
|
||||||
nose_ratio: row.get(3),
|
eye_ratio: row.get(3),
|
||||||
pose_score: row.get(4),
|
nose_ratio: row.get(4),
|
||||||
mediapipe_detected: row.get(5),
|
pose_score: row.get(5),
|
||||||
mediapipe_iou: row.get(6),
|
mediapipe_detected: row.get(6),
|
||||||
embedding_similarity: row.get(7),
|
mediapipe_iou: row.get(7),
|
||||||
consecutive_frames: row.get(8),
|
embedding_similarity: row.get(8),
|
||||||
total_frames_detected: row.get(9),
|
consecutive_frames: row.get(9),
|
||||||
apple_vision_confidence: row.get(10),
|
total_frames_detected: row.get(10),
|
||||||
mediapipe_confidence: row.get(11),
|
apple_vision_confidence: row.get(11),
|
||||||
primary_source: row.get(12),
|
mediapipe_confidence: row.get(12),
|
||||||
|
primary_source: row.get(13),
|
||||||
};
|
};
|
||||||
|
|
||||||
let result = engine.filter_face(&features).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
let result = engine.filter_face(&features).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||||
|
|||||||
@@ -9,6 +9,9 @@ pub struct FaceFilterFeatures {
|
|||||||
pub file_uuid: String,
|
pub file_uuid: String,
|
||||||
pub frame: i32,
|
pub frame: i32,
|
||||||
|
|
||||||
|
// Bbox size (pre-computed as min(width, height))
|
||||||
|
pub bbox_size: Option<i32>,
|
||||||
|
|
||||||
// Landmark geometric features
|
// Landmark geometric features
|
||||||
pub eye_ratio: Option<f64>,
|
pub eye_ratio: Option<f64>,
|
||||||
pub nose_ratio: Option<f64>,
|
pub nose_ratio: Option<f64>,
|
||||||
|
|||||||
Reference in New Issue
Block a user