|
|
import os |
|
|
import json |
|
|
|
|
|
def collect_files(data_dir): |
|
|
""" |
|
|
遍历 data 文件夹下的各子文件夹,以文件名的前 7 个字符为键,将对应的文件路径整理为字典 |
|
|
""" |
|
|
file_dict = {} |
|
|
|
|
|
|
|
|
subfolders = ['densepose', 'videos', 'cloth', 'cloth_mask', 'agnostic_mask', 'agnostic'] |
|
|
|
|
|
for subfolder in subfolders: |
|
|
subfolder_path = os.path.join(data_dir, subfolder) |
|
|
|
|
|
if not os.path.exists(subfolder_path): |
|
|
print(f"Warning: {subfolder_path} 路径不存在") |
|
|
continue |
|
|
|
|
|
|
|
|
for file_name in os.listdir(subfolder_path): |
|
|
|
|
|
key = file_name[:7] |
|
|
if key not in file_dict: |
|
|
|
|
|
file_dict[key] = {} |
|
|
|
|
|
|
|
|
file_dict[key][subfolder] = os.path.join(subfolder_path, file_name) |
|
|
|
|
|
return file_dict |
|
|
|
|
|
def generate_json(data_dir, output_file): |
|
|
""" |
|
|
生成 JSON 文件,将文件匹配结果输出 |
|
|
""" |
|
|
files = collect_files(data_dir) |
|
|
result = [] |
|
|
|
|
|
|
|
|
for key, paths in files.items(): |
|
|
result.append({ |
|
|
"densepose": paths.get("densepose", ""), |
|
|
"videos": paths.get("videos", ""), |
|
|
"cloth": paths.get("cloth", ""), |
|
|
"cloth_mask": paths.get("cloth_mask", ""), |
|
|
"agnostic_mask": paths.get("agnostic_mask", ""), |
|
|
"agnostic": paths.get("agnostic", "") |
|
|
}) |
|
|
|
|
|
|
|
|
with open(output_file, "w", encoding="utf-8") as f: |
|
|
json.dump(result, f, indent=4, ensure_ascii=False) |
|
|
|
|
|
print(f"JSON 文件已生成: {output_file}") |
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
|
|
data_dir = "/mnt/lpai-dione/ssai/cvg/team/wjj/ViViD/data" |
|
|
|
|
|
output_file = "/mnt/lpai-dione/ssai/cvg/team/wjj/ViViD/data/vividfuxian_stage1.json" |
|
|
|
|
|
generate_json(data_dir, output_file) |