shape.py 3.2 KB

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