generate_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):
    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('生成成功!')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值