generate_txt.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import os
  2. import yaml
  3. def generate_txt_file(data_dir, subset, txt_filename):
  4. subset_dir = os.path.join(data_dir, 'images', subset)
  5. image_dir = os.path.join(subset_dir+'2017')
  6. print(image_dir)
  7. label_dir = os.path.join(data_dir, 'labels', subset + '2017')
  8. image_paths = []
  9. for filename in os.listdir(image_dir):
  10. if filename.endswith('.jpg') or filename.endswith('.png'):
  11. # if filename.endswith('.txt'):
  12. image_path = os.path.join(image_dir, filename)
  13. image_paths.append(image_path)
  14. txt_path = os.path.join(data_dir, txt_filename)
  15. with open(txt_path, 'w') as f:
  16. for image_path in image_paths:
  17. f.write(image_path + '\n')
  18. def generate_class_txt(coco_dir, yaml_file):
  19. yaml_path = os.path.join(coco_dir, yaml_file)
  20. with open(yaml_path, 'r') as f:
  21. data = yaml.safe_load(f)
  22. class_names = data['names']
  23. class_txt_path = os.path.join(coco_dir, 'class.txt')
  24. with open(class_txt_path, 'w') as f:
  25. for class_name in class_names:
  26. f.write(class_name + '\n')
  27. def main():
  28. coco_dir = '/home/yhsun/ObjectDetection-main/datasets/VOC2007' # 替换为你的 COCO 数据集路径
  29. yaml_file = 'voc.yaml' # COCO YAML 文件名
  30. # # 生成 train.txt
  31. # generate_txt_file(coco_dir, 'train', 'train.txt')
  32. # print("Processed train dataset")
  33. # # 生成 val.txt
  34. # generate_txt_file(coco_dir, 'val', 'val.txt')
  35. # print("Processed val dataset")
  36. # # 生成 test.txt
  37. # generate_txt_file(coco_dir, 'test', 'test.txt')
  38. # print("Processed test dataset")
  39. # 生成 class.txt
  40. generate_class_txt(coco_dir, yaml_file)
  41. print("Processed class file")
  42. print("Finished processing COCO dataset")
  43. if __name__ == "__main__":
  44. main()