Generate the image path index file of the dataset and save it as a txt file

Here we take the OULU dataset as an example. The following is the directory structure of the Test file:

 Pay attention to the changes in the directory path marked with a red box in the picture!

In the windows system , if you want to be accurate to "1.jpg" in the third picture, then its path is "I:\oulu_images\crop_img\f_Test_256\Test_files_1_1\1_1_36_1\1.jpg",

Under the Linux system , if you want to be accurate to "1.jpg" in the third picture, it is "/home/oulu_images/crop_img/f_Test_256/Test_files_1_1/1_1_36_1/1.jpg"

The specific situation needs specific analysis! 

The code to generate the txt file is as follows:

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!")

It should be noted that the expressions of the path are different in the linux and windows environments:

If it is windows, then the file source path of the image is:

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

If it is linux, then the file source path of the image is:

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

The following figure is the image path index file "test_img_list.txt" generated for the Test of the OULU dataset in the window environment:

 

 

Guess you like

Origin blog.csdn.net/power_kaikaige/article/details/123646250