Files
momentry_core/scripts/insert_face_detections.py
Accusys 2cfcfdd1af feat: Phase 2.6 edges migration to Qdrant (TKG-only architecture)
Phase 2.6.1: co_occurrence_edges migration
- build_co_occurrence_edges_from_qdrant()
- Qdrant embeddings → frame grouping → YOLO objects
- Result: 6679 edges (vs 6701 PostgreSQL)

Phase 2.6.2: face_face_edges migration
- build_face_face_edges_from_qdrant()
- Qdrant embeddings → frame grouping → face pairs
- mutual_gaze detection preserved
- Result: 6 edges (exact match)

Phase 2.6.3: speaker_face_edges migration
- build_speaker_face_edges_from_qdrant()
- Qdrant embeddings → trace_id frame ranges
- SPEAKS_AS edge creation

Architecture:
- All edges use Qdrant payload (no face_detections queries)
- PostgreSQL fallback for empty Qdrant
- Estimated 3.6x performance improvement

Testing:
- Playground (3003): ✓ All Phase 2.6 logs verified
- Edge counts: ✓ Close match with PostgreSQL
- Fallback: ✓ Working

Docs:
- docs_v1.0/DESIGN/TKG_PHASE2_6_EDGES_MIGRATION.md
- docs_v1.0/M4_workspace/2026-06-21_phase2_6_test.md
2026-06-21 04:47:49 +08:00

82 lines
2.6 KiB
Python

#!/opt/homebrew/bin/python3.11
"""Insert face detections from traced JSON into DB."""
import json, os, sys
import psycopg2
import psycopg2.extras
DB_URL = os.environ.get("DATABASE_URL", "postgresql://accusys@localhost:5432/momentry")
def insert_faces(file_uuid, traced_json_path, schema):
conn = psycopg2.connect(DB_URL)
cur = conn.cursor()
with open(traced_json_path) as f:
data = json.load(f)
frames = data.get("frames", {})
metadata = data.get("metadata", {})
fps = metadata.get("fps", 24.0)
total = 0
for frame_num_str, frame_data in sorted(frames.items(), key=lambda x: int(x[0])):
frame_num = int(frame_num_str)
ts = frame_num / fps
faces = frame_data.get("faces", [])
for face in faces:
x = int(face.get("x", 0))
y = int(face.get("y", 0))
w = int(face.get("width", 0))
h = int(face.get("height", 0))
confidence = face.get("confidence", 0.0)
trace_id = face.get("trace_id")
embedding = face.get("embedding")
face_id = face.get("face_id")
try:
cur.execute(
f"""
INSERT INTO {schema}.face_detections
(file_uuid, frame_number, timestamp_secs, face_id, x, y, width, height, confidence, trace_id, embedding)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
ON CONFLICT DO NOTHING
""",
(
file_uuid,
frame_num,
ts,
face_id,
x,
y,
w,
h,
confidence,
trace_id,
embedding,
),
)
if cur.rowcount > 0:
total += 1
except Exception as e:
print(f"[INSERT] Error at frame {frame_num}: {e}")
conn.rollback()
conn.commit()
cur.close()
conn.close()
print(f"[INSERT] Inserted {total} face detections into {schema}.face_detections")
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Insert face detections")
parser.add_argument("--file-uuid", required=True)
parser.add_argument("--face-json", required=True)
parser.add_argument("--schema", default="public")
args = parser.parse_args()
insert_faces(args.file_uuid, args.face_json, args.schema)