生成数据集的图像路径索引文件,并以txt文件保存

这里以OULU数据集为例,以下是Test文件的目录结构:

 注意观察图片中标红框的目录路径的变化哦!

在windows系统下,如果要精确到第三张图片中的"1.jpg",那么其路径为"I:\oulu_images\crop_img\f_Test_256\Test_files_1_1\1_1_36_1\1.jpg",

在linux系统下,如果要精确到第三张图片中的"1.jpg",则为"/home/oulu_images/crop_img/f_Test_256/Test_files_1_1/1_1_36_1/1.jpg"

具体情况需要具体分析! 

生成txt文件的代码如下:

import os

#windows
root_dir = "I:\\oulu_images\\crop_img\\f_Test_256\\"
#linux
#root_dir = "/home/oulu_images/crop_img/f_Test_256/"

#windows
dst = "I:\\oulu_images\\test_img_list.txt"

#linux
#dst = "/home/oulu_images/test_img_list.txt"
# if not os.path.exists(img_dir):
#     os.makedirs(img_dir)
f = open(dst,"w+")
id_list = os.listdir(root_dir)
num = len(id_list)
for id in id_list:
    print("id:\n", id)
    #window
    imgFile_dir = root_dir + id + "\\"
    #linux
    #imgFile_dir = root_dir + id + "/"
    print("imgFile_dir:\n",imgFile_dir)
    imgFile_list = os.listdir(imgFile_dir)
    print("imgFile_list:\n",imgFile_list)
    for imgFile in imgFile_list:
        #window
        img_dir = imgFile_dir + imgFile + "\\"
        #linux
        #img_dir = imgFile_dir + imgFile + "/"
        print("img_dir:\n", img_dir)
        img_list = os.listdir(img_dir)
        for img in img_list:
            f.write(img_dir + "," + img + "\n")
            print("图片路径:\n",img_dir + "," + img)
f.close()
print("Finished!")

需要注意的是,linux与windows环境下,路径的表达方式是不一样的:

如果是windows,那么图像的文件源路径为:

root_dir = "I:\\oulu_images\\crop_img\\f_Test_256\\"

如果是linux,那么图像的文件源路径为:

root_dir = "/home/oulu_images/crop_img/f_Test_256/"

下图是在window环境运行下,生成的关于OULU数据集的Test的图像路径索引文件"test_img_list.txt" :

猜你喜欢

转载自blog.csdn.net/power_kaikaige/article/details/123646250