Upload layer_diff_dataset/make_json copy 4.py with huggingface_hub
Browse files
layer_diff_dataset/make_json copy 4.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
# 读取im_rgba.txt中的内容作为bg prompt写入json
|
| 4 |
+
|
| 5 |
+
# folder_path = '/mnt/workspace/workgroup/sihui.jsh/layer_diff_dataset/try/im'
|
| 6 |
+
# folder_path_1 = '/mnt/workspace/workgroup/sihui.jsh/layer_diff_dataset/train/im'
|
| 7 |
+
|
| 8 |
+
# file_list = os.listdir(folder_path)
|
| 9 |
+
# file_list = [os.path.join(folder_path_1,i) for i in file_list if i.endswith('.jpg')]
|
| 10 |
+
# file_list.sort()
|
| 11 |
+
# print(file_list)
|
| 12 |
+
# 读取原始JSON文件
|
| 13 |
+
data_folder = '/mnt/workspace/workgroup/sihui.jsh/layer_diff_dataset/train'
|
| 14 |
+
with open(os.path.join(data_folder,'im_rgba.json'), 'r') as file:
|
| 15 |
+
data = json.load(file)
|
| 16 |
+
|
| 17 |
+
captions_dict = {}
|
| 18 |
+
with open(os.path.join(data_folder, 'im_rgba.txt'), 'r') as f:
|
| 19 |
+
for line in f.readlines():
|
| 20 |
+
image_name, caption = line.strip().split('\t')
|
| 21 |
+
captions_dict[image_name] = caption
|
| 22 |
+
# data_try = []
|
| 23 |
+
# # 修改每个字典的"images"和"images_rgba"字段
|
| 24 |
+
# for item in data:
|
| 25 |
+
# if item["images"] not in file_list:
|
| 26 |
+
# data_try.append(item)
|
| 27 |
+
# print(data_try)
|
| 28 |
+
for item in data:
|
| 29 |
+
image_name = item["images"].split('/')[-1]
|
| 30 |
+
# item['prompt'] = class_name + ',' + item['prompt']
|
| 31 |
+
item['bg_prompt'] = captions_dict[image_name]
|
| 32 |
+
# print(item)
|
| 33 |
+
# break
|
| 34 |
+
# 写入新的JSON文件
|
| 35 |
+
with open('/mnt/workspace/workgroup/sihui.jsh/layer_diff_dataset/train/im_rgba_bg.json', 'w') as file:
|
| 36 |
+
json.dump(data, file, indent=4)
|
| 37 |
+
|
| 38 |
+
print("New JSON file has been created successfully.")
|