import os import json import argparse def main(): parser = argparse.ArgumentParser( description="Generate metadata.jsonl for DDPD dataset" ) parser.add_argument( "--train_dir", default="dd_dp_dataset_png/train_c", help="Train directory of the dataset" ) parser.add_argument( "--val_dir", default="dd_dp_dataset_png/val_c", help="Validation directory of the dataset" ) parser.add_argument( "--test_dir", default="dd_dp_dataset_png/test_c", help="Test directory of the dataset" ) parser.add_argument( "--source_dir_name", default="source", help="Source directory name of the dataset" ) parser.add_argument( "--target_dir_name", default="target", help="Target directory name of the dataset" ) parser.add_argument( "--output_dir", default="config/combined", help="Output directory", ) args = parser.parse_args() train_dir = args.train_dir val_dir = args.val_dir test_dir = args.test_dir output_dir = args.output_dir source_dir_name = args.source_dir_name target_dir_name = args.target_dir_name # Ensure output directory exists os.makedirs(output_dir, exist_ok=True) types = ['train', 'val', 'test'] dirs = [train_dir, val_dir, test_dir] for type_, dir_path in zip(types, dirs): if os.path.exists(dir_path): source_dir = os.path.join(dir_path, source_dir_name) target_dir = os.path.join(dir_path, target_dir_name) if os.path.exists(source_dir): output_file = os.path.join(output_dir, type_, 'metadata.jsonl') os.makedirs(os.path.dirname(output_file), exist_ok=True) rel_output_dir = os.path.dirname(output_file) with open(output_file, "w") as f: source_files = sorted([f for f in os.listdir(source_dir) if f.lower().endswith('.png')]) target_files = [] if os.path.exists(target_dir): target_files = sorted([f for f in os.listdir(target_dir) if f.lower().endswith('.png')]) min_len = min(len(source_files), len(target_files)) if target_files else len(source_files) for i in range(min_len): source_path = os.path.join(source_dir, source_files[i]) rel_source = os.path.relpath(source_path, rel_output_dir) entry = {'source_file_name': rel_source} if target_files: target_path = os.path.join(target_dir, target_files[i]) rel_target = os.path.relpath(target_path, rel_output_dir) entry['target_file_name'] = rel_target f.write(json.dumps(entry) + '\n') for i in range(min_len, len(source_files)): source_path = os.path.join(source_dir, source_files[i]) rel_source = os.path.relpath(source_path, rel_output_dir) entry = {'source_file_name': rel_source} f.write(json.dumps(entry) + '\n') if __name__ == "__main__": main()