shape.py 3.6 KB

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