| #!/usr/bin/env python3 | |
| """ | |
| 快速修复 metadata.jsonl 文件,添加 file_name 列 | |
| """ | |
| import json | |
| from pathlib import Path | |
| from tqdm import tqdm | |
| def fix_metadata_file(jsonl_path): | |
| """修复单个 metadata.jsonl 文件""" | |
| records = [] | |
| with open(jsonl_path, 'r') as f: | |
| for line in f: | |
| record = json.loads(line.strip()) | |
| # 添加 file_name 列(使用 image 或 image_v0 作为主图像) | |
| if 'file_name' not in record: | |
| if 'image' in record: | |
| record['file_name'] = record['image'] | |
| elif 'image_v0' in record: | |
| record['file_name'] = record['image_v0'] | |
| records.append(record) | |
| # 写回文件 | |
| with open(jsonl_path, 'w') as f: | |
| for record in records: | |
| f.write(json.dumps(record, ensure_ascii=False) + '\n') | |
| return len(records) | |
| def main(): | |
| base_dir = Path(__file__).parent | |
| # 找到所有 metadata.jsonl 文件 | |
| jsonl_files = list(base_dir.rglob('metadata.jsonl')) | |
| print(f"找到 {len(jsonl_files)} 个 metadata.jsonl 文件") | |
| total_records = 0 | |
| for jsonl_path in tqdm(jsonl_files, desc="修复 metadata"): | |
| count = fix_metadata_file(jsonl_path) | |
| total_records += count | |
| print(f"\n修复完成!共处理 {total_records} 条记录") | |
| if __name__ == "__main__": | |
| main() | |