a_longshort.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import json
  2. import os
  3. import cv2
  4. import shutil
  5. # 输入文件夹
  6. json_folder = r"\\192.168.50.222\share\zyh\data\via\via_json"
  7. image_folder = r"\\192.168.50.222\share\zqy\colorphoto_for_guanban"
  8. output_folder = r"\\192.168.50.222\share\zyh\data\rgb_tuoyuan_axis_bbox\source"
  9. os.makedirs(output_folder, exist_ok=True)
  10. for json_file in os.listdir(json_folder):
  11. if not json_file.lower().endswith(".json"):
  12. continue
  13. json_path = os.path.join(json_folder, json_file)
  14. with open(json_path, "r", encoding="utf-8") as f:
  15. data = json.load(f)
  16. img_metadata = data.get("_via_img_metadata", {})
  17. for img_info in img_metadata.values():
  18. filename = img_info.get("filename")
  19. regions = img_info.get("regions", [])
  20. # 只取椭圆
  21. ellipses = [r for r in regions if r.get("shape_attributes", {}).get("name") == "ellipse"]
  22. if not ellipses:
  23. continue
  24. # 找图片路径
  25. img_path = os.path.join(image_folder, filename)
  26. if not os.path.exists(img_path):
  27. print(f"图片未找到: {filename}")
  28. continue
  29. # 读取图片尺寸
  30. img = cv2.imread(img_path)
  31. if img is None:
  32. print(f"图片无法读取: {filename}")
  33. continue
  34. height, width = img.shape[:2]
  35. # 构造输出 JSON
  36. output_data = {
  37. "version": "5.0.1",
  38. "flags": {},
  39. "shapes": [],
  40. "imagePath": filename,
  41. "imageHeight": height,
  42. "imageWidth": width
  43. }
  44. for e in ellipses:
  45. cx = e["shape_attributes"]["cx"]
  46. cy = e["shape_attributes"]["cy"]
  47. rx = e["shape_attributes"]["rx"]
  48. ry = e["shape_attributes"]["ry"]
  49. # 四个端点
  50. left = [cx - rx, cy]
  51. right = [cx + rx, cy]
  52. top = [cx, cy - ry]
  53. bottom = [cx, cy + ry]
  54. # bbox
  55. xmin = min(left[0], right[0], top[0], bottom[0])
  56. xmax = max(left[0], right[0], top[0], bottom[0])
  57. ymin = min(left[1], right[1], top[1], bottom[1])
  58. ymax = max(left[1], right[1], top[1], bottom[1])
  59. # 长轴(水平)
  60. long_axis = [left, right]
  61. # 短轴(垂直)
  62. short_axis = [top, bottom]
  63. # 保存长轴
  64. output_data["shapes"].append({
  65. "label": "long_axis",
  66. "points": long_axis,
  67. "shape_type": "line",
  68. "flags": {},
  69. "xmin": int(xmin),
  70. "ymin": int(ymin),
  71. "xmax": int(xmax),
  72. "ymax": int(ymax)
  73. })
  74. # 保存短轴
  75. output_data["shapes"].append({
  76. "label": "short_axis",
  77. "points": short_axis,
  78. "shape_type": "line",
  79. "flags": {},
  80. "xmin": int(xmin),
  81. "ymin": int(ymin),
  82. "xmax": int(xmax),
  83. "ymax": int(ymax)
  84. })
  85. # 保存 JSON
  86. json_output_path = os.path.join(output_folder, filename.replace(".jpg", ".json"))
  87. with open(json_output_path, "w", encoding="utf-8") as out_f:
  88. json.dump(output_data, out_f, ensure_ascii=False, indent=4)
  89. # 复制图片
  90. img_output_path = os.path.join(output_folder, filename)
  91. shutil.copy(img_path, img_output_path)
  92. print(f"已生成: {json_output_path} 和 {img_output_path}")