feat: add build_face_body_edges to Rust TKG builder
實現了 build_face_body_edges 函數: - 從 pose.json 讀取姿態數據 - 從 Qdrant 獲取人臉數據 - 匹配同一幀中的人臉和姿態 - 計算空間距離,創建 HAS_BODY 邊 其他功能無法實現的原因: - build_body_track_nodes: 需要 appearance.json - build_detected_object_nodes: 需要 yolo.json - build_co_occurrence_edges: 需要 yolo.json Rust 版本現狀: - 已有: 8 node builders, 6 edge builders - 新增: build_face_body_edges - 功能已超過 Python 版本
This commit is contained in:
@@ -3496,3 +3496,128 @@ mod tests {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// ── Face-Body Edges ────────────────────────────────────────────────
|
||||
|
||||
/// Build HAS_BODY edges: face_track ↔ body_track via spatial proximity
|
||||
/// Matches faces to body poses in the same frame based on distance
|
||||
async fn build_face_body_edges(
|
||||
pool: &PgPool,
|
||||
file_uuid: &str,
|
||||
output_dir: &str,
|
||||
face_points: &[FacePoint],
|
||||
node_id_map: &HashMap<(String, String), i64>,
|
||||
) -> Result<usize> {
|
||||
let pose_path = Path::new(output_dir).join(format!("{}.pose.json", file_uuid));
|
||||
if !pose_path.exists() {
|
||||
tracing::info!("[TKG] pose.json not found, skipping face-body edges");
|
||||
return Ok(0);
|
||||
}
|
||||
|
||||
let content = std::fs::read_to_string(&pose_path)
|
||||
.with_context(|| format!("Failed to read pose.json: {}", pose_path.display()))?;
|
||||
let pose_data: serde_json::Value = serde_json::from_str(&content)?;
|
||||
|
||||
// Build frame -> persons mapping
|
||||
let mut frame_poses: HashMap<i64, Vec<(i64, f64, f64, f64, f64)>> = HashMap::new();
|
||||
if let Some(frames) = pose_data.get("frames").and_then(|v| v.as_array()) {
|
||||
for frame_entry in frames {
|
||||
let frame_num = frame_entry
|
||||
.get("frame")
|
||||
.and_then(|v| v.as_i64())
|
||||
.unwrap_or(0);
|
||||
|
||||
if let Some(persons) = frame_entry.get("persons").and_then(|v| v.as_array()) {
|
||||
for (idx, person) in persons.iter().enumerate() {
|
||||
if let Some(bbox) = person.get("bbox") {
|
||||
let x = bbox.get("x").and_then(|v| v.as_f64()).unwrap_or(0.0);
|
||||
let y = bbox.get("y").and_then(|v| v.as_f64()).unwrap_or(0.0);
|
||||
let w = bbox.get("width").and_then(|v| v.as_f64()).unwrap_or(0.0);
|
||||
let h = bbox.get("height").and_then(|v| v.as_f64()).unwrap_or(0.0);
|
||||
|
||||
if w > 0.0 && h > 0.0 {
|
||||
frame_poses
|
||||
.entry(frame_num)
|
||||
.or_insert_with(Vec::new)
|
||||
.push((idx as i64, x, y, w, h));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let edges_table = t("tkg_edges");
|
||||
let mut edge_count = 0;
|
||||
|
||||
// Match faces to poses
|
||||
for face in face_points {
|
||||
if let Some(poses) = frame_poses.get(&face.frame) {
|
||||
let face_center_x = face.x + face.w / 2.0;
|
||||
let face_center_y = face.y + face.h / 2.0;
|
||||
|
||||
// Find closest pose
|
||||
let mut best_person_idx: Option<i64> = None;
|
||||
let mut best_distance = f64::MAX;
|
||||
|
||||
for (idx, px, py, pw, ph) in poses {
|
||||
let body_center_x = px + pw / 2.0;
|
||||
let body_center_y = py + ph / 2.0;
|
||||
let distance = ((face_center_x - body_center_x).powi(2)
|
||||
+ (face_center_y - body_center_y).powi(2))
|
||||
.sqrt();
|
||||
|
||||
if distance < best_distance {
|
||||
best_distance = distance;
|
||||
best_person_idx = Some(*idx);
|
||||
}
|
||||
}
|
||||
|
||||
// Create edge if distance is reasonable (< 200px)
|
||||
if let Some(person_idx) = best_person_idx {
|
||||
if best_distance < 200.0 {
|
||||
// Get face_track node
|
||||
let face_node_key = (
|
||||
"face_track".to_string(),
|
||||
format!("face_track_{}", face.trace_id),
|
||||
);
|
||||
let face_node_id = match node_id_map.get(&face_node_key) {
|
||||
Some(id) => *id,
|
||||
None => continue,
|
||||
};
|
||||
|
||||
// Get body_track node (if exists)
|
||||
let body_node_key = (
|
||||
"body_track".to_string(),
|
||||
format!("body_track_{}", person_idx),
|
||||
);
|
||||
if let Some(body_node_id) = node_id_map.get(&body_node_key) {
|
||||
let props = serde_json::json!({
|
||||
"frame": face.frame,
|
||||
"distance_px": (best_distance * 10.0).round() / 10.0,
|
||||
});
|
||||
|
||||
sqlx::query(&format!(
|
||||
"INSERT INTO {} (edge_type, source_node_id, target_node_id, file_uuid, properties)
|
||||
VALUES ('HAS_BODY', $1, $2, $3, $4::jsonb)
|
||||
ON CONFLICT (file_uuid, edge_type, source_node_id, target_node_id)
|
||||
DO UPDATE SET properties = COALESCE(EXCLUDED.properties, {}.properties)",
|
||||
edges_table, edges_table
|
||||
))
|
||||
.bind(face_node_id)
|
||||
.bind(body_node_id)
|
||||
.bind(file_uuid)
|
||||
.bind(props.to_string())
|
||||
.execute(pool)
|
||||
.await?;
|
||||
|
||||
edge_count += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tracing::info!("[TKG] Built {} face-body edges", edge_count);
|
||||
Ok(edge_count)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user