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

Method _draw_text_in_mask

utils/visualizer.py:1250–1268  ·  view source on GitHub ↗

Find proper places to draw text given a binary mask.

(self, binary_mask, text, color)

Source from the content-addressed store, hash-verified

1248 return ret
1249
1250 def _draw_text_in_mask(self, binary_mask, text, color):
1251 """
1252 Find proper places to draw text given a binary mask.
1253 """
1254 # TODO sometimes drawn on wrong objects. the heuristics here can improve.
1255 _num_cc, cc_labels, stats, centroids = cv2.connectedComponentsWithStats(binary_mask, 8)
1256 if stats[1:, -1].size == 0:
1257 return
1258 largest_component_id = np.argmax(stats[1:, -1]) + 1
1259
1260 # draw text on the largest component, as well as other very large components.
1261 for cid in range(1, _num_cc):
1262 if cid == largest_component_id or stats[cid, -1] > _LARGE_MASK_AREA_THRESH:
1263 # median is more stable than centroid
1264 # center = centroids[largest_component_id]
1265 center = np.median((cc_labels == cid).nonzero(), axis=1)[::-1]
1266 bottom=np.max((cc_labels == cid).nonzero(), axis=1)[::-1]
1267 center[1]=bottom[1]+2
1268 self.draw_text(text, center, color=color)
1269
1270 def _convert_keypoints(self, keypoints):
1271 if isinstance(keypoints, Keypoints):

Callers 2

draw_binary_maskMethod · 0.95
draw_soft_maskMethod · 0.95

Calls 1

draw_textMethod · 0.95

Tested by

no test coverage detected