import json import os import argparse def populate_paths(annotations_file: str, integrated_file: str, output_file: str): # Load existing annotations with open(annotations_file, 'r', encoding='utf-8') as f: annotations = json.load(f) # Load integrated_output_v2 entries with open(integrated_file, 'r', encoding='utf-8') as f: integrated = json.load(f) # Update paths for item in integrated: img = item.get('s3_fileUrl', '') vid = item.get('s3_instructionReference', '') parts = img.rstrip('/').split('/') if len(parts) < 2: continue clip_id = parts[-2] # e.g. 'clip_305_000786' frame_file = parts[-1] # e.g. 'frame_000786.png' frame_id = os.path.splitext(frame_file)[0] key = f"{clip_id}_{frame_id}" # matches your JSON key if key in annotations: annotations[key]['image_path'] = img annotations[key]['video_path'] = vid # Write back with open(output_file, 'w', encoding='utf-8') as f: json.dump(annotations, f, indent=4, ensure_ascii=False) print(f"Updated file written to {output_file}") if __name__ == "__main__": parser = argparse.ArgumentParser( description="Populate image_path and video_path in drama_x_annotations.json" ) parser.add_argument( "annotations", help="Path to existing drama_x_annotations.json" ) parser.add_argument( "integrated", help="Path to integrated_output_v2.json" ) parser.add_argument( "-o", "--output", default="drama_x_annotations_populated.json", help="Where to save the updated annotations" ) args = parser.parse_args() populate_paths(args.annotations, args.integrated, args.output)