diff --git a/scripts/store_traced_faces.py b/scripts/store_traced_faces.py index 296f368f..8b5bd9a1 100644 --- a/scripts/store_traced_faces.py +++ b/scripts/store_traced_faces.py @@ -139,9 +139,46 @@ def run_face_tracker( if has_trace: print(f"[TRACE] face.json already contains trace_id, skipping tracking") - # Copy face.json to traced_json_path (no re-tracking needed) + # Build traces dict from existing trace_ids before copying + traces = defaultdict(list) + if isinstance(frames_to_check, list): + for frame in frames_to_check: + for face in frame.get("faces", []): + trace_id = face.get("trace_id") + if trace_id is not None and trace_id > 0: + traces[trace_id].append(face) + else: + for frame_data in frames_to_check.values(): + for face in frame_data.get("faces", []): + trace_id = face.get("trace_id") + if trace_id is not None and trace_id > 0: + traces[trace_id].append(face) + + # Add traces field to face_data + face_data["traces"] = {} + fps = face_data.get("fps", 30.0) + for trace_id, trace_faces in traces.items(): + if len(trace_faces) == 0: + continue + frames_list = [f.get("frame", f.get("frame_number", 0)) for f in trace_faces] + frame_indices = sorted([f if isinstance(f, int) else int(f) for f in frames_list]) + duration_frames = len(frame_indices) + duration_seconds = duration_frames / fps if fps > 0 else 0 + + face_data["traces"][str(trace_id)] = { + "trace_id": int(trace_id), + "start_frame": frame_indices[0] if frame_indices else 0, + "end_frame": frame_indices[-1] if frame_indices else 0, + "duration_frames": duration_frames, + "duration_seconds": round(duration_seconds, 2), + } + + print(f"[TRACE] Built {len(traces)} traces from existing trace_ids") + + # Write to traced_json_path import shutil - shutil.copy(face_json_path, traced_json_path) + with open(traced_json_path, "w") as f: + json.dump(strip_embeddings_and_round(face_data), f, indent=2, ensure_ascii=False) return traced_json_path # V2.0 uses list format (FaceResult), convert to dict for face_tracker