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):
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)):
print("root:", root)
print("images_path:", images_path)
print("eachname:", eachname)
path = os.path.join(images_path,eachname)
print("path:", path)
image_list = getFileList(root, path)
#print("image_list:", image_list)
images_list.append(image_list)
#print("images_list[0]:", images_list[0])
#获取图片的长度和宽度信息
hw_list = getHWList(root, images_list[0])
#将图片的信息按照一定的格式逐行写入txt文件中
img_path, segm_path = images_list
# print("img_path:", images_list[1])
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])
# print("img_path[i]:", img_path[i])
# print("segm_path[i]:", segm_path[i])
file_name.write(flag + '\n')
file_name.close()
if __name__ == "__main__":
mode = 'train_sum'
#mode_name = 'validation' if 'test' in mode else 'train'
root = './data/'
# 图片存放目录
images_path = 'limit_lever/sum'
# 生成图片列表文件的保存目录
txt_path = 'limit_lever/'
txt_name = mode+'.odgt'
text_save_path = txt_path + '/' + txt_name
# 生成txt文件
createFilelist(root, images_path, text_save_path)
print('生成成功!')
generate_odgt
最新推荐文章于 2024-10-08 12:37:10 发布