shape.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import math
  2. import uuid
  3. import numpy as np
  4. import PIL.Image
  5. import PIL.ImageDraw
  6. from labelme.logger import logger
  7. def polygons_to_mask(img_shape, polygons, shape_type=None):
  8. logger.warning(
  9. "The 'polygons_to_mask' function is deprecated, " "use 'shape_to_mask' instead."
  10. )
  11. return shape_to_mask(img_shape, points=polygons, shape_type=shape_type)
  12. def shape_to_mask(img_shape, points, shape_type=None, line_width=10, point_size=5):
  13. mask = np.zeros(img_shape[:2], dtype=np.uint8)
  14. mask = PIL.Image.fromarray(mask)
  15. draw = PIL.ImageDraw.Draw(mask)
  16. xy = [tuple(point) for point in points]
  17. if shape_type == "circle":
  18. assert len(xy) == 2, "Shape of shape_type=circle must have 2 points"
  19. (cx, cy), (px, py) = xy
  20. d = math.sqrt((cx - px) ** 2 + (cy - py) ** 2)
  21. draw.ellipse([cx - d, cy - d, cx + d, cy + d], outline=1, fill=1)
  22. elif shape_type == "rectangle":
  23. assert len(xy) == 2, "Shape of shape_type=rectangle must have 2 points"
  24. draw.rectangle(xy, outline=1, fill=1)
  25. elif shape_type == "line":
  26. assert len(xy) == 2, "Shape of shape_type=line must have 2 points"
  27. draw.line(xy=xy, fill=1, width=line_width)
  28. elif shape_type == "linestrip":
  29. draw.line(xy=xy, fill=1, width=line_width)
  30. elif shape_type == "point":
  31. assert len(xy) == 1, "Shape of shape_type=point must have 1 points"
  32. cx, cy = xy[0]
  33. r = point_size
  34. draw.ellipse([cx - r, cy - r, cx + r, cy + r], outline=1, fill=1)
  35. else:
  36. assert len(xy) > 2, "Polygon must have points more than 2"
  37. draw.polygon(xy=xy, outline=1, fill=1)
  38. mask = np.array(mask, dtype=bool)
  39. return mask
  40. def shapes_to_label(img_shape, shapes, label_name_to_value):
  41. cls = np.zeros(img_shape[:2], dtype=np.int32)
  42. ins = np.zeros_like(cls)
  43. instances = []
  44. for shape in shapes:
  45. points = shape["points"]
  46. label = shape["label"]
  47. group_id = shape.get("group_id")
  48. if group_id is None:
  49. group_id = uuid.uuid1()
  50. shape_type = shape.get("shape_type", None)
  51. cls_name = label
  52. instance = (cls_name, group_id)
  53. if instance not in instances:
  54. instances.append(instance)
  55. ins_id = instances.index(instance) + 1
  56. cls_id = label_name_to_value[cls_name]
  57. mask = shape_to_mask(img_shape[:2], points, shape_type)
  58. cls[mask] = cls_id
  59. ins[mask] = ins_id
  60. return cls, ins
  61. def labelme_shapes_to_label(img_shape, shapes):
  62. logger.warn(
  63. "labelme_shapes_to_label is deprecated, so please use " "shapes_to_label."
  64. )
  65. label_name_to_value = {"_background_": 0}
  66. for shape in shapes:
  67. label_name = shape["label"]
  68. if label_name in label_name_to_value:
  69. label_value = label_name_to_value[label_name]
  70. else:
  71. label_value = len(label_name_to_value)
  72. label_name_to_value[label_name] = label_value
  73. lbl, _ = shapes_to_label(img_shape, shapes, label_name_to_value)
  74. return lbl, label_name_to_value
  75. def masks_to_bboxes(masks):
  76. if masks.ndim != 3:
  77. raise ValueError("masks.ndim must be 3, but it is {}".format(masks.ndim))
  78. if masks.dtype != bool:
  79. raise ValueError(
  80. "masks.dtype must be bool type, but it is {}".format(masks.dtype)
  81. )
  82. bboxes = []
  83. for mask in masks:
  84. where = np.argwhere(mask)
  85. (y1, x1), (y2, x2) = where.min(0), where.max(0) + 1
  86. bboxes.append((y1, x1, y2, x2))
  87. bboxes = np.asarray(bboxes, dtype=np.float32)
  88. return bboxes