Files

208 lines
6.4 KiB
Bash
Executable File

#!/bin/bash
# Sync face JSON files to SQLite and Qdrant
# Usage: ./scripts/sync_face_data.sh [file_uuid]
OUTPUT_DIR="${MOMENTRY_OUTPUT_DIR:-/Users/accusys/momentry/output}"
sync_face_data() {
local uuid=$1
local face_json="$OUTPUT_DIR/${uuid}.face.json"
local traced_json="$OUTPUT_DIR/${uuid}.face_traced.json"
local cluster_json="$OUTPUT_DIR/${uuid}.face_cluster.json"
local trace_dir="$OUTPUT_DIR/${uuid}"
if [ ! -f "$face_json" ]; then
echo "⚠️ face.json not found for $uuid"
return
fi
echo "🔄 Syncing $uuid..."
# Create face_operations.sqlite
python3 << 'PYEOF'
import json
import sqlite3
import os
import sys
from datetime import datetime
uuid = sys.argv[1] if len(sys.argv) > 1 else ""
output_dir = os.environ.get("MOMENTRY_OUTPUT_DIR", "/Users/accusys/momentry/output")
face_json = f"{output_dir}/{uuid}.face.json"
traced_json = f"{output_dir}/{uuid}.face_traced.json"
cluster_json = f"{output_dir}/{uuid}.face_cluster.json"
trace_dir = f"{output_dir}/{uuid}"
db_path = f"{trace_dir}/face_operations.sqlite"
# Ensure directory exists
os.makedirs(trace_dir, exist_ok=True)
# Connect to SQLite
conn = sqlite3.connect(db_path)
c = conn.cursor()
# Create tables
c.executescript("""
CREATE TABLE IF NOT EXISTS processing_status (
file_uuid TEXT PRIMARY KEY,
detection_done BOOLEAN DEFAULT FALSE,
traces_done BOOLEAN DEFAULT FALSE,
clusters_done BOOLEAN DEFAULT FALSE,
vlm_done BOOLEAN DEFAULT FALSE,
last_updated DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS face_groups (
id INTEGER PRIMARY KEY AUTOINCREMENT,
file_uuid TEXT NOT NULL,
group_name TEXT NOT NULL,
trace_ids TEXT,
total_faces INTEGER DEFAULT 0,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
UNIQUE(file_uuid, group_name)
);
CREATE TABLE IF NOT EXISTS trace_custom (
id INTEGER PRIMARY KEY AUTOINCREMENT,
file_uuid TEXT NOT NULL,
trace_id INTEGER NOT NULL,
label TEXT,
group_name TEXT,
notes TEXT,
custom_properties TEXT,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
UNIQUE(file_uuid, trace_id)
);
CREATE TABLE IF NOT EXISTS group_operations (
id INTEGER PRIMARY KEY AUTOINCREMENT,
file_uuid TEXT NOT NULL,
operation TEXT NOT NULL,
before_json TEXT,
after_json TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
""")
# Update processing status
now = datetime.now().isoformat()
# Check face.json
if os.path.exists(face_json):
c.execute("INSERT OR REPLACE INTO processing_status (file_uuid, detection_done, last_updated) VALUES (?, ?, ?)",
(uuid, True, now))
print(" ✅ processing_status: detection_done=True")
# Check face_traced.json
if os.path.exists(traced_json):
c.execute("UPDATE processing_status SET traces_done = ?, last_updated = ? WHERE file_uuid = ?",
(True, now, uuid))
print(" ✅ processing_status: traces_done=True")
# Load traces and insert into trace_custom
with open(traced_json) as f:
traced = json.load(f)
for trace in traced.get('traces', []):
trace_id = trace.get('trace_id')
if trace_id is not None:
# Check if trace_profile.json exists
tp_path = f"{trace_dir}/trace_{trace_id}/trace_profile.json"
label = None
group_name = None
if os.path.exists(tp_path):
with open(tp_path) as tf:
tp = json.load(tf)
label = tp.get('label') or tp.get('name')
c.execute("""
INSERT INTO trace_custom (file_uuid, trace_id, label, updated_at)
VALUES (?, ?, ?, ?)
ON CONFLICT(file_uuid, trace_id) DO UPDATE SET
label = ?, updated_at = ?
""", (uuid, trace_id, label, now, label, now))
print(f" ✅ trace_custom: {len(traced.get('traces', []))} traces synced")
# Check face_cluster.json
if os.path.exists(cluster_json):
c.execute("UPDATE processing_status SET clusters_done = ?, last_updated = ? WHERE file_uuid = ?",
(True, now, uuid))
print(" ✅ processing_status: clusters_done=True")
# Load clusters and insert into face_groups
with open(cluster_json) as f:
cluster = json.load(f)
for cl in cluster.get('clusters', []):
group_name = cl.get('group_name', f"Person_{cl.get('cluster_id', 0)}")
trace_ids = json.dumps(cl.get('trace_ids', []))
total_faces = cl.get('total_faces', 0)
c.execute("""
INSERT OR IGNORE INTO face_groups (file_uuid, group_name, trace_ids, total_faces)
VALUES (?, ?, ?, ?)
""", (uuid, group_name, trace_ids, total_faces))
print(f" ✅ face_groups: {len(cluster.get('clusters', []))} groups synced")
# Check VLM (trace_profile.json with vlm_summary)
vlm_done = False
if os.path.exists(trace_dir):
for td in os.listdir(trace_dir):
tp_path = f"{trace_dir}/{td}/trace_profile.json"
if os.path.exists(tp_path):
with open(tp_path) as f:
tp = json.load(f)
if tp.get('vlm_summary'):
vlm_done = True
break
if vlm_done:
c.execute("UPDATE processing_status SET vlm_done = ?, last_updated = ? WHERE file_uuid = ?",
(True, now, uuid))
print(" ✅ processing_status: vlm_done=True")
# Commit and close
conn.commit()
conn.close()
print(f" ✅ face_operations.sqlite created at {db_path}")
PYEOF
# Sync to Qdrant (if face.json has embeddings)
python3 -c "
import json
import subprocess
face_json = '$face_json'
with open(face_json) as f:
d = json.load(f)
has_embeddings = any(face.get('embedding') for frame in d.get('frames', []) for face in frame.get('faces', []))
if has_embeddings:
print(' 🔄 Syncing embeddings to Qdrant...')
# Note: The actual Qdrant sync is done by rust_store_traced_faces
# This is just a placeholder for future direct sync
print(' ⏭️ Qdrant sync skipped (use rust_store_traced_faces)')
else:
print(' ⏭️ No embeddings to sync')
"
echo "✅ $uuid sync complete"
}
# Main
if [ -n "$1" ]; then
sync_face_data "$1"
else
echo "Syncing all face data..."
for f in "$OUTPUT_DIR"/*.face.json; do
uuid=$(basename "$f" .face.json)
sync_face_data "$uuid"
done
echo "✅ All syncs complete"
fi