Beispiel #1
0
 def _resize_bboxes(self, results):
     img_shape = results['img_shape']
     for key in results.get('bbox_fields', []):
         if results[key].shape[-1] == 5:
             polys = rotated_box_to_poly_np(results[key])  # to 8 points
         elif results[key].shape[-1] == 8:
             polys = results[key]  # it's already 8 points
         else:
             polys = rotated_box_to_poly_np(results[key])  # to 8 points
         polys = polys * results['scale_factor']
         if polys.shape[0] != 0:
             polys[:, 0::2] = np.clip(polys[:, 0::2], 0, img_shape[1] - 1)
             polys[:, 1::2] = np.clip(polys[:, 1::2], 0, img_shape[0] - 1)
         rboxes = poly_to_rotated_box_np(polys)  # to x,y,w,h,angle
         results[key] = rboxes
Beispiel #2
0
    def get_visualization(self, input_img, classes, test_cfg):
        stitched = "nonew3"
        batch_size = input_img.shape[0]
        img = tensor2imgs(input_img, **self.last_vals['img_metas'][0]
                          ['img_norm_cfg'])[0]  #get input image
        from PIL import Image
        #Image.fromarray(img).show()
        from mmdet.core import rotated_box_to_poly_np
        gt = rotated_box_to_poly_np(
            self.last_vals['gt_bboxes'][0].cpu().numpy())
        #TODO classes are wrong for deepscores
        img_gt = imshow_det_bboxes(
            img.copy(),
            gt,
            self.last_vals['gt_labels'][0].cpu().numpy(),
            class_names=classes,
            show=False,
            show_label=True,
            rotated=True)

        det_boxes_labels = self.get_bboxes(
            fam_cls_scores=self.last_vals['fam_cls_scores'],
            fam_bbox_preds=self.last_vals['fam_bbox_preds'],
            refine_anchors=self.last_vals['refine_anchors'],
            odm_cls_scores=self.last_vals['odm_cls_scores'],
            odm_bbox_preds=self.last_vals['odm_bbox_preds'],
            img_metas=self.last_vals['img_metas'],
            cfg=test_cfg)[0]
        det_boxes = rotated_box_to_poly_np(det_boxes_labels[0].cpu().numpy())
        det_labels = det_boxes_labels[1].cpu().numpy()
        if len(det_boxes) > 0:
            img_det = imshow_det_bboxes(img.copy(),
                                        det_boxes,
                                        det_labels.astype(np.int) + 1,
                                        class_names=classes,
                                        show=False,
                                        show_label=True,
                                        rotated=True)
        else:
            img_det = img.copy()
        stitched = vt.stitch_big_image([[img_gt], [img_det]])

        return [{"name": "stitched_img", "image": stitched}]
Beispiel #3
0
    def __call__(self, results):
        # return the results directly if not rotate
        if not self.is_rotate:
            results['rotate'] = False
            return results

        h, w, c = results['img_shape']
        img = results['img']
        # angle for rotate
        angle = self.rand_angle
        results['rotate'] = True
        results['rotate_angle'] = angle

        image_center = np.array((w / 2, h / 2))
        abs_cos, abs_sin = abs(np.cos(angle)), abs(np.sin(angle))
        if self.auto_bound:
            # find the new width and height bounds
            bound_w, bound_h = np.rint(
                [h * abs_sin + w * abs_cos,
                 h * abs_cos + w * abs_sin]).astype(int)
        else:
            bound_w, bound_h = w, h

        self.rm_coords = self.create_rotation_matrix(image_center, angle,
                                                     bound_h, bound_w)
        # Needed because of this problem https://github.com/opencv/opencv/issues/11784
        self.rm_image = self.create_rotation_matrix(image_center,
                                                    angle,
                                                    bound_h,
                                                    bound_w,
                                                    offset=-0.5)
        # rotate img
        img = self.apply_image(img, bound_h, bound_w)
        results['img'] = img
        results['img_shape'] = (bound_h, bound_w, c)
        # rotate bboxes
        gt_bboxes = results.get('gt_bboxes', [])
        labels = results.get('gt_labels', [])

        polys = rotated_box_to_poly_np(gt_bboxes).reshape(-1, 2)
        polys = self.apply_coords(polys).reshape(-1, 8)
        gt_bboxes = poly_to_rotated_box_np(polys)
        keep_inds = self.filter_border(gt_bboxes, bound_h, bound_w)
        gt_bboxes = gt_bboxes[keep_inds, :]
        labels = labels[keep_inds]
        if len(gt_bboxes) == 0:
            return None
        results['gt_bboxes'] = gt_bboxes
        results['gt_labels'] = labels
        return results
Beispiel #4
0
images_folder = "/home/tugg/Documents/RealScores/Realworld_Test"

resize = 1.0

model = init_detector(config_file, checkpoint_file, device='cuda:0')

images = os.listdir(images_folder)
os.makedirs(os.path.join(images_folder, model_name), exist_ok=True)

for img in images:
    if os.path.isdir(os.path.join(images_folder, img)):
        continue
    img_loaded = cv2.imread(os.path.join(images_folder, img))
    img_loaded = cv2.resize(
        img_loaded,
        (int(img_loaded.shape[1] * resize), int(img_loaded.shape[0] * resize)),
        interpolation=cv2.INTER_AREA)
    result = inference_detector(model, img_loaded)

    det_boxes = rotated_box_to_poly_np(result[1][0][0].cpu().numpy())
    det_labels = result[1][0][1].cpu().numpy()

    img_det = imshow_det_bboxes(img_loaded,
                                det_boxes,
                                det_labels.astype(int) + 1,
                                class_names=CLASSES,
                                show=False,
                                show_label=False,
                                rotated=True)

    cv2.imwrite(os.path.join(images_folder, model_name, img), img_det)