#!/usr/bin/env python3 """ Batch restore faces from face.json to Qdrant Usage: python3 batch_restore_faces.py [--dry-run] """ import os import sys import json import requests from pathlib import Path QDRANT_URL = os.environ.get("QDRANT_URL", "http://127.0.0.1:6333") QDRANT_API_KEY = os.environ.get("QDRANT_API_KEY", "Test3200Test3200Test3200") OUTPUT_DIR = os.environ.get("MOMENTRY_OUTPUT_DIR", "/Users/accusys/momentry/output") def count_faces_in_qdrant(file_uuid): """Count faces in Qdrant for a file""" response = requests.post( f"{QDRANT_URL}/collections/_faces/points/count", headers={"api-key": QDRANT_API_KEY}, json={ "filter": { "must": [ {"key": "file_uuid", "match": {"value": file_uuid}} ] } } ) if response.status_code == 200: return response.json()['result']['count'] return 0 def restore_faces_from_json(file_uuid, dry_run=False): """Restore faces from face.json to Qdrant""" face_json_path = os.path.join(OUTPUT_DIR, f"{file_uuid}.face.json") if not os.path.exists(face_json_path): print(f" [SKIP] face.json not found") return False # Check if already has faces in Qdrant current_count = count_faces_in_qdrant(file_uuid) if current_count > 0: print(f" [SKIP] Already has {current_count} faces in Qdrant") return True # Load face.json with open(face_json_path, 'r') as f: face_data = json.load(f) # Check if valid if face_data.get('status') == 'no_faces': print(f" [SKIP] No faces in file") return True frames = face_data.get('frames', []) if not frames: print(f" [SKIP] No frames in face.json") return False # Count valid faces valid_count = 0 invalid_count = 0 for frame in frames: faces = frame.get('faces', []) for face in faces: w = face.get('width', 0) h = face.get('height', 0) if w > 0 and h > 0 and face.get('embedding'): valid_count += 1 else: invalid_count += 1 print(f" [INFO] Faces: {valid_count} valid, {invalid_count} invalid") if valid_count == 0: print(f" [SKIP] No valid faces") return False if dry_run: print(f" [DRY-RUN] Would restore {valid_count} faces") return True # TODO: Implement actual Qdrant storage print(f" [TODO] Implement Qdrant storage") return False def main(): dry_run = '--dry-run' in sys.argv if dry_run: print("[INFO] DRY-RUN mode - no changes will be made") # Find all face.json files output_dir = Path(OUTPUT_DIR) face_files = list(output_dir.glob("*.face.json")) print(f"\n[INFO] Found {len(face_files)} face.json files") print("\n[INFO] Processing files (first 20):") restored = 0 skipped = 0 failed = 0 for i, face_file in enumerate(face_files[:20]): # Extract file_uuid (remove .face.json) filename = face_file.name file_uuid = filename.replace('.face.json', '') print(f"\n{i+1}. {file_uuid[:20]}...:") result = restore_faces_from_json(file_uuid, dry_run) if result: restored += 1 else: failed += 1 print(f"\n[INFO] Summary (first 20):") print(f" Restored: {restored}") print(f" Failed: {failed}") if dry_run: print("\n[INFO] Run without --dry-run to actually restore") if __name__ == "__main__": main()