文件目录如下
若无数据集可以先去下载,下载链接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/BIT_CD-master/LEVIR_CD/A/train'
train_output_file = r'/home/aistudio/BIT_CD-master/LEVIR_CD/list/train.txt'
test_folder_path = r'/home/aistudio/BIT_CD-master/LEVIR_CD/A/test'
test_output_file = r'/home/aistudio/BIT_CD-master/LEVIR_CD/list/test.txt'
val_folder_path = r'/home/aistudio/BIT_CD-master/LEVIR_CD/A/val'
val_output_file = r'/home/aistudio/BIT_CD-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)