123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- import os
- def generate_change_txt(a_dir, b_dir, label_dir, output_txt_path):
- # 打开输出文件进行写入
- with open(output_txt_path, 'w') as f:
- # 获取所有文件名(假设文件名格式一致)
- a_files = sorted(os.listdir(a_dir))
- b_files = sorted(os.listdir(b_dir))
- label_files = sorted(os.listdir(label_dir))
-
- # 假设三个文件夹中的文件数量和名称是一致的
- for a_file, b_file, label_file in zip(a_files, b_files, label_files):
- # if a_file.endswith('.png') and b_file.endswith('.png') and label_file.endswith('.png'):
- # 构建完整路径
- a_filepath = os.path.join('tuban', 'train', 'A', a_file)
- b_filepath = os.path.join('tuban', 'train', 'B', b_file)
- label_filepath = os.path.join('tuban', 'train', 'label', label_file)
- # 写入文件路径到txt
- f.write(f"{a_filepath} {b_filepath} {label_filepath}\n")
- def generate_seg_txt(a_dir, label_dir, output_txt_path):
- # 打开输出文件进行写入
- with open(output_txt_path, 'w') as f:
- # 获取所有文件名(假设文件名格式一致)
- a_files = sorted(os.listdir(a_dir))
- label_files = sorted(os.listdir(label_dir))
-
- # 假设三个文件夹中的文件数量和名称是一致的
- for a_file, label_file in zip(a_files,label_files):
- # if a_file.endswith('.png') and b_file.endswith('.png') and label_file.endswith('.png'):
- # 构建完整路径
- a_filepath = os.path.join('seg', 'train', 'input', a_file)
- label_filepath = os.path.join('seg', 'train', 'output', label_file)
- # 写入文件路径到txt
- f.write(f"{a_filepath} {label_filepath}\n")
- def generate_building_txt(a_dir, label_dir, output_txt_path):
- # 打开输出文件进行写入
- with open(output_txt_path, 'w') as f:
- # 获取所有文件名(假设文件名格式一致)
- a_files = sorted(os.listdir(a_dir))
- label_files = sorted(os.listdir(label_dir))
-
- # 假设三个文件夹中的文件数量和名称是一致的
- for a_file, label_file in zip(a_files,label_files):
- # if a_file.endswith('.png') and b_file.endswith('.png') and label_file.endswith('.png'):
- # 构建完整路径
- a_filepath = os.path.join( 'val','JPGImages', a_file)
- label_filepath = os.path.join( 'val','Annotations', label_file)
- # 写入文件路径到txt
- f.write(f"{a_filepath} {label_filepath}\n")
- # 指定A、B和label文件夹路径及输出的txt文件路径
- a_dir = r'E:\building_data\remote_sensing\val\JPGImages'
- b_dir = r'E:\PaddleRS-1.0.0\data\tuban\val\\B'
- label_dir = r'E:\building_data\remote_sensing\val\Annotations'
- output_txt_path = r'E:\PaddleRS\data\building\val.txt'
- # 生成对应的txt文件
- # generate_change_txt(a_dir, b_dir, label_dir, output_txt_path)
- generate_building_txt(a_dir, label_dir, output_txt_path)
|