From b4b84dc79707964ff3ecf5e2423fa7d0bfa03786 Mon Sep 17 00:00:00 2001 From: Accusys Date: Tue, 17 Nov 2026 10:56:42 +0800 Subject: [PATCH] feat: composite scoring for best face selection (0.4 frontal + 0.4 confidence + 0.2 quality) --- tools/cli/rust/src/bin/rust_face_cluster.rs | 76 +++++++++++++++++---- 1 file changed, 63 insertions(+), 13 deletions(-) diff --git a/tools/cli/rust/src/bin/rust_face_cluster.rs b/tools/cli/rust/src/bin/rust_face_cluster.rs index 0eeba156..e251fd48 100644 --- a/tools/cli/rust/src/bin/rust_face_cluster.rs +++ b/tools/cli/rust/src/bin/rust_face_cluster.rs @@ -891,9 +891,40 @@ fn main() { return; } - // Person face counts + best face (representative). + // Person face counts + best face (representative) using composite scoring. + // Composite score: 0.4 × frontal + 0.4 × confidence + 0.2 × quality let mut person_face_count: BTreeMap = BTreeMap::new(); let mut person_best_face: BTreeMap = BTreeMap::new(); + let mut person_best_score: BTreeMap = BTreeMap::new(); + + // Pre-compute min/max for normalization within each person + let mut person_stats: BTreeMap = BTreeMap::new(); + for (frame_idx, frame_obj) in frames_list.iter().enumerate() { + let frame_num = frame_obj.get("frame") + .and_then(|f| f.as_u64()) + .unwrap_or(frame_idx as u64); + let faces = frame_obj.get("faces").and_then(|f| f.as_array()).cloned().unwrap_or_default(); + for face in faces.iter() { + let person_id = face.get("person_id").and_then(|p| p.as_str()); + if let Some(pid) = person_id { + let conf = face.get("confidence").and_then(|c| c.as_f64()).unwrap_or(0.5); + let quality = face.get("captureQuality").and_then(|c| c.as_f64()).unwrap_or(0.0); + let pose = face.get("pose_angle").or_else(|| face.get("pose")).and_then(|p| p.as_object()); + let yaw_rad = pose.and_then(|p| p.get("yaw").and_then(|y| y.as_f64())).unwrap_or(0.0); + let yaw_deg = yaw_rad * 57.2958; + let frontal = (1.0 - yaw_deg.abs() / 60.0).max(0.0); + + let entry = person_stats.entry(pid.to_string()).or_insert((conf, conf, quality, quality, frontal, frontal)); + entry.0 = entry.0.min(conf); + entry.1 = entry.1.max(conf); + entry.2 = entry.2.min(quality); + entry.3 = entry.3.max(quality); + entry.4 = entry.4.min(frontal); + entry.5 = entry.5.max(frontal); + } + } + } + for (frame_idx, frame_obj) in frames_list.iter().enumerate() { let frame_num = frame_obj.get("frame") .and_then(|f| f.as_u64()) @@ -903,18 +934,37 @@ fn main() { let person_id = face.get("person_id").and_then(|p| p.as_str()); if let Some(person_id) = person_id { *person_face_count.entry(person_id.to_string()).or_insert(0) += 1; - let confidence = face.get("confidence").and_then(|c| c.as_f64()).unwrap_or(0.9); - let entry = person_best_face - .entry(person_id.to_string()) - .or_insert_with(|| { - json!({ - "confidence": f64::NEG_INFINITY, - }) - }); - if confidence > entry.get("confidence").and_then(|c| c.as_f64()).unwrap_or(f64::NEG_INFINITY) { - *entry = json!({ + + // Calculate composite score + let conf = face.get("confidence").and_then(|c| c.as_f64()).unwrap_or(0.5); + let quality = face.get("captureQuality").and_then(|c| c.as_f64()).unwrap_or(0.0); + let pose = face.get("pose_angle").or_else(|| face.get("pose")).and_then(|p| p.as_object()); + let yaw_rad = pose.and_then(|p| p.get("yaw").and_then(|y| y.as_f64())).unwrap_or(0.0); + let yaw_deg = yaw_rad * 57.2958; + let frontal = (1.0 - yaw_deg.abs() / 60.0).max(0.0); + + // Normalize within person + let stats = person_stats.get(person_id).unwrap(); + let conf_range = stats.1 - stats.0; + let quality_range = stats.3 - stats.2; + let frontal_range = stats.5 - stats.4; + + let conf_norm = if conf_range > 0.001 { (conf - stats.0) / conf_range } else { 0.5 }; + let quality_norm = if quality_range > 0.001 { (quality - stats.2) / quality_range } else { 0.5 }; + let frontal_norm = if frontal_range > 0.001 { (frontal - stats.4) / frontal_range } else { 0.5 }; + + // Composite: 0.4 frontal + 0.4 confidence + 0.2 quality + let composite_score = 0.4 * frontal_norm + 0.4 * conf_norm + 0.2 * quality_norm; + + let current_best = person_best_score.get(person_id).copied().unwrap_or(-1.0); + if composite_score > current_best { + person_best_score.insert(person_id.to_string(), composite_score); + person_best_face.insert(person_id.to_string(), json!({ "face_id": format!("face_{}_{}", frame_num, face_idx), - "confidence": confidence, + "confidence": conf, + "capture_quality": quality, + "yaw_deg": round_digits(yaw_deg, 2), + "composite_score": round_digits(composite_score, 4), "frame": frame_num, "bbox": { "x": face.get("x").cloned().unwrap_or(Value::Null), @@ -922,7 +972,7 @@ fn main() { "width": face.get("width").cloned().unwrap_or(Value::Null), "height": face.get("height").cloned().unwrap_or(Value::Null), } - }); + })); } } }