wyo.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import os
  2. import json
  3. import shutil
  4. from tqdm import tqdm
  5. # ======================
  6. # ⚙️ 可配置路径
  7. # ======================
  8. # 原始数据目录(Labelme)
  9. INPUT_DIR = r"\\192.168.50.222\share\zyh\data\坡口切割\10-11"
  10. # 输出 YOLO 数据集目录(标准格式)
  11. OUTPUT_DIR =r"\\192.168.50.222\share\zyh\data\坡口切割\yolo"
  12. # ======================
  13. # 🚀 核心函数
  14. # ======================
  15. def convert_labelme_to_yolo(json_path, output_txt, image_w, image_h):
  16. """将 Labelme JSON 转为 YOLO txt"""
  17. with open(json_path, "r", encoding="utf-8") as f:
  18. data = json.load(f)
  19. lines = []
  20. for shape in data.get("shapes", []):
  21. if shape["label"] != "line":
  22. continue
  23. points = shape["points"]
  24. if len(points) != 2:
  25. continue
  26. x1, y1 = points[0]
  27. x2, y2 = points[1]
  28. # 归一化
  29. x1 /= image_w
  30. y1 /= image_h
  31. x2 /= image_w
  32. y2 /= image_h
  33. lines.append(f"0 {x1:.6f} {y1:.6f} {x2:.6f} {y2:.6f}")
  34. os.makedirs(os.path.dirname(output_txt), exist_ok=True)
  35. with open(output_txt, "w", encoding="utf-8") as f:
  36. f.write("\n".join(lines))
  37. def process_split(split):
  38. """处理 train / val 子集"""
  39. print(f"\n📂 处理中:{split} 集")
  40. img_src_dir = os.path.join(INPUT_DIR, "images", split)
  41. json_src_dir = os.path.join(INPUT_DIR, "labels", split)
  42. img_dst_dir = os.path.join(OUTPUT_DIR, split, "images")
  43. label_dst_dir = os.path.join(OUTPUT_DIR, split, "labels")
  44. os.makedirs(img_dst_dir, exist_ok=True)
  45. os.makedirs(label_dst_dir, exist_ok=True)
  46. json_files = [f for f in os.listdir(json_src_dir) if f.endswith(".json")]
  47. for json_file in tqdm(json_files):
  48. json_path = os.path.join(json_src_dir, json_file)
  49. with open(json_path, "r", encoding="utf-8") as f:
  50. data = json.load(f)
  51. image_name = data["imagePath"]
  52. image_w = data["imageWidth"]
  53. image_h = data["imageHeight"]
  54. # 图片路径
  55. src_img_path = os.path.join(img_src_dir, image_name)
  56. dst_img_path = os.path.join(img_dst_dir, image_name)
  57. if not os.path.exists(src_img_path):
  58. print(f"⚠️ 缺失图片:{src_img_path}")
  59. continue
  60. # 拷贝图片
  61. shutil.copy2(src_img_path, dst_img_path)
  62. # 生成 YOLO txt
  63. txt_name = os.path.splitext(image_name)[0] + ".txt"
  64. txt_path = os.path.join(label_dst_dir, txt_name)
  65. convert_labelme_to_yolo(json_path, txt_path, image_w, image_h)
  66. def generate_data_yaml():
  67. """生成 data.yaml 文件"""
  68. yaml_path = os.path.join(OUTPUT_DIR, "data.yaml")
  69. yaml_content = f"""path: {OUTPUT_DIR}
  70. train: train/images
  71. val: val/images
  72. names:
  73. 0: line
  74. """
  75. with open(yaml_path, "w", encoding="utf-8") as f:
  76. f.write(yaml_content)
  77. print(f"\n✅ 已生成 data.yaml: {yaml_path}")
  78. if __name__ == "__main__":
  79. print("🚀 开始转换 Labelme → YOLO 数据集 (标准格式)")
  80. process_split("train")
  81. process_split("val")
  82. generate_data_yaml()
  83. print("\n🎉 完成!YOLO 数据集已生成于:", OUTPUT_DIR)