fix: add min_capture_quality to FaceFilterSettings + trace profile fixes

- Add min_capture_quality to FaceFilterSettings (file_profile.rs)
- Fix store_traced_faces.py: load embeddings from Qdrant with_vector=True
- Fix face_tracker.py: force new traces when frame_gap > 30
- Fix backfill_trace_profiles.py: key_frame select=eq(n,N), bbox scaling, best_frame
- Add best_frame to FaceTraceProfile (Rust + Python)
- Fix min_capture_quality persistence in file-profile API
This commit is contained in:
Accusys
2026-08-13 10:12:21 +08:00
parent 71172fa354
commit 39e9ce4c3b
25 changed files with 723 additions and 131 deletions
+7
View File
@@ -107,8 +107,10 @@ mod tests {
#[test]
fn test_asr_result_serialization() {
let result = AsrResult {
status: None,
language: Some("en".to_string()),
language_probability: Some(0.95),
segment_count: 2,
segments: vec![
AsrSegment {
start_time: 0.0,
@@ -116,6 +118,7 @@ mod tests {
start_frame: Some(0),
end_frame: Some(60),
text: "Hello world".to_string(),
confidence: 0.95,
},
AsrSegment {
start_time: 2.5,
@@ -123,6 +126,7 @@ mod tests {
start_frame: Some(60),
end_frame: Some(120),
text: "Test speech".to_string(),
confidence: 0.9,
},
],
};
@@ -157,6 +161,7 @@ mod tests {
start_frame: Some(0),
end_frame: Some(24),
text: String::new(),
confidence: 0.0,
};
assert_eq!(segment.start_time, 0.0);
assert_eq!(segment.end_time, 1.0);
@@ -166,9 +171,11 @@ mod tests {
#[test]
fn test_asr_result_empty_segments() {
let result = AsrResult {
status: None,
language: None,
language_probability: None,
segments: vec![],
segment_count: 0,
};
assert!(result.language.is_none());
assert!(result.segments.is_empty());
+4
View File
@@ -214,6 +214,7 @@ mod tests {
#[test]
fn test_asrx_result_serialization() {
let result = AsrxResult {
status: None,
language: Some("en".to_string()),
segments: vec![AsrxSegment {
start_time: 0.0,
@@ -227,6 +228,7 @@ mod tests {
quality: None,
}],
embeddings: None,
segment_count: 1,
};
let json = serde_json::to_string(&result).unwrap();
@@ -255,9 +257,11 @@ mod tests {
#[test]
fn test_asrx_result_empty_segments() {
let result = AsrxResult {
status: None,
language: None,
segments: vec![],
embeddings: None,
segment_count: 0,
};
assert!(result.segments.is_empty());
assert!(result.language.is_none());
+9
View File
@@ -53,6 +53,8 @@ pub struct Face {
pub width: i32,
pub height: i32,
pub confidence: f32,
#[serde(default)]
pub capture_quality: Option<f32>,
pub embedding: Option<Vec<f32>>,
pub landmarks: Option<serde_json::Value>,
pub pose_angle: Option<serde_json::Value>,
@@ -148,8 +150,10 @@ mod tests {
#[test]
fn test_face_result_serialization() {
let result = FaceResult {
status: None,
frame_count: 100,
fps: 30.0,
total_faces: 1,
frames: vec![FaceFrame {
frame: 0,
timestamp: 0.0,
@@ -160,6 +164,7 @@ mod tests {
width: 50,
height: 60,
confidence: 0.95,
capture_quality: Some(0.9),
embedding: Some(vec![0.1, 0.2, 0.3]),
landmarks: Some(serde_json::json!([[10.0, 20.0], [30.0, 40.0]])),
pose_angle: None,
@@ -216,9 +221,11 @@ mod tests {
#[test]
fn test_face_result_empty_frames() {
let result = FaceResult {
status: None,
frame_count: 0,
fps: 0.0,
frames: vec![],
total_faces: 0,
};
assert!(result.frames.is_empty());
}
@@ -232,6 +239,7 @@ mod tests {
width: 10,
height: 10,
confidence: 0.5,
capture_quality: None,
embedding: None,
landmarks: None,
pose_angle: None,
@@ -249,6 +257,7 @@ mod tests {
width: 50,
height: 60,
confidence: 0.95,
capture_quality: Some(0.8),
embedding: Some(vec![0.1; 512]),
landmarks: None,
pose_angle: None,
+73 -10
View File
@@ -20,6 +20,7 @@ struct FacePoint {
w: f64,
h: f64,
confidence: f64,
capture_quality: f64,
point_id: Option<String>,
}
@@ -121,6 +122,7 @@ fn parse_face_points(points: Vec<serde_json::Value>) -> Vec<FacePoint> {
w,
h,
confidence: payload["confidence"].as_f64().unwrap_or(0.5),
capture_quality: payload["capture_quality"].as_f64().unwrap_or(0.0),
point_id: p["id"].as_u64().map(|id| id.to_string()),
})
})
@@ -1134,10 +1136,16 @@ async fn build_face_track_nodes_from_qdrant(
sum_w: f64,
sum_h: f64,
sum_confidence: f64,
sum_capture_quality: f64,
frames: Vec<(i64, f64, f64, f64, f64)>, // (frame, x, y, w, h)
best_frame: i64,
best_confidence: f64,
best_point_id: Option<String>,
cq_best_frame: i64,
cq_best_quality: f64,
cq_best_point_id: Option<String>,
cq_best_bbox: Option<serde_json::Value>,
best_bbox: Option<serde_json::Value>,
}
let mut traces: HashMap<i64, TraceAgg> = HashMap::new();
@@ -1151,10 +1159,16 @@ async fn build_face_track_nodes_from_qdrant(
sum_w: 0.0,
sum_h: 0.0,
sum_confidence: 0.0,
sum_capture_quality: 0.0,
frames: vec![],
best_frame: 0,
best_confidence: 0.0,
best_point_id: None,
cq_best_frame: 0,
cq_best_quality: 0.0,
cq_best_point_id: None,
cq_best_bbox: None,
best_bbox: None,
});
entry.frame_count += 1;
entry.start_f = entry.start_f.min(p.frame);
@@ -1164,12 +1178,31 @@ async fn build_face_track_nodes_from_qdrant(
entry.sum_w += p.w;
entry.sum_h += p.h;
entry.sum_confidence += p.confidence;
entry.sum_capture_quality += p.capture_quality;
entry.frames.push((p.frame, p.x, p.y, p.w, p.h));
// Track best face (highest confidence)
if p.confidence > entry.best_confidence {
entry.best_confidence = p.confidence;
entry.best_frame = p.frame;
entry.best_point_id = p.point_id.clone();
entry.best_bbox = Some(serde_json::json!({
"x": p.x.round() as i64,
"y": p.y.round() as i64,
"width": p.w.round() as i64,
"height": p.h.round() as i64,
}));
}
// Track best capture quality face (used for key frame / key face set)
if p.capture_quality > entry.cq_best_quality {
entry.cq_best_quality = p.capture_quality;
entry.cq_best_frame = p.frame;
entry.cq_best_point_id = p.point_id.clone();
entry.cq_best_bbox = Some(serde_json::json!({
"x": p.x.round() as i64,
"y": p.y.round() as i64,
"width": p.w.round() as i64,
"height": p.h.round() as i64,
}));
}
}
@@ -1210,24 +1243,34 @@ async fn build_face_track_nodes_from_qdrant(
0.0
};
let avg_capture_quality = if agg.frame_count > 0 {
agg.sum_capture_quality / agg.frame_count as f64
} else {
0.0
};
let props = serde_json::json!({
"trace_id": tid,
"frame_count": agg.frame_count,
"start_frame": agg.start_f,
"end_frame": agg.end_f,
"key_frame": agg.best_frame,
"key_face": agg.best_point_id.as_deref().unwrap_or(""),
"key_frame": agg.cq_best_frame,
"key_face": agg.cq_best_point_id.as_deref().unwrap_or(""),
"avg_bbox": {
"x": (agg.sum_x / agg.frame_count as f64).round() as i64,
"y": (agg.sum_y / agg.frame_count as f64).round() as i64,
"width": (agg.sum_w / agg.frame_count as f64).round() as i64,
"height": (agg.sum_h / agg.frame_count as f64).round() as i64,
},
"best_bbox": agg.cq_best_bbox,
"avg_yaw": (avg_yaw * 1000.0).round() / 1000.0,
"avg_pitch": (avg_pitch * 1000.0).round() / 1000.0,
"avg_roll": (avg_roll * 1000.0).round() / 1000.0,
"pose_count": pose_count,
"avg_confidence": (avg_confidence * 1000.0).round() / 1000.0,
"best_confidence": (agg.best_confidence * 1000.0).round() / 1000.0,
"avg_capture_quality": (avg_capture_quality * 1000.0).round() / 1000.0,
"best_capture_quality": (agg.cq_best_quality * 1000.0).round() / 1000.0,
"status": "pending",
});
@@ -1277,13 +1320,19 @@ async fn build_face_track_nodes_from_qdrant(
agg.frame_count,
agg.start_f,
agg.end_f,
agg.cq_best_frame,
Some(avg_confidence),
Some(avg_capture_quality),
agg.best_confidence,
agg.cq_best_quality,
props["avg_bbox"].clone(),
agg.cq_best_bbox.clone(),
);
// Extract key_frame JPEG bytes via ffmpeg pipe
// Use select=eq(n\,frame) for VFR compatibility (decoded index, not time-based)
let key_frame_jpeg: Option<Vec<u8>> = video_path.as_ref().and_then(|vp| {
let select = format!("select=eq(n\\,{})", agg.best_frame);
let select = format!("select=eq(n\\,{})", agg.cq_best_frame);
let output = std::process::Command::new("ffmpeg")
.args(["-y", "-v", "quiet", "-i"])
.arg(vp)
@@ -1311,13 +1360,13 @@ async fn build_face_track_nodes_from_qdrant(
}
});
// Crop key_face JPEG bytes from key_frame using representative bbox (best confidence frame)
// Crop key_face JPEG bytes from key_frame using representative bbox (best capture quality frame)
let key_face_jpeg: Option<Vec<u8>> = key_frame_jpeg.as_ref().and_then(|kf| {
// Find bbox for best_frame from trace frames
// Find bbox for cq_best_frame from trace frames
let (bf, bx, by, bw, bh) = agg
.frames
.iter()
.find(|(f, _, _, _, _)| *f == agg.best_frame)?;
.find(|(f, _, _, _, _)| *f == agg.cq_best_frame)?;
let (bx, by, bw, bh) = (*bx, *by, *bw, *bh);
// Validate bbox: must have positive dimensions
@@ -1364,8 +1413,8 @@ async fn build_face_track_nodes_from_qdrant(
});
// Save all three files to output/{file_uuid}/{trace_id}/
let mut key_frame_file = agg.best_frame.to_string();
let mut key_face_file = agg.best_point_id.as_deref().unwrap_or("").to_string();
let mut key_frame_file = agg.cq_best_frame.to_string();
let mut key_face_file = agg.cq_best_point_id.as_deref().unwrap_or("").to_string();
match trace_profile.save(
output_dir,
@@ -1416,7 +1465,7 @@ async fn build_face_track_nodes_from_qdrant(
if need_retry {
if let Some(ref vp) = video_path {
let select = format!("select=eq(n\\,{})", agg.best_frame);
let select = format!("select=eq(n\\,{})", agg.cq_best_frame);
// Retry key_frame extraction
if !kf_path.exists() {
if let Ok(output) = std::process::Command::new("ffmpeg")
@@ -1455,7 +1504,7 @@ async fn build_face_track_nodes_from_qdrant(
let (_, bx, by, bw, bh) = agg
.frames
.iter()
.find(|(f, _, _, _, _)| *f == agg.best_frame)
.find(|(f, _, _, _, _)| *f == agg.cq_best_frame)
.cloned()
.unwrap_or((0, 0.0, 0.0, 100.0, 100.0));
@@ -1533,10 +1582,15 @@ async fn build_face_track_nodes_from_qdrant(
"key_frame": key_frame_file,
"key_face": key_face_file,
"avg_bbox": props["avg_bbox"].clone(),
"best_bbox": props["best_bbox"].clone(),
"avg_yaw": props["avg_yaw"].clone(),
"avg_pitch": props["avg_pitch"].clone(),
"avg_roll": props["avg_roll"].clone(),
"pose_count": pose_count,
"avg_confidence": props["avg_confidence"].clone(),
"best_confidence": props["best_confidence"].clone(),
"avg_capture_quality": props["avg_capture_quality"].clone(),
"best_capture_quality": props["best_capture_quality"].clone(),
"status": "pending",
});
@@ -3601,6 +3655,7 @@ mod tests {
w: 50.0,
h: 50.0,
confidence: 0.9,
capture_quality: 0.0,
point_id: None,
},
FacePoint {
@@ -3611,6 +3666,7 @@ mod tests {
w: 50.0,
h: 50.0,
confidence: 0.9,
capture_quality: 0.0,
point_id: None,
},
FacePoint {
@@ -3621,6 +3677,7 @@ mod tests {
w: 50.0,
h: 50.0,
confidence: 0.9,
capture_quality: 0.0,
point_id: None,
},
FacePoint {
@@ -3631,6 +3688,7 @@ mod tests {
w: 50.0,
h: 50.0,
confidence: 0.9,
capture_quality: 0.0,
point_id: None,
},
];
@@ -3661,6 +3719,7 @@ mod tests {
w: 100.0,
h: 100.0,
confidence: 0.9,
capture_quality: 0.0,
point_id: None,
},
FacePoint {
@@ -3671,6 +3730,7 @@ mod tests {
w: 100.0,
h: 100.0,
confidence: 0.9,
capture_quality: 0.0,
point_id: None,
},
];
@@ -3692,6 +3752,7 @@ mod tests {
w: 100.0,
h: 100.0,
confidence: 0.9,
capture_quality: 0.0,
point_id: None,
},
FacePoint {
@@ -3702,6 +3763,7 @@ mod tests {
w: 100.0,
h: 100.0,
confidence: 0.9,
capture_quality: 0.0,
point_id: None,
},
];
@@ -3722,6 +3784,7 @@ mod tests {
w: 100.0,
h: 100.0,
confidence: 0.9,
capture_quality: 0.0,
point_id: None,
}];
let index = build_frame_index(&points);