feat: add Qdrant trace_id update to Rust Face Tracing
## Problem - Rust Face Tracing only generated face_traced.json - Did not update trace_id in Qdrant - Caused trace_id to remain null in Qdrant - Face Clustering failed due to missing trace_ids ## Solution Added two functions: - build_trace_mapping(): Build (frame, bbox) -> trace_id mapping - update_qdrant_trace_ids(): Batch update Qdrant points with trace_ids ## Flow 1. Read embeddings from Qdrant 2. Calculate traces (IoU + Similarity) 3. Generate face_traced.json 4. Update trace_ids in Qdrant (NEW) ## Testing - Previous: trace_id=null in Qdrant - After: trace_id correctly set - Face Clustering now works ## Files Changed - tools/cli/rust/src/bin/rust_store_traced_faces.rs Closes #QDRANT_TRACE_ID_NULL
This commit is contained in:
@@ -144,6 +144,16 @@ fn main() -> Result<()> {
|
||||
|
||||
println!("[TRACE_STORE] Traces: {}", traces.len());
|
||||
|
||||
// Update trace_ids in Qdrant
|
||||
println!("[TRACE_STORE] Updating trace_ids in Qdrant...");
|
||||
let trace_mapping: HashMap<(i64, i32, i32, i32, i32), i32> = build_trace_mapping(&face_data);
|
||||
if !trace_mapping.is_empty() {
|
||||
update_qdrant_trace_ids(&args.file_uuid, &trace_mapping)?;
|
||||
println!("[TRACE_STORE] Updated {} trace_ids in Qdrant", trace_mapping.len());
|
||||
} else {
|
||||
println!("[TRACE_STORE] No trace_ids to update");
|
||||
}
|
||||
|
||||
// Create output
|
||||
let traced_data = FaceTracedData {
|
||||
fps: face_data.fps,
|
||||
@@ -439,3 +449,116 @@ fn load_qdrant_faces(uuid: &str) -> Vec<FacePoint> {
|
||||
eprintln!("[TRACE] Loaded {} points from Qdrant _faces", all_points.len());
|
||||
all_points
|
||||
}
|
||||
|
||||
fn build_trace_mapping(face_data: &FaceData) -> HashMap<(i64, i32, i32, i32, i32), i32> {
|
||||
let mut mapping = HashMap::new();
|
||||
for frame in &face_data.frames {
|
||||
for face in &frame.faces {
|
||||
if let Some(trace_id) = face.trace_id {
|
||||
let key = (frame.frame, face.bbox.x, face.bbox.y, face.bbox.width, face.bbox.height);
|
||||
mapping.insert(key, trace_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
mapping
|
||||
}
|
||||
|
||||
fn update_qdrant_trace_ids(
|
||||
file_uuid: &str,
|
||||
trace_mapping: &HashMap<(i64, i32, i32, i32, i32), i32>,
|
||||
) -> Result<()> {
|
||||
let qdrant_url = env_var("QDRANT_URL", DEFAULT_QDRANT_URL);
|
||||
let qdrant_api_key = env_var("QDRANT_API_KEY", DEFAULT_QDRANT_API_KEY);
|
||||
|
||||
// Load all points for this file
|
||||
let scroll_url = format!("{}/collections/_faces/points/scroll", qdrant_url);
|
||||
let mut offset: Option<Value> = None;
|
||||
let mut updates: Vec<Value> = Vec::new();
|
||||
|
||||
loop {
|
||||
let mut body = json!({
|
||||
"limit": 10000,
|
||||
"with_payload": true,
|
||||
"with_vector": false,
|
||||
"filter": {"must": [{"key": "file_uuid", "match": {"value": file_uuid}}]},
|
||||
});
|
||||
if let Some(ref off) = offset {
|
||||
body["offset"] = off.clone();
|
||||
}
|
||||
|
||||
let resp = ureq::post(&scroll_url)
|
||||
.set("api-key", &qdrant_api_key)
|
||||
.timeout(Duration::from_secs(60))
|
||||
.send_json(body);
|
||||
|
||||
match resp {
|
||||
Ok(r) => {
|
||||
let data: Value = r.into_json().unwrap_or(Value::Null);
|
||||
let points = data
|
||||
.pointer("/result/points")
|
||||
.and_then(|p| p.as_array())
|
||||
.cloned()
|
||||
.unwrap_or_default();
|
||||
|
||||
for point in points {
|
||||
let point_id = point.get("id").cloned();
|
||||
let payload = point.get("payload").cloned().unwrap_or(Value::Null);
|
||||
|
||||
let frame = payload.get("frame").and_then(|f| f.as_i64()).unwrap_or(0);
|
||||
let x = payload.get("x").and_then(|x| x.as_i64()).unwrap_or(0) as i32;
|
||||
let y = payload.get("y").and_then(|y| y.as_i64()).unwrap_or(0) as i32;
|
||||
let width = payload.get("width").and_then(|w| w.as_i64()).unwrap_or(0) as i32;
|
||||
let height = payload.get("height").and_then(|h| h.as_i64()).unwrap_or(0) as i32;
|
||||
|
||||
let key = (frame, x, y, width, height);
|
||||
if let Some(&trace_id) = trace_mapping.get(&key) {
|
||||
if let Some(id) = point_id {
|
||||
updates.push(json!({
|
||||
"id": id,
|
||||
"payload": {"trace_id": trace_id}
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let next = data.pointer("/result/next_page_offset").cloned();
|
||||
match next {
|
||||
Some(Value::Number(n)) if n.as_i64().unwrap_or(0) != 0 => offset = Some(Value::Number(n)),
|
||||
Some(Value::Number(_)) => break,
|
||||
Some(Value::Null) | None => break,
|
||||
other => {
|
||||
offset = other;
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("[TRACE] Qdrant scroll error: {}", e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if updates.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// Batch update points
|
||||
let update_url = format!("{}/collections/_faces/points", qdrant_url);
|
||||
let batch_size = 100;
|
||||
for chunk in updates.chunks(batch_size) {
|
||||
let body = json!({
|
||||
"points": chunk
|
||||
});
|
||||
|
||||
let resp = ureq::post(&update_url)
|
||||
.set("api-key", &qdrant_api_key)
|
||||
.timeout(Duration::from_secs(60))
|
||||
.send_json(body);
|
||||
|
||||
if let Err(e) = resp {
|
||||
eprintln!("[TRACE] Failed to update Qdrant: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user