MCPcopy Create free account
hub / github.com/PeizeSun/SparseR-CNN / GenericMask

Class GenericMask

detectron2/utils/visualizer.py:59–149  ·  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 == (height, width), m.shape
90 self._mask = m.astype("uint8")
91 return
92
93 raise ValueError("GenericMask cannot handle object {} of type '{}'".format(m, type(m)))
94
95 @property
96 def mask(self):
97 if self._mask is None:
98 self._mask = self.polygons_to_mask(self._polygons)
99 return self._mask
100
101 @property
102 def polygons(self):
103 if self._polygons is None:
104 self._polygons, self._has_holes = self.mask_to_polygons(self._mask)
105 return self._polygons
106
107 @property
108 def has_holes(self):
109 if self._has_holes is None:
110 if self._mask is not None:
111 self._polygons, self._has_holes = self.mask_to_polygons(self._mask)
112 else:
113 self._has_holes = False # if original format is polygon, does not have holes
114 return self._has_holes
115
116 def mask_to_polygons(self, mask):

Callers 3

draw_binary_maskMethod · 0.85
_convert_masksMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected