def _heatmap_to_rects(self, grid_pred, bb_img):
"""Convert a heatmap to rectangles / bounding box candidates."""
grid_pred = np.squeeze(grid_pred) # (1, H, W) => (H, W)
# remove low activations
grid_thresh = grid_pred >= self.heatmap_activation_threshold
# find connected components
grid_labeled, num_labels = morphology.label(
grid_thresh, background=0, connectivity=1, return_num=True
)
# for each connected components,
# - draw a bounding box around it,
# - shrink the bounding box to optimal size
# - estimate a score/confidence value
bbs = []
for label in range(1, num_labels+1):
(yy, xx) = np.nonzero(grid_labeled == label)
min_y, max_y = np.min(yy), np.max(yy)
min_x, max_x = np.min(xx), np.max(xx)
rect = RectangleOnImage(x1=min_x, x2=max_x+1, y1=min_y, y2=max_y+1, shape=grid_labeled)
activation = self._rect_to_score(rect, grid_pred)
rect_shrunk, activation_shrunk = self._shrink(grid_pred, rect)
rect_rs_shrunk = rect_shrunk.on(bb_img)
bbs.append((rect_rs_shrunk, activation_shrunk))
return bbs
评论列表
文章目录