prepare_data.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import os
  2. def generate_change_txt(a_dir, b_dir, label_dir, output_txt_path):
  3. # 打开输出文件进行写入
  4. with open(output_txt_path, 'w') as f:
  5. # 获取所有文件名(假设文件名格式一致)
  6. a_files = sorted(os.listdir(a_dir))
  7. b_files = sorted(os.listdir(b_dir))
  8. label_files = sorted(os.listdir(label_dir))
  9. # 假设三个文件夹中的文件数量和名称是一致的
  10. for a_file, b_file, label_file in zip(a_files, b_files, label_files):
  11. # if a_file.endswith('.png') and b_file.endswith('.png') and label_file.endswith('.png'):
  12. # 构建完整路径
  13. a_filepath = os.path.join('tuban', 'train', 'A', a_file)
  14. b_filepath = os.path.join('tuban', 'train', 'B', b_file)
  15. label_filepath = os.path.join('tuban', 'train', 'label', label_file)
  16. # 写入文件路径到txt
  17. f.write(f"{a_filepath} {b_filepath} {label_filepath}\n")
  18. def generate_seg_txt(a_dir, label_dir, output_txt_path):
  19. # 打开输出文件进行写入
  20. with open(output_txt_path, 'w') as f:
  21. # 获取所有文件名(假设文件名格式一致)
  22. a_files = sorted(os.listdir(a_dir))
  23. label_files = sorted(os.listdir(label_dir))
  24. # 假设三个文件夹中的文件数量和名称是一致的
  25. for a_file, label_file in zip(a_files,label_files):
  26. # if a_file.endswith('.png') and b_file.endswith('.png') and label_file.endswith('.png'):
  27. # 构建完整路径
  28. a_filepath = os.path.join('seg', 'train', 'input', a_file)
  29. label_filepath = os.path.join('seg', 'train', 'output', label_file)
  30. # 写入文件路径到txt
  31. f.write(f"{a_filepath} {label_filepath}\n")
  32. def generate_building_txt(a_dir, label_dir, output_txt_path):
  33. # 打开输出文件进行写入
  34. with open(output_txt_path, 'w') as f:
  35. # 获取所有文件名(假设文件名格式一致)
  36. a_files = sorted(os.listdir(a_dir))
  37. label_files = sorted(os.listdir(label_dir))
  38. # 假设三个文件夹中的文件数量和名称是一致的
  39. for a_file, label_file in zip(a_files,label_files):
  40. # if a_file.endswith('.png') and b_file.endswith('.png') and label_file.endswith('.png'):
  41. # 构建完整路径
  42. a_filepath = os.path.join( 'val','JPGImages', a_file)
  43. label_filepath = os.path.join( 'val','Annotations', label_file)
  44. # 写入文件路径到txt
  45. f.write(f"{a_filepath} {label_filepath}\n")
  46. # 指定A、B和label文件夹路径及输出的txt文件路径
  47. a_dir = r'E:\building_data\remote_sensing\val\JPGImages'
  48. b_dir = r'E:\PaddleRS-1.0.0\data\tuban\val\\B'
  49. label_dir = r'E:\building_data\remote_sensing\val\Annotations'
  50. output_txt_path = r'E:\PaddleRS\data\building\val.txt'
  51. # 生成对应的txt文件
  52. # generate_change_txt(a_dir, b_dir, label_dir, output_txt_path)
  53. generate_building_txt(a_dir, label_dir, output_txt_path)