From 774ad4d82010ef350a6dc7d210007663884b42fd Mon Sep 17 00:00:00 2001 From: Accusys Date: Mon, 2 Nov 2026 15:52:11 +0800 Subject: [PATCH] feat: rust_store_traced_faces v2 schema - FaceTracedData with schema_version, stats, traces as Vec, TracePoint with timestamp/pose/landmarks --- .../rust/src/bin/rust_store_traced_faces.rs | 257 ++++++++++++++++-- 1 file changed, 236 insertions(+), 21 deletions(-) diff --git a/tools/cli/rust/src/bin/rust_store_traced_faces.rs b/tools/cli/rust/src/bin/rust_store_traced_faces.rs index c2fb549d..30c6136d 100644 --- a/tools/cli/rust/src/bin/rust_store_traced_faces.rs +++ b/tools/cli/rust/src/bin/rust_store_traced_faces.rs @@ -79,6 +79,23 @@ struct BBox { y: i32, width: i32, height: i32, + #[serde(default)] + x2: i32, + #[serde(default)] + y2: i32, + #[serde(default)] + center_x: i32, + #[serde(default)] + center_y: i32, +} + +impl BBox { + fn compute_derived(&mut self) { + self.x2 = self.x + self.width; + self.y2 = self.y + self.height; + self.center_x = self.x + self.width / 2; + self.center_y = self.y + self.height / 2; + } } #[derive(Debug, Serialize, Deserialize, Clone)] @@ -91,18 +108,75 @@ struct FaceFrame { #[derive(Debug, Serialize, Deserialize, Clone)] struct TraceInfo { trace_id: i32, + #[serde(default)] + frame_count: u64, + start_frame: u64, + end_frame: u64, + #[serde(default)] + start_time: f64, + #[serde(default)] + end_time: f64, + #[serde(default)] + avg_confidence: f64, + #[serde(default)] + avg_capture_quality: f64, + #[serde(default)] + best_frame: u64, + #[serde(default)] + best_confidence: f64, + #[serde(default)] + avg_bbox: BBox, + #[serde(default)] + face_instances: Vec, + // Legacy fields (keep for backward compatibility) + #[serde(default)] total_appearances: usize, - avg_confidence: f32, + #[serde(default)] path: Vec, - #[serde(skip_serializing_if = "Vec::is_empty")] + #[serde(default)] frames: Vec, } #[derive(Debug, Serialize, Deserialize, Clone)] struct TracePoint { frame: i64, + #[serde(default)] + timestamp: f64, bbox: BBox, confidence: f32, + #[serde(default)] + capture_quality: f32, + #[serde(skip_serializing_if = "Option::is_none")] + pose: Option, + #[serde(skip_serializing_if = "Option::is_none")] + landmarks: Option, +} + +#[derive(Debug, Serialize, Deserialize, Default)] +struct TraceStatsSummary { + #[serde(default)] + total_traces: usize, + #[serde(default)] + total_face_instances: usize, + #[serde(default)] + avg_trace_length: f64, +} + +#[derive(Debug, Serialize)] +struct FaceTracedData { + #[serde(default = "default_schema_version")] + schema_version: String, + fps: f64, + #[serde(default)] + file_uuid: Option, + status: String, + stats: TraceStatsSummary, + frames: Vec, + traces: Vec, +} + +fn default_schema_version() -> String { + "2.0".to_string() } #[derive(Debug, Serialize, Deserialize)] @@ -112,13 +186,6 @@ struct FaceData { frames: Vec, } -#[derive(Debug, Serialize)] -struct FaceTracedData { - fps: f64, - frames: Vec, - traces: HashMap, -} - /// Load existing trace_ids from face_traced.json if it exists. /// Returns a map of (frame, face_idx) -> trace_id. fn load_existing_trace_ids(traced_path: &std::path::Path) -> HashMap<(i64, usize), i32> { @@ -192,8 +259,14 @@ fn main() -> Result<()> { y: face.y.unwrap_or(0), width: face.width.unwrap_or(0), height: face.height.unwrap_or(0), + x2: 0, + y2: 0, + center_x: 0, + center_y: 0, }; } + // Compute derived bbox fields + face.bbox.compute_derived(); } } @@ -234,11 +307,103 @@ fn main() -> Result<()> { println!("[TRACE_STORE] No trace_ids to update"); } - // Create output + // Convert traces to v2 format + let traces_v2: Vec = traces + .into_iter() + .map(|(_, mut t)| { + // Compute derived bbox fields + for point in &mut t.path { + let mut bbox = point.bbox.clone(); + bbox.compute_derived(); + point.bbox = bbox; + } + for point in &mut t.frames { + let mut bbox = point.bbox.clone(); + bbox.compute_derived(); + point.bbox = bbox; + } + + // Calculate stats + let frame_count = t.path.len() as u64; + let start_frame = t.path.first().map(|p| p.frame as u64).unwrap_or(0); + let end_frame = t.path.last().map(|p| p.frame as u64).unwrap_or(0); + let start_time = start_frame as f64 / face_data.fps; + let end_time = end_frame as f64 / face_data.fps; + let avg_confidence = if !t.path.is_empty() { + t.path.iter().map(|p| p.confidence as f64).sum::() / t.path.len() as f64 + } else { + 0.0 + }; + let avg_capture_quality = if !t.path.is_empty() { + t.path.iter().map(|p| p.capture_quality as f64).sum::() / t.path.len() as f64 + } else { + 0.0 + }; + let best = t.path.iter().max_by(|a, b| a.confidence.partial_cmp(&b.confidence).unwrap()); + let best_frame = best.map(|p| p.frame as u64).unwrap_or(0); + let best_confidence = best.map(|p| p.confidence as f64).unwrap_or(0.0); + + // Calculate average bbox + let avg_bbox = if !t.path.is_empty() { + let mut bbox = BBox::default(); + for p in &t.path { + bbox.x += p.bbox.x; + bbox.y += p.bbox.y; + bbox.width += p.bbox.width; + bbox.height += p.bbox.height; + } + let n = t.path.len() as i32; + bbox.x /= n; + bbox.y /= n; + bbox.width /= n; + bbox.height /= n; + bbox.compute_derived(); + bbox + } else { + BBox::default() + }; + + TraceInfo { + trace_id: t.trace_id, + frame_count, + start_frame, + end_frame, + start_time, + end_time, + avg_confidence, + avg_capture_quality, + best_frame, + best_confidence, + avg_bbox, + face_instances: t.path.clone(), + total_appearances: t.total_appearances, + path: t.path, + frames: t.frames, + } + }) + .collect(); + + let total_traces = traces_v2.len(); + let total_face_instances: usize = traces_v2.iter().map(|t| t.frame_count as usize).sum(); + let avg_trace_length = if total_traces > 0 { + traces_v2.iter().map(|t| t.frame_count as f64).sum::() / total_traces as f64 + } else { + 0.0 + }; + + // Create output (v2 format) let traced_data = FaceTracedData { + schema_version: "2.0".to_string(), fps: face_data.fps, + file_uuid: Some(args.file_uuid.clone()), + status: "completed".to_string(), + stats: TraceStatsSummary { + total_traces, + total_face_instances, + avg_trace_length, + }, frames: face_data.frames, - traces, + traces: traces_v2, }; // Write face_traced.json @@ -287,12 +452,16 @@ fn run_face_tracking( // Qdrant may store bbox in two formats: // 1. Nested bbox field (from face_processor.py) // 2. Direct x, y, width, height fields (from rust_store_traced_faces) - let bbox_struct = if let Some(bbox_obj) = point.payload.get("bbox").and_then(|b| b.as_object()) { + let mut bbox_struct = if let Some(bbox_obj) = point.payload.get("bbox").and_then(|b| b.as_object()) { BBox { x: bbox_obj.get("x").and_then(|x| x.as_i64()).unwrap_or(0) as i32, y: bbox_obj.get("y").and_then(|y| y.as_i64()).unwrap_or(0) as i32, width: bbox_obj.get("width").and_then(|w| w.as_i64()).unwrap_or(0) as i32, height: bbox_obj.get("height").and_then(|h| h.as_i64()).unwrap_or(0) as i32, + x2: bbox_obj.get("x2").and_then(|x| x.as_i64()).unwrap_or(0) as i32, + y2: bbox_obj.get("y2").and_then(|y| y.as_i64()).unwrap_or(0) as i32, + center_x: bbox_obj.get("center_x").and_then(|x| x.as_i64()).unwrap_or(0) as i32, + center_y: bbox_obj.get("center_y").and_then(|y| y.as_i64()).unwrap_or(0) as i32, } } else { // Direct fields @@ -301,8 +470,13 @@ fn run_face_tracking( y: point.payload.get("y").and_then(|y| y.as_i64()).unwrap_or(0) as i32, width: point.payload.get("width").and_then(|w| w.as_i64()).unwrap_or(0) as i32, height: point.payload.get("height").and_then(|h| h.as_i64()).unwrap_or(0) as i32, + x2: 0, + y2: 0, + center_x: 0, + center_y: 0, } }; + bbox_struct.compute_derived(); // Debug: log first few embeddings if frame_embeddings.len() < 5 { @@ -340,21 +514,41 @@ fn run_face_tracking( let trace_key = existing_tid.to_string(); let trace = traces.entry(trace_key).or_insert(TraceInfo { trace_id: existing_tid, - total_appearances: 0, + frame_count: 0, + start_frame: 0, + end_frame: 0, + start_time: 0.0, + end_time: 0.0, avg_confidence: 0.0, + avg_capture_quality: 0.0, + best_frame: 0, + best_confidence: 0.0, + avg_bbox: BBox::default(), + face_instances: Vec::new(), + total_appearances: 0, path: Vec::new(), frames: Vec::new(), }); trace.total_appearances += 1; + let mut bbox = current_face.bbox.clone(); + bbox.compute_derived(); trace.path.push(TracePoint { frame: current_frame_num, - bbox: current_face.bbox.clone(), + timestamp: current_frame_num as f64 / face_data.fps, + bbox: bbox.clone(), confidence: current_face.confidence, + capture_quality: current_face.capture_quality.unwrap_or(0.0), + pose: current_face.pose.clone(), + landmarks: current_face.landmarks.clone(), }); trace.frames.push(TracePoint { frame: current_frame_num, - bbox: current_face.bbox.clone(), + timestamp: current_frame_num as f64 / face_data.fps, + bbox, confidence: current_face.confidence, + capture_quality: current_face.capture_quality.unwrap_or(0.0), + pose: current_face.pose.clone(), + landmarks: current_face.landmarks.clone(), }); continue; } @@ -423,22 +617,43 @@ fn run_face_tracking( let trace_key = trace_id.to_string(); let trace = traces.entry(trace_key).or_insert(TraceInfo { trace_id, - total_appearances: 0, + frame_count: 0, + start_frame: 0, + end_frame: 0, + start_time: 0.0, + end_time: 0.0, avg_confidence: 0.0, + avg_capture_quality: 0.0, + best_frame: 0, + best_confidence: 0.0, + avg_bbox: BBox::default(), + face_instances: Vec::new(), + total_appearances: 0, path: Vec::new(), frames: Vec::new(), }); trace.total_appearances += 1; + let mut bbox = current_face.bbox.clone(); + bbox.compute_derived(); + let timestamp = current_frame_num as f64 / face_data.fps; trace.path.push(TracePoint { frame: current_frame_num, - bbox: current_face.bbox.clone(), + timestamp, + bbox: bbox.clone(), confidence: current_face.confidence, + capture_quality: current_face.capture_quality.unwrap_or(0.0), + pose: current_face.pose.clone(), + landmarks: current_face.landmarks.clone(), }); trace.frames.push(TracePoint { frame: current_frame_num, - bbox: current_face.bbox.clone(), + timestamp, + bbox, confidence: current_face.confidence, + capture_quality: current_face.capture_quality.unwrap_or(0.0), + pose: current_face.pose.clone(), + landmarks: current_face.landmarks.clone(), }); } } @@ -455,8 +670,8 @@ fn run_face_tracking( // Calculate average confidence for each trace for trace in traces.values_mut() { if !trace.path.is_empty() { - let sum: f32 = trace.path.iter().map(|p| p.confidence).sum(); - trace.avg_confidence = sum / trace.path.len() as f32; + let sum: f64 = trace.path.iter().map(|p| p.confidence as f64).sum(); + trace.avg_confidence = sum / trace.path.len() as f64; } } @@ -464,7 +679,7 @@ fn run_face_tracking( if min_trace_len > 0 || min_confidence > 0.0 { traces.retain(|_, trace| { trace.total_appearances >= min_trace_len - && (min_confidence == 0.0 || trace.avg_confidence >= min_confidence) + && (min_confidence == 0.0 || trace.avg_confidence >= min_confidence as f64) }); }