欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

pytorch数据预处理的前期准备

程序员文章站 2024-03-01 17:10:34
...

作用:把地址和原图,标签内容保存到txt 中

import os
# 读取图片文件,并将图片地址、图片名和标签写到txt文件中
def txt():
    data_base_dir = "F:\data\MSRA10K_Imgs_GT\Imgs"
    list = []
    list1 = []
    write_file_name = 'F:\data\MSRA10K_Imgs_GT\dir.txt'
    write_file = open(write_file_name, "w")
    write_file_name1 = 'F:\data\MSRA10K_Imgs_GT\dir1.txt'
    write_file1 = open(write_file_name1, "w")
    for filename in os.listdir(data_base_dir):
        if filename.endswith(".jpg"):
            write_name = filename
            # list.append(data_base_dir)
            list.append(write_name)
            # print(list)
            sorted(list)
            number_of_lines = len(list)  # 列表中元素个数
        if filename.endswith(".png"):
            write_name1 = filename
            # list.append(data_base_dir)
            list1.append(write_name1)
            # print(list1)
            sorted(list1)
            number_of_lines1 = len(list1)  # 列表中元素个数
    # print(list1[0:5])
    # print(number_of_lines1)

            # 将图片信息逐行写入
    for current_line in range(number_of_lines):
        write_file.write('F:\data\MSRA10K_Imgs_GT\Imgs' + '\ ' + ' ' + list[current_line] + '\n')
    write_file.close()

    for current_line1 in range(number_of_lines1):
        write_file1.write('F:\data\MSRA10K_Imgs_GT\Imgs' + '\ ' + ' ' + list1[current_line1] + '\n')
    write_file1.close()
txt()