cs_readv.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import os
  2. import csv
  3. import json
  4. import math
  5. import ast
  6. import shutil
  7. def ellipse_to_box_points(cx, cy, rx, ry, theta):
  8. """
  9. ½«ÍÖÔ²²ÎÊýת»»Îª¾ØÐΣ¨ÉÏÏÂ×óÓÒ4µã£©
  10. ½üËÆ·½·¨£ºÓÃÐýת¾ØÐεÄ4¸ö¶¥µã±íʾÍÖÔ²±ß½ç
  11. """
  12. # Ëĸöµã£¨×óÓÒÖе㣬ÉÏÏÂÖе㣩
  13. points = [
  14. [cx - rx, cy], # ×ó
  15. [cx + rx, cy], # ÓÒ
  16. [cx, cy - ry], # ÉÏ
  17. [cx, cy + ry], # ÏÂ
  18. ]
  19. # ÐýתÕâЩµã
  20. cos_t = math.cos(theta)
  21. sin_t = math.sin(theta)
  22. rotated_points = []
  23. for x, y in points:
  24. x_new = (x - cx) * cos_t - (y - cy) * sin_t + cx
  25. y_new = (x - cx) * sin_t + (y - cy) * cos_t + cy
  26. rotated_points.append([x_new, y_new])
  27. return rotated_points
  28. def csv_to_labelme_json(csv_path, img_folder, output_folder, image_exts=(".png", ".tiff")):
  29. os.makedirs(output_folder, exist_ok=True)
  30. with open(csv_path, newline='', encoding='utf-8') as csvfile:
  31. reader = csv.DictReader(csvfile)
  32. for row in reader:
  33. filename = row["filename"]
  34. region_shape_attributes = row["region_shape_attributes"]
  35. # ת³É×ֵ䣨CSV ÀïÊÇ×Ö·û´®£©
  36. try:
  37. attr = json.loads(region_shape_attributes)
  38. except json.JSONDecodeError:
  39. attr = ast.literal_eval(region_shape_attributes)
  40. if attr.get("name") != "ellipse":
  41. continue
  42. cx, cy = float(attr["cx"]), float(attr["cy"])
  43. rx, ry = float(attr["rx"]), float(attr["ry"])
  44. theta = float(attr["theta"])
  45. # ÍÖÔ²Ëĸöµã
  46. points = ellipse_to_box_points(cx, cy, rx, ry, theta)
  47. # »ñÈ¡xmin, ymin, xmax, ymax
  48. xs = [p[0] for p in points]
  49. ys = [p[1] for p in points]
  50. xmin, ymin, xmax, ymax = int(min(xs)), int(min(ys)), int(max(xs)), int(max(ys))
  51. # Éú³É Labelme ¸ñʽ
  52. data = {
  53. "version": "5.0.1",
  54. "flags": {},
  55. "shapes": [
  56. {
  57. "label": "circle",
  58. "points": points,
  59. "shape_type": "polygon",
  60. "flags": {},
  61. "xmin": xmin,
  62. "ymin": ymin,
  63. "xmax": xmax,
  64. "ymax": ymax,
  65. }
  66. ],
  67. "imagePath": filename.replace(".png", ".jpg").replace(".tiff", ".jpg"),
  68. "imageHeight": 2000, # Èç¹ûÄãÖªµÀʵ¼ÊͼÏñ³ß´ç¿ÉÒÔ¸Ä
  69. "imageWidth": 2000
  70. }
  71. # дÈë JSON Îļþ
  72. json_filename = os.path.splitext(filename)[0] + ".json"
  73. json_path = os.path.join(output_folder, json_filename)
  74. with open(json_path, "w", encoding="utf-8") as f:
  75. json.dump(data, f, indent=4, ensure_ascii=False)
  76. # ¿½±´Í¼Æ¬Îļþ
  77. src_img_path = None
  78. for ext in image_exts:
  79. test_path = os.path.join(img_folder, filename.replace(".png", ext).replace(".tiff", ext))
  80. if os.path.exists(test_path):
  81. src_img_path = test_path
  82. break
  83. if src_img_path:
  84. dst_img_path = os.path.join(output_folder, os.path.basename(src_img_path))
  85. shutil.copy(src_img_path, dst_img_path)
  86. else:
  87. print(f"?? ÕÒ²»µ½Í¼Æ¬: {filename}")
  88. print(f"? ת»»Íê³É£¬Êä³ö·¾¶£º{output_folder}")
  89. if __name__ == "__main__":
  90. # ÊäÈëÎļþ¼Ð
  91. csv_folder = "/data/share/zyh/数据集汇总/circle/huayan_circle/csv"
  92. img_folder = "/data/share/zyh/数据集汇总/circle/huayan_circle/彩色图"
  93. output_folder = "/home/zhaoyinghan/py_ws/data/circle/huayan"
  94. for csv_file in os.listdir(csv_folder):
  95. if csv_file.endswith(".csv"):
  96. csv_path = os.path.join(csv_folder, csv_file)
  97. csv_to_labelme_json(csv_path, img_folder, output_folder)