fix: face pipeline 三項修復
1. face_clustering: 先試 Rust binary,Python script 缺失不再提前返回空結果 2. face.rs: facenet model 查找加 fallback ( -> momentry_core/models),config 路徑補 --model 參數 3. rust_store_traced_faces: embedding 缺失時退回純 IoU 追蹤(不再強制要求 similarity);trace_profile.json 補上真實 avg_bbox/best_bbox/capture_quality
This commit is contained in:
@@ -349,9 +349,64 @@ fn extract_full_frame(video_path: &str, frame_num: usize, output: &std::path::Pa
|
||||
fn write_trace_profile(trace: &TraceInfo, file_uuid: &str, output: &std::path::Path) {
|
||||
use std::io::Write;
|
||||
|
||||
let best_confidence = trace.best_confidence;
|
||||
let avg_confidence = trace.avg_confidence;
|
||||
let best_frame = trace.best_frame;
|
||||
// Compute real stats from face instances (fallback to path when instances empty)
|
||||
let instances: &Vec<TracePoint> = if !trace.face_instances.is_empty() {
|
||||
&trace.face_instances
|
||||
} else {
|
||||
&trace.path
|
||||
};
|
||||
|
||||
let avg_confidence = if !instances.is_empty() {
|
||||
instances.iter().map(|p| p.confidence as f64).sum::<f64>() / instances.len() as f64
|
||||
} else {
|
||||
trace.avg_confidence
|
||||
};
|
||||
|
||||
let best = instances.iter().max_by(|a, b| {
|
||||
a.confidence
|
||||
.partial_cmp(&b.confidence)
|
||||
.unwrap_or(std::cmp::Ordering::Equal)
|
||||
});
|
||||
|
||||
let best_frame = best.map(|p| p.frame as u64).unwrap_or(trace.best_frame as u64);
|
||||
let best_confidence = best.map(|p| p.confidence as f64).unwrap_or(trace.best_confidence);
|
||||
|
||||
let best_capture_quality = instances
|
||||
.iter()
|
||||
.map(|p| p.capture_quality as f64)
|
||||
.fold(0.0f64, |acc, q| if q > acc { q } else { acc });
|
||||
let avg_capture_quality = if !instances.is_empty() {
|
||||
instances.iter().map(|p| p.capture_quality as f64).sum::<f64>() / instances.len() as f64
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
|
||||
let best_bbox = best.map(|p| {
|
||||
serde_json::json!({
|
||||
"x": p.bbox.x, "y": p.bbox.y,
|
||||
"width": p.bbox.width, "height": p.bbox.height,
|
||||
"x2": p.bbox.x2, "y2": p.bbox.y2,
|
||||
"center_x": p.bbox.center_x, "center_y": p.bbox.center_y,
|
||||
})
|
||||
});
|
||||
|
||||
let avg_bbox = if !instances.is_empty() {
|
||||
let mut b = trace.avg_bbox.clone();
|
||||
let n = instances.len() as i32;
|
||||
b.x = instances.iter().map(|p| p.bbox.x).sum::<i32>() / n;
|
||||
b.y = instances.iter().map(|p| p.bbox.y).sum::<i32>() / n;
|
||||
b.width = instances.iter().map(|p| p.bbox.width).sum::<i32>() / n;
|
||||
b.height = instances.iter().map(|p| p.bbox.height).sum::<i32>() / n;
|
||||
b.compute_derived();
|
||||
serde_json::json!({
|
||||
"x": b.x, "y": b.y,
|
||||
"width": b.width, "height": b.height,
|
||||
"x2": b.x2, "y2": b.y2,
|
||||
"center_x": b.center_x, "center_y": b.center_y,
|
||||
})
|
||||
} else {
|
||||
serde_json::Value::Null
|
||||
};
|
||||
|
||||
let profile = serde_json::json!({
|
||||
"version": "1.0",
|
||||
@@ -364,10 +419,10 @@ fn write_trace_profile(trace: &TraceInfo, file_uuid: &str, output: &std::path::P
|
||||
"best_frame": best_frame,
|
||||
"avg_confidence": avg_confidence,
|
||||
"best_confidence": best_confidence,
|
||||
"avg_capture_quality": null,
|
||||
"best_capture_quality": null,
|
||||
"avg_bbox": null,
|
||||
"best_bbox": null,
|
||||
"avg_capture_quality": avg_capture_quality,
|
||||
"best_capture_quality": best_capture_quality,
|
||||
"avg_bbox": avg_bbox,
|
||||
"best_bbox": best_bbox,
|
||||
"key_frame": "key_frame.jpg",
|
||||
"key_face": "key_face.jpg",
|
||||
"status": "pending",
|
||||
@@ -789,11 +844,19 @@ fn run_face_tracking(
|
||||
(Some(curr), Some(prev)) => calculate_cosine_similarity(curr, prev),
|
||||
_ => 0.0,
|
||||
};
|
||||
|
||||
// Match if IoU AND similarity meet thresholds
|
||||
if iou > best_iou
|
||||
&& iou > iou_threshold
|
||||
&& similarity > similarity_threshold {
|
||||
|
||||
// Match if IoU AND similarity meet thresholds.
|
||||
// When embeddings are unavailable (either face missing its
|
||||
// vector), fall back to IoU-only geometric continuation:
|
||||
// consecutive frames of the same face have overlapping bboxes.
|
||||
let matched = if current_embedding.is_none() || prev_embedding.is_none() {
|
||||
iou > best_iou && iou > iou_threshold
|
||||
} else {
|
||||
iou > best_iou
|
||||
&& iou > iou_threshold
|
||||
&& similarity > similarity_threshold
|
||||
};
|
||||
if matched {
|
||||
best_iou = iou;
|
||||
best_match = Some((prev_trace_id, iou, similarity));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user