Files
momentry_core/scripts/upgrade_face_json_v2.sh

173 lines
4.7 KiB
Bash
Executable File

#!/bin/bash
# Upgrade face JSON files from v1 to v2 schema
# Usage: ./scripts/upgrade_face_json_v2.sh [file_uuid]
OUTPUT_DIR="${MOMENTRY_OUTPUT_DIR:-/Users/accusys/momentry/output}"
upgrade_face_json() {
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"
if [ ! -f "$face_json" ]; then
echo "⚠️ face.json not found for $uuid"
return
fi
echo "🔄 Upgrading $uuid to v2..."
# Upgrade face.json
python3 -c "
import json
with open('$face_json') as f:
d = json.load(f)
# Add schema_version
d['schema_version'] = '2.0'
# Add x2, y2, center_x, center_y to each face
for frame in d.get('frames', []):
for face in frame.get('faces', []):
x = face.get('x', 0)
y = face.get('y', 0)
w = face.get('width', 0)
h = face.get('height', 0)
face['x2'] = x + w
face['y2'] = y + h
face['center_x'] = x + w // 2
face['center_y'] = y + h // 2
with open('$face_json', 'w') as f:
json.dump(d, f, indent=2)
print(' ✅ face.json upgraded')
"
# Upgrade face_traced.json
if [ -f "$traced_json" ]; then
python3 -c "
import json
with open('$traced_json') as f:
d = json.load(f)
fps = d.get('fps', 30.0)
# Add schema_version and restructure
d['schema_version'] = '2.0'
d['file_uuid'] = '$uuid'
d['status'] = 'completed'
# Convert traces from HashMap to Vec
traces_old = d.get('traces', {})
traces_new = []
for tid, t in traces_old.items():
trace_id = int(tid)
path = t.get('path', [])
frames_list = t.get('frames', [])
# Calculate stats
start_frame = path[0]['frame'] if path else 0
end_frame = path[-1]['frame'] if path else 0
start_time = start_frame / fps
end_time = end_frame / fps
avg_confidence = sum(p['confidence'] for p in path) / len(path) if path else 0
# Calculate avg_bbox
if path:
avg_x = sum(p['bbox']['x'] for p in path) // len(path)
avg_y = sum(p['bbox']['y'] for p in path) // len(path)
avg_w = sum(p['bbox']['width'] for p in path) // len(path)
avg_h = sum(p['bbox']['height'] for p in path) // len(path)
else:
avg_x = avg_y = avg_w = avg_h = 0
# Add x2/y2/center to bbox
for p in path:
b = p['bbox']
b['x2'] = b['x'] + b['width']
b['y2'] = b['y'] + b['height']
b['center_x'] = b['x'] + b['width'] // 2
b['center_y'] = b['y'] + b['height'] // 2
traces_new.append({
'trace_id': trace_id,
'frame_count': len(path),
'start_frame': start_frame,
'end_frame': end_frame,
'start_time': start_time,
'end_time': end_time,
'avg_confidence': avg_confidence,
'avg_capture_quality': 0.0,
'best_frame': max(path, key=lambda p: p['confidence'])['frame'] if path else 0,
'best_confidence': max(p['confidence'] for p in path) if path else 0,
'avg_bbox': {
'x': avg_x, 'y': avg_y, 'width': avg_w, 'height': avg_h,
'x2': avg_x + avg_w, 'y2': avg_y + avg_h,
'center_x': avg_x + avg_w // 2, 'center_y': avg_y + avg_h // 2
},
'face_instances': path,
'total_appearances': t.get('total_appearances', 0),
'path': path,
'frames': frames_list
})
d['traces'] = traces_new
d['stats'] = {
'total_traces': len(traces_new),
'total_face_instances': sum(t['frame_count'] for t in traces_new),
'avg_trace_length': sum(t['frame_count'] for t in traces_new) / len(traces_new) if traces_new else 0
}
with open('$traced_json', 'w') as f:
json.dump(d, f, indent=2)
print(' ✅ face_traced.json upgraded')
"
fi
# Upgrade face_cluster.json
if [ -f "$cluster_json" ]; then
python3 -c "
import json
with open('$cluster_json') as f:
d = json.load(f)
# Add schema_version and stats
d['schema_version'] = '2.0'
d['status'] = d.get('status', 'completed')
# Add stats
clusters = d.get('clusters', [])
total_traces = sum(len(c.get('trace_ids', [])) for c in clusters)
d['stats'] = {
'total_clusters': len(clusters),
'unassigned_traces': len(d.get('unassigned_traces', [])),
'total_traces': total_traces
}
with open('$cluster_json', 'w') as f:
json.dump(d, f, indent=2)
print(' ✅ face_cluster.json upgraded')
"
fi
echo "✅ $uuid upgrade complete"
}
# Main
if [ -n "$1" ]; then
upgrade_face_json "$1"
else
echo "Upgrading all face JSON files..."
for f in "$OUTPUT_DIR"/*.face.json; do
uuid=$(basename "$f" .face.json)
upgrade_face_json "$uuid"
done
echo "✅ All upgrades complete"
fi