diff --git a/scripts/store_traced_faces.py b/scripts/store_traced_faces.py index 1ee7a707..3125526c 100644 --- a/scripts/store_traced_faces.py +++ b/scripts/store_traced_faces.py @@ -128,6 +128,7 @@ def run_face_tracker( print(f"[TRACE] Loading embeddings from Qdrant for {file_uuid}...") try: from qdrant_faces import get_file_faces + qdrant_points = get_file_faces(file_uuid, with_vector=True) # Build lookup: (frame, x, y) -> embedding emb_lookup = {} @@ -181,7 +182,9 @@ def run_face_tracker( face_data["metadata"] = metadata with open(traced_json_path, "w") as f: - json.dump(strip_embeddings_and_round(face_data), f, indent=2, ensure_ascii=False) + json.dump( + strip_embeddings_and_round(face_data), f, indent=2, ensure_ascii=False + ) trace_count = len(face_data.get("traces", {})) print(f"[TRACE] Completed: {trace_count} traces -> {traced_json_path}") @@ -210,27 +213,20 @@ def store_traced_faces(file_uuid: str, traced_json_path: str, schema: str = SCHE bbox_key = f"{face['x']}_{face['y']}_{face['width']}_{face['height']}" trace_mapping[frame_num][bbox_key] = trace_id - # Update Qdrant _faces collection with trace_id + # Update Qdrant _faces collection with trace_id. + # return_trace_ids=True makes update_trace_ids also return the trace_id set seen + # during its scroll, so we avoid a redundant third full-collection scan below. + trace_ids = set() try: - qdrant_updated = update_trace_ids(file_uuid, trace_mapping) + qdrant_updated, trace_ids = update_trace_ids( + file_uuid, trace_mapping, return_trace_ids=True + ) except Exception as e: print(f"[TRACE] Warning: Qdrant trace_id update failed: {e}") qdrant_updated = 0 - # Count unique traces from Qdrant - try: - from qdrant_faces import get_file_faces - - points = get_file_faces(file_uuid) - trace_ids = set() - for p in points: - tid = p.get("payload", {}).get("trace_id") - if tid is not None and tid > 0: - trace_ids.add(tid) - qdrant_trace_count = len(trace_ids) - except Exception as e: - print(f"[TRACE] Warning: Qdrant trace count failed: {e}") - qdrant_trace_count = 0 + # Unique trace count from the update scroll (no extra full scan needed). + qdrant_trace_count = len(trace_ids) total_faces = sum( 1 diff --git a/scripts/utils/qdrant_faces.py b/scripts/utils/qdrant_faces.py index 0d52bf0d..25336649 100644 --- a/scripts/utils/qdrant_faces.py +++ b/scripts/utils/qdrant_faces.py @@ -35,7 +35,9 @@ QDRANT_API_KEY = os.environ.get("QDRANT_API_KEY", "Test3200Test3200Test3200") FACES_COLLECTION = "_faces" SEEDS_COLLECTION = "_seeds" VECTOR_DIM = 512 -BATCH_SIZE = int(os.environ.get("QDRANT_BATCH_SIZE", "100")) +# Default 1000 (was 100): larger pages cut the number of scroll/set_payload HTTP +# round-trips ~10x on large files (B2). Still overridable via QDRANT_BATCH_SIZE. +BATCH_SIZE = int(os.environ.get("QDRANT_BATCH_SIZE", "1000")) def qdrant_request(method: str, path: str, body: dict = None) -> dict: @@ -158,7 +160,9 @@ def push_face_embeddings_batch(file_uuid: str, faces: list, publisher=None) -> i return pushed -def update_trace_ids(file_uuid: str, trace_mapping: dict) -> int: +def update_trace_ids( + file_uuid: str, trace_mapping: dict, return_trace_ids: bool = False +): """Update trace_id for all face points in a file Called by store_traced_faces.py after face tracking. @@ -168,9 +172,12 @@ def update_trace_ids(file_uuid: str, trace_mapping: dict) -> int: file_uuid: Video file UUID trace_mapping: {frame: {bbox_key: trace_id}} bbox_key = f"{x}_{y}_{width}_{height}" + return_trace_ids: If True, also return the set of trace_ids seen during the + scroll. This lets the caller avoid a redundant full collection scan just + to count unique traces (B1 optimization for large files). Returns: - Number of updated points + Number of updated points, or (updated_count, trace_id_set) if return_trace_ids. """ all_points = [] offset = None @@ -217,7 +224,7 @@ def update_trace_ids(file_uuid: str, trace_mapping: dict) -> int: trace_groups[trace_id].append(point_id) if not trace_groups: - return 0 + return (0, set()) if return_trace_ids else 0 # Use set_payload per trace_id group (no vector write) total = 0 @@ -228,6 +235,8 @@ def update_trace_ids(file_uuid: str, trace_mapping: dict) -> int: total += len(batch_ids) print(f"[QDRANT] Updated {total} trace_ids in {FACES_COLLECTION}") + if return_trace_ids: + return total, set(trace_groups.keys()) return total