MCPcopy Create free account
hub / github.com/UX-Decoder/Semantic-SAM / GenericMask

Class GenericMask

utils/visualizer.py:59–152  ·  view source on GitHub ↗

Attribute: polygons (list[ndarray]): list[ndarray]: polygons for this mask. Each ndarray has format [x, y, x, y, ...] mask (ndarray): a binary mask

Source from the content-addressed store, hash-verified

57
58
59class GenericMask:
60 """
61 Attribute:
62 polygons (list[ndarray]): list[ndarray]: polygons for this mask.
63 Each ndarray has format [x, y, x, y, ...]
64 mask (ndarray): a binary mask
65 """
66
67 def __init__(self, mask_or_polygons, height, width):
68 self._mask = self._polygons = self._has_holes = None
69 self.height = height
70 self.width = width
71
72 m = mask_or_polygons
73 if isinstance(m, dict):
74 # RLEs
75 assert "counts" in m and "size" in m
76 if isinstance(m["counts"], list): # uncompressed RLEs
77 h, w = m["size"]
78 assert h == height and w == width
79 m = mask_util.frPyObjects(m, h, w)
80 self._mask = mask_util.decode(m)[:, :]
81 return
82
83 if isinstance(m, list): # list[ndarray]
84 self._polygons = [np.asarray(x).reshape(-1) for x in m]
85 return
86
87 if isinstance(m, np.ndarray): # assumed to be a binary mask
88 assert m.shape[1] != 2, m.shape
89 assert m.shape == (
90 height,
91 width,
92 ), f"mask shape: {m.shape}, target dims: {height}, {width}"
93 self._mask = m.astype("uint8")
94 return
95
96 raise ValueError("GenericMask cannot handle object {} of type '{}'".format(m, type(m)))
97
98 @property
99 def mask(self):
100 if self._mask is None:
101 self._mask = self.polygons_to_mask(self._polygons)
102 return self._mask
103
104 @property
105 def polygons(self):
106 if self._polygons is None:
107 self._polygons, self._has_holes = self.mask_to_polygons(self._mask)
108 return self._polygons
109
110 @property
111 def has_holes(self):
112 if self._has_holes is None:
113 if self._mask is not None:
114 self._polygons, self._has_holes = self.mask_to_polygons(self._mask)
115 else:
116 self._has_holes = False # if original format is polygon, does not have holes

Callers 3

draw_binary_maskMethod · 0.85
_convert_masksMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected