feat: Face JSON v2 schema - add schema_version, bbox x2/y2/center, keep embeddings, update clustering

This commit is contained in:
Accusys
2026-11-02 14:34:02 +08:00
parent 2fca49b120
commit 5d18183d82
566 changed files with 321788 additions and 1662 deletions
+4 -4
View File
@@ -808,10 +808,10 @@ fn main() {
// Filter low-quality clusters (hand detections, isolated faces)
// Filter criteria:
// - face_count < 20 (too few faces, likely isolated or hand)
// - bbox_area > 150,000 (abnormally large bbox, likely hand/object)
let min_face_count = 20;
let max_bbox_area = 150_000.0;
// - face_count < 5 (too few faces, likely isolated or hand)
// - bbox_area > 200,000 (abnormally large bbox, likely hand/object)
let min_face_count = 3;
let max_bbox_area = 200_000.0;
let mut filtered_cluster_ids: std::collections::HashSet<String> = std::collections::HashSet::new();
let mut filtered_clusters: Vec<Value> = Vec::new();
@@ -48,7 +48,18 @@ struct FacePoint {
struct FaceDetection {
#[serde(skip_serializing_if = "Option::is_none")]
trace_id: Option<i32>,
// FIX: Add flat bbox fields for consistency with face.json
#[serde(default, skip_serializing_if = "Option::is_none")]
x: Option<i32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
y: Option<i32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
width: Option<i32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
height: Option<i32>,
confidence: f32,
// FIX: Make bbox optional during deserialization, will be filled later if missing
#[serde(default)]
bbox: BBox,
#[serde(skip_serializing_if = "Option::is_none")]
embedding: Option<Vec<f32>>,
@@ -62,7 +73,7 @@ struct FaceDetection {
capture_quality: Option<f32>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
struct BBox {
x: i32,
y: i32,
@@ -171,6 +182,21 @@ fn main() -> Result<()> {
let mut face_data: FaceData = serde_json::from_str(&face_json_str)
.with_context(|| "Failed to parse face.json")?;
// FIX: Ensure all faces have bbox by constructing from flat fields if missing
for frame in &mut face_data.frames {
for face in &mut frame.faces {
if face.bbox.x == 0 && face.bbox.y == 0 && face.bbox.width == 0 && face.bbox.height == 0 {
// bbox is empty, construct from flat fields
face.bbox = BBox {
x: face.x.unwrap_or(0),
y: face.y.unwrap_or(0),
width: face.width.unwrap_or(0),
height: face.height.unwrap_or(0),
};
}
}
}
println!("[TRACE_STORE] Frames: {}", face_data.frames.len());
// Load embeddings from Qdrant
@@ -258,12 +284,24 @@ fn run_face_tracking(
.and_then(|c| c.as_f64())
.unwrap_or(0.9) as f32;
// Qdrant stores x, y, width, height directly (not in bbox object)
let bbox_struct = BBox {
x: point.payload.get("x").and_then(|x| x.as_i64()).unwrap_or(0) as i32,
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,
// 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()) {
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,
}
} else {
// Direct fields
BBox {
x: point.payload.get("x").and_then(|x| x.as_i64()).unwrap_or(0) as i32,
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,
}
};
// Debug: log first few embeddings
@@ -592,10 +630,23 @@ fn update_qdrant_trace_ids(
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;
// Support two bbox formats
let (x, y, width, height) = if let Some(bbox_obj) = payload.get("bbox").and_then(|b| b.as_object()) {
(
bbox_obj.get("x").and_then(|x| x.as_i64()).unwrap_or(0) as i32,
bbox_obj.get("y").and_then(|y| y.as_i64()).unwrap_or(0) as i32,
bbox_obj.get("width").and_then(|w| w.as_i64()).unwrap_or(0) as i32,
bbox_obj.get("height").and_then(|h| h.as_i64()).unwrap_or(0) as i32,
)
} else {
(
payload.get("x").and_then(|x| x.as_i64()).unwrap_or(0) as i32,
payload.get("y").and_then(|y| y.as_i64()).unwrap_or(0) as i32,
payload.get("width").and_then(|w| w.as_i64()).unwrap_or(0) as i32,
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) {
+16 -3
View File
@@ -37,7 +37,9 @@ from qdrant_faces import push_face_embeddings_batch
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
SWIFT_BIN = os.path.join(SCRIPT_DIR, "swift_processors", ".build", "release", "swift_face")
SWIFT_BIN_DEBUG = os.path.join(SCRIPT_DIR, "swift_processors", ".build", "debug", "swift_face")
FACENET_PATH = os.path.join(SCRIPT_DIR, "..", "models", "facenet512.mlpackage")
# FIX: Correct path to facenet512.mlpackage (project root models/)
# SCRIPT_DIR = tools/script/face/, so we need ../../.. to reach project root
FACENET_PATH = os.path.join(SCRIPT_DIR, "..", "..", "..", "models", "facenet512.mlpackage")
# Pose angle classification from roll/yaw
def classify_pose(roll: float, yaw: float) -> str:
@@ -120,11 +122,17 @@ class FaceProcessorVision:
input_array = np.expand_dims(normalized, axis=0)
result = self.coreml_model.predict({"input": input_array})
# Find output key (var_xxx)
emb_key = [k for k in result.keys() if k.startswith("var_")][0]
var_keys = [k for k in result.keys() if k.startswith("var_")]
if not var_keys:
print(f"[FACE_V2] WARNING: No var_ output key found, keys={list(result.keys())}", file=sys.stderr)
return None
emb_key = var_keys[0]
emb = result[emb_key].flatten().tolist()
return emb
except Exception as e:
print(f"[FACE_V2] Embedding error: {e}")
import traceback
print(f"[FACE_V2] Embedding error: {e}", file=sys.stderr)
print(f"[FACE_V2] Traceback: {traceback.format_exc()}", file=sys.stderr)
return None
def process_with_swift(self) -> Dict:
@@ -252,19 +260,24 @@ class FaceProcessorVision:
self.video.set(cv2.CAP_PROP_POS_FRAMES, frame_num)
ret, frame = self.video.read()
if not ret:
print(f"[FACE_V2] WARNING: video.read() failed at frame {frame_num}", file=sys.stderr)
continue
# Crop face
x1, y1 = max(0, x), max(0, y)
x2, y2 = min(self.width, x + w), min(self.height, y + h)
if x2 <= x1 or y2 <= y1:
print(f"[FACE_V2] WARNING: invalid crop at frame {frame_num}: bbox={bb}", file=sys.stderr)
continue
face_img = frame[y1:y2, x1:x2]
if face_img.size == 0:
print(f"[FACE_V2] WARNING: empty crop at frame {frame_num}", file=sys.stderr)
continue
# CoreML embedding - push to Qdrant _faces collection
emb = self.extract_face_embedding(face_img)
if emb is None:
print(f"[FACE_V2] WARNING: embedding failed at frame {frame_num}", file=sys.stderr)
if emb is not None:
embed_count += 1
# Collect for batch Qdrant push