def accumulate(self, scoremap, image_id): """ Score histograms over the score map values at GT positive and negative pixels are computed. Args: scoremap: numpy.ndarray(size=(H, W), dtype=np.float) image_id: string. """ check_scoremap_validity(scoremap) gt_mask = get_mask(self.mask_root, self.mask_paths[image_id], self.ignore_paths[image_id]) gt_true_scores = scoremap[gt_mask == 1] gt_false_scores = scoremap[gt_mask == 0] # histograms in ascending order gt_true_hist, _ = np.histogram(gt_true_scores, bins=self.threshold_list_right_edge) self.gt_true_score_hist += gt_true_hist.astype(np.float) gt_false_hist, _ = np.histogram(gt_false_scores, bins=self.threshold_list_right_edge) self.gt_false_score_hist += gt_false_hist.astype(np.float)
def compute_bboxes_from_scoremaps(scoremap, scoremap_threshold_list, multi_contour_eval=False): """ Args: scoremap: numpy.ndarray(dtype=np.float32, size=(H, W)) between 0 and 1 scoremap_threshold_list: iterable multi_contour_eval: flag for multi-contour evaluation Returns: estimated_boxes_at_each_thr: list of estimated boxes (list of np.array) at each cam threshold number_of_box_list: list of the number of boxes at each cam threshold """ check_scoremap_validity(scoremap) height, width = scoremap.shape scoremap_image = np.expand_dims((scoremap * 255).astype(np.uint8), 2) def scoremap2bbox(threshold): _, thr_gray_heatmap = cv2.threshold(src=scoremap_image, thresh=int(threshold * np.max(scoremap_image)), maxval=255, type=cv2.THRESH_BINARY) contours = cv2.findContours( image=thr_gray_heatmap, mode=cv2.RETR_TREE, method=cv2.CHAIN_APPROX_SIMPLE)[_CONTOUR_INDEX] if len(contours) == 0: return np.asarray([[0, 0, 0, 0]]), 1 if not multi_contour_eval: contours = [max(contours, key=cv2.contourArea)] estimated_boxes = [] for contour in contours: x, y, w, h = cv2.boundingRect(contour) x0, y0, x1, y1 = x, y, x + w, y + h x1 = min(x1, width - 1) y1 = min(y1, height - 1) estimated_boxes.append([x0, y0, x1, y1]) return np.asarray(estimated_boxes), len(contours) estimated_boxes_at_each_thr = [] number_of_box_list = [] for threshold in scoremap_threshold_list: boxes, number_of_box = scoremap2bbox(threshold) estimated_boxes_at_each_thr.append(boxes) number_of_box_list.append(number_of_box) return estimated_boxes_at_each_thr, number_of_box_list
def compute_bboxes_from_scoremaps(scoremap, scoremap_threshold_list): """ Args: scoremap: numpy.ndarray(dtype=np.float32, size=(H, W)) between 0 and 1 scoremap_threshold_list: iterable Returns: boxes: list of estimated boxes (list of ints) at each cam threshold """ check_scoremap_validity(scoremap) height, width = scoremap.shape scoremap_image = np.expand_dims((scoremap * 255).astype(np.uint8), 2) def scoremap2bbox(threshold): _, thr_gray_heatmap = cv2.threshold(src=scoremap_image, thresh=int(threshold * np.max(scoremap_image)), maxval=255, type=cv2.THRESH_BINARY) contours = cv2.findContours(image=thr_gray_heatmap, mode=cv2.RETR_TREE, method=cv2.CHAIN_APPROX_SIMPLE)[1] if len(contours) == 0: return [0, 0, 0, 0] c = max(contours, key=cv2.contourArea) x, y, w, h = cv2.boundingRect(c) x0, y0, x1, y1 = x, y, x + w, y + h x1 = min(x1, width - 1) y1 = min(y1, height - 1) return [x0, y0, x1, y1] estimated_bbox = [ scoremap2bbox(threshold) for threshold in scoremap_threshold_list ] return estimated_bbox