File size: 1,439 Bytes
1df8ab8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/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()