clean_label.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import json
  2. import os
  3. import random
  4. import shutil
  5. import argparse
  6. from tqdm import tqdm
  7. def clean_label_files(labels_dir, output_labels_dir, log_file):
  8. if not os.path.exists(output_labels_dir):
  9. os.makedirs(output_labels_dir)
  10. processed_files = []
  11. label_files = os.listdir(labels_dir)
  12. with tqdm(total=len(label_files), desc="Processing files", unit="file") as pbar:
  13. for label_file in label_files: # 遍历每一个txt文件名
  14. file_path = os.path.join(labels_dir, label_file) # 拼接得到旧的txt文件地址
  15. output_file_path = os.path.join(output_labels_dir, label_file) # 新的txt文件地址
  16. _, file_extension = os.path.splitext(file_path)
  17. if not file_extension.lower() == ".txt":
  18. continue
  19. try:
  20. with open(file_path, 'r') as f: # 打开旧的txt文件
  21. lines = f.readlines() # 得到文件内容
  22. new_lines = []
  23. file_needs_logging = False
  24. for line in lines: # 遍历处理文件内容
  25. parts = line.strip().split()
  26. if parts:
  27. first_num = int(parts[0])
  28. other_nums = [float(num) for num in parts[1:]]
  29. if first_num not in [4, 20] and all(
  30. 0 <= num <= 1 for num in other_nums): # 只有当一行内容的第一个数字不是4或20,其他数字都在[0,1]之间时这一行内容才能被保留
  31. new_lines.append(line)
  32. elif not all(0 <= num <= 1 for num in other_nums): # 如果不满足上诉条件且有数字不在【0,1】的,要记录文件名
  33. file_needs_logging = True
  34. if file_needs_logging:
  35. processed_files.append(label_file)
  36. with open(output_file_path, 'w') as f: # 将文本内容写入新文件夹中
  37. f.writelines(new_lines)
  38. except Exception as e:
  39. print(f"发生错误: {e}")
  40. pbar.update(1) # 更新进度条
  41. with open(log_file, 'w') as log:
  42. for file_name in processed_files:
  43. log.write(f"{file_name}\n")
  44. if __name__ == '__main__':
  45. parser = argparse.ArgumentParser("删除特定标注信息\n")
  46. parser.add_argument("image_dir", help="图片文件夹路径")
  47. parser.add_argument("--log_file", default="processed_files.log", help="记录被处理文件的日志文件路径")
  48. args = parser.parse_args()
  49. output_labels_dir = os.path.join(args.image_dir, 'processed_labels') # 生成的新的labels文件夹
  50. log_file_dir = os.path.join(args.image_dir, args.log_file) # 日志文件
  51. clean_label_files(os.path.join(args.image_dir, 'labels'), output_labels_dir, log_file_dir)