Пример #1
0
def postprocess(result, img_meta, num_classes=81, rescale=True):
    det_bboxes = result['boxes']
    det_labels = result['labels']
    det_masks = result.get('masks', None)

    if rescale:
        img_h, img_w = img_meta[0]['ori_shape'][:2]
        scale = img_meta[0]['scale_factor']
        det_bboxes[:, :4] /= scale
    else:
        img_h, img_w = img_meta[0]['img_shape'][:2]

    det_bboxes[:, 0:4:2] = np.clip(det_bboxes[:, 0:4:2], 0, img_w - 1)
    det_bboxes[:, 1:4:2] = np.clip(det_bboxes[:, 1:4:2], 0, img_h - 1)

    bbox_results = bbox2result(det_bboxes, det_labels, num_classes)
    if det_masks is not None:
        segm_results = mask2result(
            det_bboxes,
            det_labels,
            det_masks,
            num_classes,
            mask_thr_binary=0.5,
            rle=True,
            full_size=True,
            img_size=(img_h, img_w))
        return bbox_results, segm_results
    return bbox_results
Пример #2
0
    def postprocess(self,
                    det_bboxes,
                    det_labels,
                    det_masks,
                    img_meta,
                    rescale=False):
        img_h, img_w = img_meta[0]['ori_shape'][:2]
        num_classes = self.bbox_head.num_classes
        scale_factor = img_meta[0]['scale_factor']
        if isinstance(scale_factor, float):
            scale_factor = np.asarray((scale_factor, ) * 4)

        if rescale:
            # Keep original image resolution unchanged
            # and scale bboxes and masks to it.
            if isinstance(det_bboxes, torch.Tensor):
                scale_factor = det_bboxes.new_tensor(scale_factor)
            det_bboxes[:, :4] /= scale_factor
        else:
            # Resize image to test resolution
            # and keep bboxes and masks in test scale.
            img_h = np.round(img_h * scale_factor[1]).astype(np.int32)
            img_w = np.round(img_w * scale_factor[0]).astype(np.int32)

        bbox_results = bbox2result(det_bboxes, det_labels, num_classes)
        if self.with_mask:
            segm_results = mask2result(
                det_bboxes,
                det_labels,
                det_masks,
                num_classes,
                mask_thr_binary=self.test_cfg.rcnn.mask_thr_binary,
                rle=True,
                full_size=True,
                img_size=(img_h, img_w))
            return bbox_results, segm_results

        return bbox_results