SIMS: https://github.com/xjqicuhk/SIMS
ADE20K_outdoor数据集生成对应的.odgt文件
import os
from PIL import Image
def getFileList(root, file_path):
file_list = []
for file in os.listdir(os.path.join(root, file_path)):
if file.endswith(".png") or file.endswith(".jpg"):
write_name = file.split('/')[-1]
file_list.append(os.path.join(file_path,write_name))
return sorted(file_list)
def getHWList(root, images_path):
file_list = []
for path in images_path:
path = os.path.join(root, path)
image = Image.open(path)
file_list.append(list(image.size))
return file_list
def createFilelist(root, images_path, text_save_path, mode_name):
text_save_path = os.path.join(root, text_save_path)
file_name = open(text_save_path, "w")
#获取图片的路径信息
images_list = []
for eachname in os.listdir(os.path.join(root,images_path)):
path = os.path.join(images_path,eachname,mode_name)
image_list = getFileList(root, path)
images_list.append(image_list)
#获取图片的长度和宽度信息
hw_list = getHWList(root, images_list[0])
#将图片的信息按照一定的格式逐行写入txt文件中
segm_path, img_path = images_list
for i in range(len(hw_list)):
flag = '{{"fpath_img":"{0}", "fpath_segm":"{1}", "width":"{2}", "height":"{3}"}}'.\
format(img_path[i],segm_path[i],hw_list[i][0],hw_list[i][1])
file_name.write(flag + '\n')
file_name.close()
if __name__ == "__main__":
mode = 'test'
mode_name = 'validation' if 'test' in mode else 'training'
root = 'D:\Dataset'
# 图片存放目录
images_path = 'ADEChallengeData2016\ADE20K_outdoor'
# 生成图片列表文件的保存目录
txt_path = 'ADEChallengeData2016'
txt_name = 'outdoor_'+mode_name+'.odgt'
text_save_path = txt_path + '/' + txt_name
# 生成txt文件
createFilelist(root, images_path, text_save_path, mode_name)
print('生成成功!')