Add files using upload-large-folder tool
Browse files- fix_metadata.py +54 -0
fix_metadata.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
快速修复 metadata.jsonl 文件,添加 file_name 列
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import json
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
from tqdm import tqdm
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def fix_metadata_file(jsonl_path):
|
| 12 |
+
"""修复单个 metadata.jsonl 文件"""
|
| 13 |
+
records = []
|
| 14 |
+
|
| 15 |
+
with open(jsonl_path, 'r') as f:
|
| 16 |
+
for line in f:
|
| 17 |
+
record = json.loads(line.strip())
|
| 18 |
+
|
| 19 |
+
# 添加 file_name 列(使用 image 或 image_v0 作为主图像)
|
| 20 |
+
if 'file_name' not in record:
|
| 21 |
+
if 'image' in record:
|
| 22 |
+
record['file_name'] = record['image']
|
| 23 |
+
elif 'image_v0' in record:
|
| 24 |
+
record['file_name'] = record['image_v0']
|
| 25 |
+
|
| 26 |
+
records.append(record)
|
| 27 |
+
|
| 28 |
+
# 写回文件
|
| 29 |
+
with open(jsonl_path, 'w') as f:
|
| 30 |
+
for record in records:
|
| 31 |
+
f.write(json.dumps(record, ensure_ascii=False) + '\n')
|
| 32 |
+
|
| 33 |
+
return len(records)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def main():
|
| 37 |
+
base_dir = Path(__file__).parent
|
| 38 |
+
|
| 39 |
+
# 找到所有 metadata.jsonl 文件
|
| 40 |
+
jsonl_files = list(base_dir.rglob('metadata.jsonl'))
|
| 41 |
+
|
| 42 |
+
print(f"找到 {len(jsonl_files)} 个 metadata.jsonl 文件")
|
| 43 |
+
|
| 44 |
+
total_records = 0
|
| 45 |
+
for jsonl_path in tqdm(jsonl_files, desc="修复 metadata"):
|
| 46 |
+
count = fix_metadata_file(jsonl_path)
|
| 47 |
+
total_records += count
|
| 48 |
+
|
| 49 |
+
print(f"\n修复完成!共处理 {total_records} 条记录")
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
if __name__ == "__main__":
|
| 53 |
+
main()
|
| 54 |
+
|