文件目录如下
在这里插入图片描述
若无数据集可以先去下载,下载链接http://chenhao.in/LEVIR/
下面把python代码复制运行即可,记得改路径!!!

import os
import glob

def create_image_list_txt(folder_path, output_file):
    # 使用glob模块获取文件夹中的所有图片文件
    image_files = glob.glob(os.path.join(folder_path, 'train_*.png'))  # .png可自行修改为其他格式
    image_files += glob.glob(os.path.join(folder_path, 'test_*.png'))
    image_files += glob.glob(os.path.join(folder_path, 'val_*.png'))

    # 对图片文件进行自定义排序,按照文件名中的数字部分进行排序
    image_files.sort(key=lambda x: int(os.path.splitext(os.path.basename(x))[0].split('_')[1]))

    # 打开文本文件以写入模式,使用utf-8编码
    with open(output_file, 'w', encoding='utf-8') as file:
        # 将图片文件名称写入文本文件
        for image_file in image_files:
            file.write(os.path.basename(image_file) + '\n')

    print(output_file)

# 路径的环境
train_folder_path = r'/home/aistudio/master/LEVIR_CD/A/train'
train_output_file = r'/home/aistudio/master/LEVIR_CD/list/train.txt'

test_folder_path = r'/home/aistudio/master/LEVIR_CD/A/test'
test_output_file = r'/home/aistudio/master/LEVIR_CD/list/test.txt'

val_folder_path = r'/home/aistudio/master/LEVIR_CD/A/val'
val_output_file = r'/home/aistudio/master/LEVIR_CD/list/val.txt'

# 执行代码
create_image_list_txt(train_folder_path, train_output_file)
create_image_list_txt(test_folder_path, test_output_file)
create_image_list_txt(val_folder_path, val_output_file)
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐