Example #1
0
def main():
    parser = ArgumentParser()
    parser.add_argument('--img', default="demo.jpg", help='Image file')
    parser.add_argument(
        '--config',
        # default="/home/jaychen/Desktop/PycharmProjects/2020.12.10/mmdetection_2.7/configs/retinanet/retinanet_r50_caffe_fpn_1x_coco.py",
        default=
        "/home/jaychen/Desktop/PycharmProjects/2020.12.10/mmdetection_2.7/configs/rpn/rpn_r50_fpn_2x_coco.py",
        help='Config file')
    parser.add_argument(
        '--checkpoint',
        # default="/home/jaychen/Desktop/MODELWEIGHTS/retinanet_r50_caffe_fpn_1x_coco_20200531-f11027c5.pth",
        default="../checkpoints/rpn_r50_fpn_1x_coco_20200218-5525fa2e.pth",
        help='Checkpoint file')
    parser.add_argument('--device',
                        default='cuda:0',
                        help='Device used for inference')
    parser.add_argument('--score-thr',
                        type=float,
                        default=0.3,
                        help='bbox score threshold')
    args = parser.parse_args()

    # build the model from a config file and a checkpoint file
    model = init_detector(args.config, args.checkpoint, device=args.device)
    # test a single image
    result = inference_detector(model, args.img)
    # show the results
    # show_result_pyplot(model, args.img, result, score_thr=args.score_thr)
    # show RPN result
    img_show = mmcv.imread(args.img)
    mmcv.imshow_bboxes(img_show, result, top_k=20)
    def visualize_gt_bboxes(self, imgs, gt_bboxes, img_meta):
        gt_bboxes_np = gt_bboxes[0].cpu().numpy()

        for bbox in gt_bboxes_np:
            for slice_num in range(int(bbox[4]), int(bbox[5])):
                img = tensor2img3D(imgs, slice_num=slice_num)
                filename = 'tests/iter_{}_img_id_{}_slice_{}.png'.format(self.iteration, img_meta[0]['image_id'], slice_num)
                mmcv.imshow_bboxes(img, np.array([bbox]), show=False, out_file=filename)
Example #3
0
 def visualize_gt_bboxes(self, imgs, gt_bboxes, img_meta):
     imgs = tensor2imgs(imgs, mean=(123.675, 116.28, 103.53), std=(58.395, 57.12, 57.375))
     batch_num = 1
     for img, gt_bbox in zip(imgs, gt_bboxes):
         filename = 'tests/iter_{}_batchnum_{}.png'.format(self.iteration, batch_num)
         mmcv.imshow_bboxes(img, gt_bbox.cpu().numpy(), show=False, out_file=filename)
         batch_num += 1
     breakpoint()
Example #4
0
 def show_result(self, data, result, img_norm_cfg, dataset=None, top_k=20):
     """Show RPN proposals on the image.
     Although we assume batch size is 1, this method supports arbitrary
     batch size.
     """
     img_tensor = data['img'][0]
     img_metas = data['img_meta'][0].data[0]
     imgs = tensor2imgs(img_tensor, **img_norm_cfg)
     assert len(imgs) == len(img_metas)
     for img, img_meta in zip(imgs, img_metas):
         h, w, _ = img_meta['img_shape']
         img_show = img[:h, :w, :]
         mmcv.imshow_bboxes(img_show, result, top_k=top_k)
Example #5
0
    def show_result(self, data, result, top_k=20, **kwargs):
        """Show RPN proposals on the image.

        Args:
            data (str or np.ndarray): Image filename or loaded image.
            result (Tensor or tuple): The results to draw over `img`
                bbox_result or (bbox_result, segm_result).
            top_k (int): Plot the first k bboxes only
               if set positive. Default: 20

        Returns:
            np.ndarray: The image with bboxes drawn on it.
        """
        mmcv.imshow_bboxes(data, result, top_k=top_k)
Example #6
0
def filter(dets):

    det_bboxes = np.vstack(dets)

    adj = np.zeros((len(det_bboxes), len(det_bboxes)), int)
    for i in range(len(det_bboxes)):
        for j in range(i):
            boxA = det_bboxes[i]
            boxB = det_bboxes[j]
            xA = max(boxA[0], boxB[0])
            yA = max(boxA[1], boxB[1])
            xB = min(boxA[2], boxB[2])
            yB = min(boxA[3], boxB[3])
            # compute the area of intersection rectangle
            interArea = max(0, xB - xA) * max(0, yB - yA)

            boxAArea = (boxA[2] - boxA[0] + 1) * (boxA[3] - boxA[1] + 1)
            boxBArea = (boxB[2] - boxB[0] + 1) * (boxB[3] - boxB[1] + 1)
            iou = interArea / float(boxAArea + boxBArea - interArea)
            if (iou > 0.02):
                adj[i][j] = adj[j][i] = 1
    print(adj)
    n = len(det_bboxes)
    ConSubSets = []

    for i in range(len(det_bboxes)):
        bset = []
        for j in range(len(det_bboxes)):
            if adj[i][j] == 0:
                bset.append(det_bboxes[j])
        ConSubSets.append(np.asarray(bset))

    alla, alls, fscr = [], [], []

    for sl in ConSubSets:
        areas = sum([(box[2] - box[0]) * (box[3] - box[1])
                     for box in sl]) / 500000.0
        alla.append(areas)
        # print(areas)
        bxnd = np.asarray(sl)
        scores = bxnd[:, -1]
        ms = np.mean(scores)
        # print(ms)
        alls.append(ms)
        fscr.append(np.mean(areas + ms))
        mmcv.imshow_bboxes('demo/minival/PMC4982048_00021.jpg', bxnd)

    idx = fscr.index(max(fscr))
    mmcv.imshow_bboxes('demo/minival/PMC4982048_00021.jpg',
                       np.asarray(ConSubSets[idx]))
Example #7
0
    def show_result(self,
                    img,
                    result,
                    color='green',
                    thickness=1,
                    show=False,
                    win_name='',
                    wait_time=0,
                    out_file=None,
                    **kwargs):
        """Visualize tracking results.

        Args:
            img (str or ndarray): The image to be displayed.
            result (dict): Tracking result.
                The value of key 'track_results' is ndarray with shape (5, )
                in [tl_x, tl_y, br_x, br_y, score] format.
            color (str or tuple or Color, optional): color of bbox.
                Defaults to green.
            thickness (int, optional): Thickness of lines.
                Defaults to 1.
            show (bool, optional): Whether to show the image.
                Defaults to False.
            win_name (str, optional): The window name.
                Defaults to ''.
            wait_time (int, optional): Value of waitKey param.
                Defaults to 0.
            out_file (str, optional): The filename to write the image.
                Defaults to None.

        Returns:
            ndarray: Visualized image.
        """
        assert isinstance(result, dict)
        track_result = result.get('track_results', None)
        assert track_result.ndim == 1
        assert track_result.shape[0] == 5

        track_bbox = track_result[:4]
        mmcv.imshow_bboxes(
            img,
            track_bbox[np.newaxis, :],
            colors=color,
            thickness=thickness,
            show=show,
            win_name=win_name,
            wait_time=wait_time,
            out_file=out_file)
        return img
    def show_result(self, data):
        """Show the init proposals in EmbeddingRPN.

        Args:
            data (dict): Dict contains image and
                corresponding meta information.
        """
        img_tensor = data['img'][0]
        img_metas = data['img_metas'][0].data[0]
        imgs = tensor2imgs(img_tensor, **img_metas[0]['img_norm_cfg'])
        proposals, _ = self._decode_init_proposals(data['img'],
                                                   data['img_metas'])
        assert len(imgs) == len(img_metas)
        for img, img_meta in zip(imgs, img_metas):
            h, w, _ = img_meta['img_shape']
            img_show = img[:h, :w, :]
            mmcv.imshow_bboxes(img_show, proposals)
def tensor2img3DNPrint(tensor,
                       slice_num=85,
                       bboxes=np.array([[0, 0, 0, 0]]),
                       mean=(123.675, 116.28, 103.53),
                       std=(58.395, 57.12, 57.375),
                       to_rgb=True):
    mean = np.array(mean, dtype=np.float32)
    std = np.array(std, dtype=np.float32)

    # get the first image
    imgs = tensor[0, ...].cpu().numpy().transpose(2, 3, 1, 0)
    # get the first slice
    img = imgs[:, :, slice_num, :]
    img = mmcv.imdenormalize(img, mean, std, to_bgr=False)
    img = np.ascontiguousarray(img)
    mmcv.imshow_bboxes(img, bboxes)
    return img
Example #10
0
    def test_imshow(self, debug=True):
        """
    Usage:
        export CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7
        export TIME_STR=1
        export PYTHONPATH=./exp:./stylegan2-pytorch:./
        python 	-c "from exp.tests.test_styleganv2 import Testing_stylegan2;\
          Testing_stylegan2().test_train_ffhq_128()"

    :return:
    """
        if 'CUDA_VISIBLE_DEVICES' not in os.environ:
            os.environ['CUDA_VISIBLE_DEVICES'] = '0'
        if 'TIME_STR' not in os.environ:
            os.environ['TIME_STR'] = '0' if utils.is_debugging() else '0'
        from template_lib.v2.config_cfgnode.argparser import \
          (get_command_and_outdir, setup_outdir_and_yaml, get_append_cmd_str, start_cmd_run)

        tl_opts = ' '.join(sys.argv[sys.argv.index('--tl_opts') +
                                    1:]) if '--tl_opts' in sys.argv else ''
        print(f'tl_opts:\n {tl_opts}')

        command, outdir = get_command_and_outdir(
            self, func_name=sys._getframe().f_code.co_name, file=__file__)
        argv_str = f"""
                --tl_config_file none
                --tl_command none
                --tl_outdir {outdir}
                --tl_opts {tl_opts}
                """
        args, cfg = setup_outdir_and_yaml(argv_str, return_cfg=True)

        n_gpus = len(os.environ['CUDA_VISIBLE_DEVICES'].split(','))
        import mmcv
        import numpy as np

        img_path = "template_lib/datasets/images/zebra_GT_target_origin.png"
        mmcv.imshow(img_path)

        # show image with bounding boxes
        img = np.random.rand(100, 100, 3)
        bboxes = np.array([[0, 0, 50, 50], [20, 20, 60, 60]])
        mmcv.imshow_bboxes(img, bboxes)

        pass
    def show_result(self, data, result, top_k=20, **kwargs):
        """Show RPN proposals on the image.

        Args:
            data (str or np.ndarray): Image filename or loaded image.
            result (Tensor or tuple): The results to draw over `img`
                bbox_result or (bbox_result, segm_result).
            top_k (int): Plot the first k bboxes only
               if set positive. Default: 20

        Returns:
            np.ndarray: The image with bboxes drawn on it.
        """
        if kwargs is not None:
            kwargs.pop('score_thr', None)
            kwargs.pop('text_color', None)
            kwargs['colors'] = kwargs.pop('bbox_color', 'green')
        mmcv.imshow_bboxes(data, result, top_k=top_k, **kwargs)
    def visualize_anchor_boxes(self,
                               imgs,
                               cls_scores,
                               img_metas,
                               slice_num=45,
                               top_k=None,
                               shuffle=False):
        featmap_sizes = [featmap.size()[-3:] for featmap in cls_scores]
        assert len(featmap_sizes) == len(self.anchor_generators)
        anchor_list, valid_flag_list = self.get_anchors(
            featmap_sizes, img_metas)
        img = tensor2img3D(imgs, slice_num=slice_num)
        anchors = []
        unique_set = set()
        for bboxes in anchor_list[0]:
            bboxes = bboxes.cpu().numpy()
            for bbox in bboxes:
                # select each aspect ratio bounding box in the middle of an image
                # if bbox[0] >= 100 and bbox[0] <= 400 and bbox[2] >= 100 and bbox[2] <= 400 and \
                #     bbox[1] >= 150 and bbox[1] <= 450 and bbox[3] >= 150 and bbox[3] <= 450 and \
                #     slice_num >= bbox[4] and slice_num <= bbox[5] and (bbox[5] - bbox[4]) not in unique_set:
                #     unique_set.add(bbox[5] - bbox[4])
                #     anchors.append([bbox[0], bbox[1], bbox[2], bbox[3]])

                # Get all anchors in the middle of the image
                if bbox[0] >= 100 and bbox[0] <= 400 and bbox[2] >= 100 and bbox[2] <= 400 and \
                    bbox[1] >= 150 and bbox[1] <= 450 and bbox[3] >= 150 and bbox[3] <= 450 and \
                    slice_num >= bbox[4] and slice_num <= bbox[5] and (bbox[2] - bbox[0]) not in unique_set:
                    anchors.append([bbox[0], bbox[1], bbox[2], bbox[3]])
        print(unique_set)
        breakpoint()
        if shuffle is True:
            while True:
                random.shuffle(anchors)
                mmcv.imshow_bboxes(img, np.array(anchors), top_k=20)
        elif top_k is None:
            mmcv.imshow_bboxes(img, np.array(anchors))
        else:
            mmcv.imshow_bboxes(img, np.array(anchors), top_k=top_k)
        breakpoint()
Example #13
0
    def show_result(self,
                    result,
                    img=None,
                    skeleton=None,
                    kpt_score_thr=0.3,
                    radius=8,
                    bbox_color='green',
                    thickness=2,
                    pose_kpt_color=None,
                    pose_link_color=None,
                    vis_height=400,
                    num_instances=-1,
                    axis_azimuth=-115,
                    win_name='',
                    show=False,
                    wait_time=0,
                    out_file=None):
        """Visualize 3D pose estimation results.

        Args:
            result (list[dict]): The pose estimation results containing:

                - "keypoints_3d" ([K,4]): 3D keypoints
                - "keypoints" ([K,3] or [T,K,3]): Optional for visualizing
                    2D inputs. If a sequence is given, only the last frame
                    will be used for visualization
                - "bbox" ([4,] or [T,4]): Optional for visualizing 2D inputs
                - "title" (str): title for the subplot
            img (str or Tensor): Optional. The image to visualize 2D inputs on.
            skeleton (list of [idx_i,idx_j]): Skeleton described by a list of
                links, each is a pair of joint indices.
            kpt_score_thr (float, optional): Minimum score of keypoints
                to be shown. Default: 0.3.
            radius (int): Radius of circles.
            bbox_color (str or tuple or :obj:`Color`): Color of bbox lines.
            thickness (int): Thickness of lines.
            pose_kpt_color (np.array[Nx3]`): Color of N keypoints.
                If None, do not draw keypoints.
            pose_link_color (np.array[Mx3]): Color of M limbs.
                If None, do not draw limbs.
            vis_height (int): The image height of the visualization. The width
                will be N*vis_height depending on the number of visualized
                items.
            num_instances (int): Number of instances to be shown in 3D. If
                smaller than 0, all the instances in the pose_result will be
                shown. Otherwise, pad or truncate the pose_result to a length
                of num_instances.
            axis_azimuth (float): axis azimuth angle for 3D visualizations.
            win_name (str): The window name.
            show (bool): Whether to show the image. Default: False.
            wait_time (int): Value of waitKey param.
                Default: 0.
            out_file (str or None): The filename to write the image.
                Default: None.

        Returns:
            Tensor: Visualized img, only if not `show` or `out_file`.
        """
        if num_instances < 0:
            assert len(result) > 0
        result = sorted(result, key=lambda x: x.get('track_id', 0))

        # draw image and 2d poses
        if img is not None:
            img = mmcv.imread(img)

            bbox_result = []
            pose_2d = []
            for res in result:
                if 'bbox' in res:
                    bbox = np.array(res['bbox'])
                    if bbox.ndim != 1:
                        assert bbox.ndim == 2
                        bbox = bbox[-1]  # Get bbox from the last frame
                    bbox_result.append(bbox)
                if 'keypoints' in res:
                    kpts = np.array(res['keypoints'])
                    if kpts.ndim != 2:
                        assert kpts.ndim == 3
                        kpts = kpts[-1]  # Get 2D keypoints from the last frame
                    pose_2d.append(kpts)

            if len(bbox_result) > 0:
                bboxes = np.vstack(bbox_result)
                mmcv.imshow_bboxes(img,
                                   bboxes,
                                   colors=bbox_color,
                                   top_k=-1,
                                   thickness=2,
                                   show=False)
            if len(pose_2d) > 0:
                imshow_keypoints(img,
                                 pose_2d,
                                 skeleton,
                                 kpt_score_thr=kpt_score_thr,
                                 pose_kpt_color=pose_kpt_color,
                                 pose_link_color=pose_link_color,
                                 radius=radius,
                                 thickness=thickness)
            img = mmcv.imrescale(img, scale=vis_height / img.shape[0])

        img_vis = imshow_keypoints_3d(result,
                                      img,
                                      skeleton,
                                      pose_kpt_color,
                                      pose_link_color,
                                      vis_height,
                                      axis_limit=300,
                                      axis_azimuth=axis_azimuth,
                                      axis_elev=15,
                                      kpt_score_thr=kpt_score_thr,
                                      num_instances=num_instances)

        if show:
            mmcv.visualization.imshow(img_vis, win_name, wait_time)

        if out_file is not None:
            mmcv.imwrite(img_vis, out_file)

        return img_vis
Example #14
0
    def show_result(self,
                    img,
                    result,
                    skeleton=None,
                    kpt_score_thr=0.3,
                    bbox_color='green',
                    pose_kpt_color=None,
                    pose_limb_color=None,
                    radius=4,
                    text_color=(255, 0, 0),
                    thickness=1,
                    font_scale=0.5,
                    win_name='',
                    show=False,
                    show_keypoint_weight=False,
                    wait_time=0,
                    out_file=None):
        """Draw `result` over `img`.

        Args:
            img (str or Tensor): The image to be displayed.
            result (list[dict]): The results to draw over `img`
                (bbox_result, pose_result).
            kpt_score_thr (float, optional): Minimum score of keypoints
                to be shown. Default: 0.3.
            bbox_color (str or tuple or :obj:`Color`): Color of bbox lines.
            pose_kpt_color (np.array[Nx3]`): Color of N keypoints.
                If None, do not draw keypoints.
            pose_limb_color (np.array[Mx3]): Color of M limbs.
                If None, do not draw limbs.
            text_color (str or tuple or :obj:`Color`): Color of texts.
            thickness (int): Thickness of lines.
            font_scale (float): Font scales of texts.
            win_name (str): The window name.
            wait_time (int): Value of waitKey param.
                Default: 0.
            out_file (str or None): The filename to write the image.
                Default: None.

        Returns:
            Tensor: Visualized img, only if not `show` or `out_file`.
        """

        img = mmcv.imread(img)
        img = img.copy()
        img_h, img_w, _ = img.shape

        bbox_result = []
        pose_result = []
        for res in result:
            bbox_result.append(res['bbox'])
            pose_result.append(res['keypoints'])

        if len(bbox_result) > 0:
            bboxes = np.vstack(bbox_result)
            # draw bounding boxes
            mmcv.imshow_bboxes(
                img,
                bboxes,
                colors=bbox_color,
                top_k=-1,
                thickness=thickness,
                show=False,
                win_name=win_name,
                wait_time=wait_time,
                out_file=None)

            for _, kpts in enumerate(pose_result):
                # draw each point on image
                if pose_kpt_color is not None:
                    assert len(pose_kpt_color) == len(kpts)
                    for kid, kpt in enumerate(kpts):
                        x_coord, y_coord, kpt_score = int(kpt[0]), int(
                            kpt[1]), kpt[2]
                        if kpt_score > kpt_score_thr:
                            if show_keypoint_weight:
                                img_copy = img.copy()
                                r, g, b = pose_kpt_color[kid]
                                cv2.circle(img_copy,
                                           (int(x_coord), int(y_coord)),
                                           radius, (int(r), int(g), int(b)),
                                           -1)
                                transparency = max(0, min(1, kpt_score))
                                cv2.addWeighted(
                                    img_copy,
                                    transparency,
                                    img,
                                    1 - transparency,
                                    0,
                                    dst=img)
                            else:
                                r, g, b = pose_kpt_color[kid]
                                cv2.circle(img, (int(x_coord), int(y_coord)),
                                           radius, (int(r), int(g), int(b)),
                                           -1)

                # draw limbs
                if skeleton is not None and pose_limb_color is not None:
                    assert len(pose_limb_color) == len(skeleton)
                    for sk_id, sk in enumerate(skeleton):
                        pos1 = (int(kpts[sk[0] - 1, 0]), int(kpts[sk[0] - 1,
                                                                  1]))
                        pos2 = (int(kpts[sk[1] - 1, 0]), int(kpts[sk[1] - 1,
                                                                  1]))
                        if (pos1[0] > 0 and pos1[0] < img_w and pos1[1] > 0
                                and pos1[1] < img_h and pos2[0] > 0
                                and pos2[0] < img_w and pos2[1] > 0
                                and pos2[1] < img_h
                                and kpts[sk[0] - 1, 2] > kpt_score_thr
                                and kpts[sk[1] - 1, 2] > kpt_score_thr):
                            r, g, b = pose_limb_color[sk_id]
                            if show_keypoint_weight:
                                img_copy = img.copy()
                                X = (pos1[0], pos2[0])
                                Y = (pos1[1], pos2[1])
                                mX = np.mean(X)
                                mY = np.mean(Y)
                                length = ((Y[0] - Y[1])**2 +
                                          (X[0] - X[1])**2)**0.5
                                angle = math.degrees(
                                    math.atan2(Y[0] - Y[1], X[0] - X[1]))
                                stickwidth = 2
                                polygon = cv2.ellipse2Poly(
                                    (int(mX), int(mY)),
                                    (int(length / 2), int(stickwidth)),
                                    int(angle), 0, 360, 1)
                                cv2.fillConvexPoly(img_copy, polygon,
                                                   (int(r), int(g), int(b)))
                                transparency = max(
                                    0,
                                    min(
                                        1, 0.5 * (kpts[sk[0] - 1, 2] +
                                                  kpts[sk[1] - 1, 2])))
                                cv2.addWeighted(
                                    img_copy,
                                    transparency,
                                    img,
                                    1 - transparency,
                                    0,
                                    dst=img)
                            else:
                                cv2.line(
                                    img,
                                    pos1,
                                    pos2, (int(r), int(g), int(b)),
                                    thickness=thickness)

        if show:
            imshow(img, win_name, wait_time)

        if out_file is not None:
            imwrite(img, out_file)

        return img
Example #15
0
def imshow_bboxes_ref(img, bboxes0, bboxes1):
    bboxes = np.concatenate([bboxes0, bboxes1], 0)
    n0 = bboxes0.shape[0]
    n1 = bboxes1.shape[0]
    colors = ['red'] * n0 + ['green'] * n1
    mmcv.imshow_bboxes(img, bboxes, colors)
Example #16
0
def imshow_bboxes(img,
                  bboxes,
                  labels=None,
                  colors='green',
                  text_color='white',
                  thickness=1,
                  font_scale=0.5,
                  show=True,
                  win_name='',
                  wait_time=0,
                  out_file=None):
    """Draw bboxes with labels (optional) on an image. This is a wrapper of
    mmcv.imshow_bboxes.

    Args:
        img (str or ndarray): The image to be displayed.
        bboxes (ndarray): ndarray of shape (k, 4), each row is a bbox in
            format [x1, y1, x2, y2].
        labels (str or list[str], optional): labels of each bbox.
        colors (list[str or tuple or :obj:`Color`]): A list of colors.
        text_color (str or tuple or :obj:`Color`): Color of texts.
        thickness (int): Thickness of lines.
        font_scale (float): Font scales of texts.
        show (bool): Whether to show the image.
        win_name (str): The window name.
        wait_time (int): Value of waitKey param.
        out_file (str, optional): The filename to write the image.

    Returns:
        ndarray: The image with bboxes drawn on it.
    """

    # adapt to mmcv.imshow_bboxes input format
    bboxes = np.split(bboxes, bboxes.shape[0],
                      axis=0) if bboxes.shape[0] > 0 else []
    if not isinstance(colors, list):
        colors = [colors for _ in range(len(bboxes))]
    colors = [mmcv.color_val(c) for c in colors]
    assert len(bboxes) == len(colors)

    img = mmcv.imshow_bboxes(img,
                             bboxes,
                             colors,
                             top_k=-1,
                             thickness=thickness,
                             show=False,
                             out_file=None)

    if labels is not None:
        if not isinstance(labels, list):
            labels = [labels for _ in range(len(bboxes))]
        assert len(labels) == len(bboxes)

        for bbox, label, color in zip(bboxes, labels, colors):
            bbox_int = bbox[0, :4].astype(np.int32)
            # roughly estimate the proper font size
            text_size, text_baseline = cv2.getTextSize(label,
                                                       cv2.FONT_HERSHEY_DUPLEX,
                                                       font_scale, thickness)
            text_x1 = bbox_int[0]
            text_y1 = max(0, bbox_int[1] - text_size[1] - text_baseline)
            text_x2 = bbox_int[0] + text_size[0]
            text_y2 = text_y1 + text_size[1] + text_baseline
            cv2.rectangle(img, (text_x1, text_y1), (text_x2, text_y2), color,
                          cv2.FILLED)
            cv2.putText(img, label, (text_x1, text_y2 - text_baseline),
                        cv2.FONT_HERSHEY_DUPLEX, font_scale,
                        mmcv.color_val(text_color), thickness)

    if show:
        mmcv.imshow(img, win_name, wait_time)
    if out_file is not None:
        mmcv.imwrite(img, out_file)
    return img
Example #17
0
    def show_result(self,
                    img,
                    result,
                    skeleton=None,
                    kpt_score_thr=0.3,
                    bbox_color='green',
                    pose_kpt_color=None,
                    pose_limb_color=None,
                    text_color=(255, 0, 0),
                    radius=4,
                    thickness=1,
                    font_scale=0.5,
                    win_name='',
                    show=False,
                    show_keypoint_weight=False,
                    wait_time=0,
                    out_file=None):
        """Draw `result` over `img`.

        Args:
            img (str or Tensor): The image to be displayed.
            result (list[dict]): The results to draw over `img`
                (bbox_result, pose_result).
            skeleton (list[list]): The connection of keypoints.
            kpt_score_thr (float, optional): Minimum score of keypoints
                to be shown. Default: 0.3.
            bbox_color (str or tuple or :obj:`Color`): Color of bbox lines.
            pose_kpt_color (np.array[Nx3]`): Color of N keypoints.
                If None, do not draw keypoints.
            pose_limb_color (np.array[Mx3]): Color of M limbs.
                If None, do not draw limbs.
            text_color (str or tuple or :obj:`Color`): Color of texts.
            radius (int): Radius of circles.
            thickness (int): Thickness of lines.
            font_scale (float): Font scales of texts.
            win_name (str): The window name.
            show (bool): Whether to show the image. Default: False.
            show_keypoint_weight (bool): Whether to change the transparency
                using the predicted confidence scores of keypoints.
            wait_time (int): Value of waitKey param.
                Default: 0.
            out_file (str or None): The filename to write the image.
                Default: None.

        Returns:
            Tensor: Visualized img, only if not `show` or `out_file`.
        """

        img = mmcv.imread(img)
        img = img.copy()
        img_h, img_w, _ = img.shape

        bbox_result = []
        pose_result = []
        for res in result:
            bbox_result.append(res['bbox'])
            pose_result.append(res['keypoints'])

        if len(bbox_result) > 0:
            bboxes = np.vstack(bbox_result)
            # draw bounding boxes
            mmcv.imshow_bboxes(img,
                               bboxes,
                               colors=bbox_color,
                               top_k=-1,
                               thickness=thickness,
                               show=False)

            imshow_keypoints(img, pose_result, skeleton, kpt_score_thr,
                             pose_kpt_color, pose_limb_color, radius,
                             thickness)

        if show:
            imshow(img, win_name, wait_time)

        if out_file is not None:
            imwrite(img, out_file)

        return img
Example #18
0
def add_bboxs_to_img(img_path: str, xml_path: str, out_path: str):
    bboxs = get_bboxs_from_xml(xml_path)
    mmcv.imshow_bboxes(img_path, bboxs, show=False, out_file=out_path)
Example #19
0
def crop4patches(img_prefix, img_writen, istrain=True):

    if not os.path.exists(img_writen + 'annotations/'):
        os.makedirs(img_writen + 'annotations/')
    if not os.path.exists(img_writen + 'images/'):
        os.makedirs(img_writen + 'images/')
    img_infos = []
    img_file = img_prefix + 'images/'
    all_imgs_files = os.listdir(img_file)
    for img_file in all_imgs_files:
        img_id = img_file.split('.')[0]
        anno_name = 'annotations/{}.txt'.format(img_id)
        img_name = 'images/{}.jpg'.format(img_id)
        #p rint(filename)
        print("dealing with {}".format(img_name))
        img_path = osp.join(img_prefix, img_name)
        anno_path = osp.join(img_prefix, anno_name)

        img = cv2.imread(img_path)
        h, w, c = img.shape
        print("h {}".format(h))
        print("w {}".format(w))

        patch_width = int(w) // 2
        patch_height = int(h) // 2
        bboxes = []
        bboxes.append(np.array([0, 0, patch_width, patch_height]))
        bboxes.append(np.array([0, patch_height, patch_width, h]))
        bboxes.append(np.array([patch_width, 0, w, patch_height]))
        bboxes.append(np.array([patch_width, patch_height, w, h]))
        padw = (w - patch_width) // 2
        padh = (h - patch_height) // 2
        if istrain:
            bboxes.append(np.array([padw, padh, w - padw, h - padh]))
        bboxes = np.array(bboxes)
        img_patches = mmcv.imcrop(img, bboxes, scale=1.0)
        for jj in range(len(img_patches)):
            if istrain:
                assert (len(img_patches)) == 5
            else:
                assert (len(img_patches)) == 4
            cv2.imwrite(img_writen + 'images/{}_{}.jpg'.format(img_id, jj + 1),
                        img_patches[jj])

        with open(anno_path, 'r') as ann:
            note = ann.readlines()
            # 计算中心 patch的标注
            if istrain:
                for item in note:
                    values_str = item.split(',')  #list()
                    bbox_left,bbox_top,bbox_width,bbox_height,score,object_category,\
                    truncation,occulusion = int(values_str[0]),int(values_str[1]),\
                    int(values_str[2]),int(values_str[3]),int(values_str[4]),int(values_str[5]),\
                    int(values_str[6]),int(values_str[7])
                    # in central patch
                    if bbox_left > padw and bbox_top > padh and bbox_left < w - padw and bbox_top < h - padh:

                        if bbox_left + bbox_width > w - padw or bbox_top + bbox_height > h - padh:
                            if bbox_iou((bbox_left,bbox_top,bbox_left+bbox_width,bbox_top+bbox_height),\
                                        (bbox_left,bbox_top,min(w-padw,bbox_left+bbox_width),min(h-padh,bbox_top+bbox_height))) > 0.5:
                                message = str(bbox_left-padw)+','+str(bbox_top-padh)+','+str(min(w-padw,bbox_left+bbox_width)-bbox_left)+','+str(min(h-padh,bbox_top+bbox_height)-bbox_top)\
                                +','+str(score)+','+str(object_category)+','+str(1)+','+str(occulusion)
                                path = img_writen + 'annotations/{}_{}.txt'.format(
                                    img_id, 5)
                                save_newanno(message, path)
                                continue
                            else:
                                continue
                        else:
                            message = str(bbox_left-padw)+','+str(bbox_top-padh)+','+str(min(w-padw,bbox_left+bbox_width)-bbox_left)+','+str(min(h-padh,bbox_top+bbox_height)-bbox_top)\
                            +','+str(score)+','+str(object_category)+','+str(truncation)+','+str(occulusion)
                            path = img_writen + 'annotations/{}_{}.txt'.format(
                                img_id, 5)
                            #print("5loc {}".format(message))
                            save_newanno(message, path)
                            continue

            for item in note:
                values_str = item.split(',')  #list()
                bbox_left,bbox_top,bbox_width,bbox_height,score,object_category,\
                truncation,occulusion = int(values_str[0]),int(values_str[1]),\
                int(values_str[2]),int(values_str[3]),int(values_str[4]),int(values_str[5]),\
                int(values_str[6]),int(values_str[7])

                if bbox_left < patch_width and bbox_top < patch_height:  # zuoshang
                    if bbox_left + bbox_width > patch_width or bbox_top + bbox_height > patch_height:  # outline
                        if bbox_iou((bbox_left,bbox_top,bbox_left+bbox_width,bbox_top+bbox_height),\
                                    (bbox_left,bbox_top,min(patch_width,bbox_left+bbox_width),min(patch_height,bbox_top+bbox_height))) > 0.5:
                            #save
                            message = str(bbox_left-0)+','+str(bbox_top-0)+','+str(min(patch_width,bbox_left+bbox_width)-bbox_left)+','+str(min(patch_height,bbox_top+bbox_height)-bbox_top)\
                            +','+str(score)+','+str(object_category)+','+str(1)+','+str(occulusion)
                            path = img_writen + 'annotations/{}_{}.txt'.format(
                                img_id, 1)
                            save_newanno(message, path)
                            continue
                        else:  # dont save
                            continue
                    else:  # 完整直接save
                        message = str(bbox_left-0)+','+str(bbox_top-0)+','+str(min(patch_width,bbox_left+bbox_width)-bbox_left)+','+str(min(patch_height,bbox_top+bbox_height)-bbox_top)\
                        +','+str(score)+','+str(object_category)+','+str(truncation)+','+str(occulusion)
                        path = img_writen + 'annotations/{}_{}.txt'.format(
                            img_id, 1)
                        save_newanno(message, path)
                        #print("1loc {}".format(message))
                        continue
                #zuoxia
                if bbox_left < patch_width and bbox_top >= patch_height:
                    if bbox_top + bbox_height > h:  # 原本标注错误
                        raise IOError
                    if bbox_left + bbox_width > patch_width:  # outline
                        if bbox_iou((bbox_left,bbox_top,bbox_left+bbox_width,bbox_top+bbox_height),\
                (bbox_left,bbox_top,min(patch_width,bbox_left+bbox_width),min(patch_height,bbox_top+bbox_height))) > 0.5:
                            #save
                            message = str(bbox_left-0)+','+str(bbox_top-patch_height)+','+str(min(patch_width,bbox_left+bbox_width)-bbox_left)+','+str(min(h,bbox_top+bbox_height)-bbox_top)\
                            +','+str(score)+','+str(object_category)+','+str(1)+','+str(occulusion)
                            path = img_writen + 'annotations/{}_{}.txt'.format(
                                img_id, 2)
                            save_newanno(message, path)
                            continue
                        else:  # dont save
                            continue
                    else:
                        #save
                        message = str(bbox_left-0)+','+str(bbox_top-patch_height)+','+str(min(patch_width,bbox_left+bbox_width)-bbox_left)+','+str(min(h,bbox_top+bbox_height)-bbox_top)\
                        +','+str(score)+','+str(object_category)+','+str(truncation)+','+str(occulusion)
                        path = img_writen + 'annotations/{}_{}.txt'.format(
                            img_id, 2)
                        save_newanno(message, path)
                        #print("2loc {}".format(message))
                        continue
                #youshang
                if bbox_left >= patch_width and bbox_top < patch_height:
                    if bbox_left + bbox_width > w:
                        raise IOError
                    if bbox_top + bbox_height > patch_height:  # outline
                        if bbox_iou((bbox_left,bbox_top,bbox_left+bbox_width,bbox_top+bbox_height),\
                                    (bbox_left,bbox_top,min(patch_width,bbox_left+bbox_width),min(patch_height,bbox_top+bbox_height))) > 0.5:
                            #save
                            message = str(bbox_left-patch_width)+','+str(bbox_top-0)+','+str(min(w,bbox_left+bbox_width)-bbox_left)+','+str(min(patch_height,bbox_top+bbox_height)-bbox_top)\
                            +','+str(score)+','+str(object_category)+','+str(1)+','+str(occulusion)# must trucncation
                            path = img_writen + 'annotations/{}_{}.txt'.format(
                                img_id, 3)
                            save_newanno(message, path)
                            continue
                        else:  # dont save
                            continue
                    else:
                        #save
                        message = str(bbox_left-patch_width)+','+str(bbox_top-0)+','+str(min(w,bbox_left+bbox_width)-bbox_left)+','+str(min(patch_height,bbox_top+bbox_height)-bbox_top)\
                        +','+str(score)+','+str(object_category)+','+str(truncation)+','+str(occulusion)
                        path = img_writen + 'annotations/{}_{}.txt'.format(
                            img_id, 3)
                        save_newanno(message, path)
                        #print("3loc {}".format(message))
                        continue
                # youxia
                if bbox_left >= patch_width and bbox_top >= patch_height:
                    if bbox_left + bbox_width > w or bbox_height + bbox_top > h:
                        raise IOError
                    # 第四个区域不会有 outline
                    message = str(bbox_left-patch_width)+','+str(bbox_top-patch_height)+','+str(bbox_width)+','+str(bbox_height)\
                    +','+str(score)+','+str(object_category)+','+str(truncation)+','+str(occulusion)
                    path = img_writen + 'annotations/{}_{}.txt'.format(
                        img_id, 4)
                    save_newanno(message, path)
                    #print("4loc {}".format(message))
                    continue
        #check if the image has no annotaion , delet it
        for jj in range(len(img_patches)):
            if istrain:
                assert (len(img_patches)) == 5
            else:
                assert (len(img_patches)) == 4
            if not os.path.exists(
                    img_writen +
                    'annotations/{}_{}.txt'.format(img_id, jj + 1)):
                os.remove(img_writen +
                          'images/{}_{}.jpg'.format(img_id, jj + 1))
                #path = img_writen+'annotations/{}_{}.txt'.format(img_id,jj+1)
                #with open(path,'w') as ann: # 追加模式
                #   pass
                #print("empty {}".format('annotations/{}_{}.jpg'.format(img_id,jj+1)))

    new_list = os.listdir(img_writen + 'images/')
    new_list_show = []
    new_list_show.extend(new_list[:100])
    new_list_show.extend(new_list[500:600])
    for ii, item in enumerate(new_list_show):
        showimg = cv2.imread(img_writen + 'images/' + item)
        id = item.split('.')[0]
        annotation = img_writen + 'annotations/' + id + '.txt'
        #if not os.path.exists(annotation):
        #    continue
        with open(annotation, 'r') as ann:
            note = ann.readlines()
            bboxes = []
            for jj in note:
                values_str = jj.split(',')  #list()
                bbox_left,bbox_top,bbox_width,bbox_height,score,object_category,\
                truncation,occulusion = int(values_str[0]),int(values_str[1]),\
                int(values_str[2]),int(values_str[3]),int(values_str[4]),int(values_str[5]),\
                int(values_str[6]),int(values_str[7])
                bboxes.append(
                    np.array([
                        bbox_left, bbox_top, bbox_left + bbox_width,
                        bbox_top + bbox_height
                    ]))
            bboxes = np.array(bboxes)
            print('/home/share2/VisDrone2019/vispatch/' + item)
            if istrain:
                mmcv.imshow_bboxes(
                    showimg,
                    bboxes,
                    show=False,
                    out_file='/home/share2/VisDrone2019/TASK1/trainpatch/' +
                    item)
            else:
                mmcv.imshow_bboxes(
                    showimg,
                    bboxes,
                    show=False,
                    out_file='/home/share2/VisDrone2019/TASK1/valpatch/' +
                    item)
Example #20
0
    def show_result(self,
                    img,
                    result,
                    skeleton=None,
                    kpt_score_thr=0.3,
                    bbox_color='green',
                    pose_kpt_color=(255, 0, 0),
                    pose_limb_color=None,
                    radius=4,
                    text_color=(255, 0, 0),
                    thickness=1,
                    font_scale=0.5,
                    win_name='',
                    show=False,
                    wait_time=0,
                    out_file=None):
        """Draw `result` over `img`.

        Args:
            img (str or Tensor): The image to be displayed.
            result (list[dict]): The results to draw over `img`
                (bbox_result, pose_result).
            kpt_score_thr (float, optional): Minimum score of keypoints
                to be shown. Default: 0.3.
            bbox_color (str or tuple or :obj:`Color`): Color of bbox lines.
            pose_kpt_color (str or tuple or :obj:`Color`): Color of keypoints.
            pose_limb_color (str or tuple or :obj:`Color`): Color of limbs.
            text_color (str or tuple or :obj:`Color`): Color of texts.
            thickness (int): Thickness of lines.
            font_scale (float): Font scales of texts.
            win_name (str): The window name.
            wait_time (int): Value of waitKey param.
                Default: 0.
            show (bool): Whether to show the image.
                Default: False.
            out_file (str or None): The filename to write the image.
                Default: None.

        Returns:
            Tensor: Img: Only if not `show` or `out_file`.
        """
        img = mmcv.imread(img)
        img = img.copy()
        img_h, img_w, _ = img.shape

        bbox_result = []
        pose_result = []
        for res in result:
            bbox_result.append(res['bbox'])
            pose_result.append(res['keypoints'])

        bboxes = np.vstack(bbox_result)

        # if out_file specified, do not show image in window
        if out_file is not None:
            show = False

        # draw bounding boxes
        mmcv.imshow_bboxes(img,
                           bboxes,
                           colors=bbox_color,
                           top_k=-1,
                           thickness=thickness,
                           show=False,
                           win_name=win_name,
                           wait_time=wait_time,
                           out_file=None)

        for person_id, kpts in enumerate(pose_result):
            # draw each point on image
            for kid, kpt in enumerate(kpts):
                x_coord, y_coord, kpt_score = int(kpt[0]), int(kpt[1]), kpt[2]
                if kpt_score > kpt_score_thr:
                    cv2.circle(img, (x_coord, y_coord), radius, pose_kpt_color,
                               thickness)
                    cv2.putText(img, f'{kid}', (x_coord, y_coord - 2),
                                cv2.FONT_HERSHEY_COMPLEX, font_scale,
                                text_color)

            # draw limbs
            if skeleton is not None:
                for sk in skeleton:
                    pos1 = (int(kpts[sk[0] - 1, 0]), int(kpts[sk[0] - 1, 1]))
                    pos2 = (int(kpts[sk[1] - 1, 0]), int(kpts[sk[1] - 1, 1]))
                    if pos1[0] > 0 and pos1[0] < img_w \
                            and pos1[1] > 0 and pos1[1] < img_h \
                            and pos2[0] > 0 and pos2[0] < img_w \
                            and pos2[1] > 0 and pos2[1] < img_h \
                            and kpts[sk[0] - 1, 2] > kpt_score_thr \
                            and kpts[sk[1] - 1, 2] > kpt_score_thr:
                        cv2.line(img, pos1, pos2, pose_kpt_color, 2, 8)

        if show:
            imshow(img, win_name, wait_time)
        if out_file is not None:
            imwrite(img, out_file)
        if not (show or out_file):
            warnings.warn('show==False and out_file is not specified, only '
                          'result image will be returned')
            return img
Example #21
0
# -*- coding: utf-8 -* -
'''
MMCV显示图片相关
参考:https://mmcv.readthedocs.io/en/latest/visualization.html
'''
import mmcv
import numpy as np

# 显示图片文件
mmcv.imshow("cluo.jpg")

# 显示numpy数组格式的图片
img = np.random.rand(100,100,3)*255
mmcv.imshow(img)

# 显示图片并伴有bounding box
img = np.random.rand(100,100,3)*255
bboxes = np.array([[0,0,50,50],[20,20,60,60]])
mmcv.imshow_bboxes(img,bboxes,colors=["red"],show=False,out_file="cluo_bbox.jpg")

# 显示图片并伴有bounding box以及框的标题
labels = np.array([1,3])
classes = ["类别0","类别1","类别2","类别3"]
mmcv.imshow_det_bboxes(img,bboxes,labels,classes,bbox_color="green",text_color="blue",out_file="cluo_bbox_label.jpg")

Example #22
0
    def show_result(self,
                    result,
                    img,
                    show=False,
                    out_file=None,
                    win_name='',
                    wait_time=0,
                    bbox_color='green',
                    mesh_color=(76, 76, 204),
                    **kwargs):
        """Visualize 3D mesh estimation results.

        Args:
            result (list[dict]): The mesh estimation results containing:

               - "bbox" (ndarray[4]): instance bounding bbox
               - "center" (ndarray[2]): bbox center
               - "scale" (ndarray[2]): bbox scale
               - "keypoints_3d" (ndarray[K,3]): predicted 3D keypoints
               - "camera" (ndarray[3]): camera parameters
               - "vertices" (ndarray[V, 3]): predicted 3D vertices
               - "faces" (ndarray[F, 3]): mesh faces
            img (str or Tensor): Optional. The image to visualize 2D inputs on.
            win_name (str): The window name.
            show (bool): Whether to show the image. Default: False.
            wait_time (int): Value of waitKey param. Default: 0.
            out_file (str or None): The filename to write the image.
                Default: None.
            bbox_color (str or tuple or :obj:`Color`): Color of bbox lines.
            mesh_color (str or tuple or :obj:`Color`): Color of mesh surface.

        Returns:
            ndarray: Visualized img, only if not `show` or `out_file`.
        """

        if img is not None:
            img = mmcv.imread(img)

        focal_length = self.loss_mesh.focal_length
        H, W, C = img.shape
        img_center = np.array([[0.5 * W], [0.5 * H]])

        # show bounding boxes
        bboxes = [res['bbox'] for res in result]
        bboxes = np.vstack(bboxes)
        mmcv.imshow_bboxes(img,
                           bboxes,
                           colors=bbox_color,
                           top_k=-1,
                           thickness=2,
                           show=False)

        vertex_list = []
        face_list = []
        for res in result:
            vertices = res['vertices']
            faces = res['faces']
            camera = res['camera']
            camera_center = res['center']
            scale = res['scale']

            # predicted vertices are in root-relative space,
            # we need to translate them to camera space.
            translation = np.array([
                camera[1], camera[2],
                2 * focal_length / (scale[0] * 200.0 * camera[0] + 1e-9)
            ])
            mean_depth = vertices[:, -1].mean() + translation[-1]
            translation[:2] += (camera_center -
                                img_center[:, 0]) / focal_length * mean_depth
            vertices += translation[None, :]

            vertex_list.append(vertices)
            face_list.append(faces)

        # render from front view
        img_vis = imshow_mesh_3d(img,
                                 vertex_list,
                                 face_list,
                                 img_center, [focal_length, focal_length],
                                 colors=mesh_color)

        # render from side view
        # rotate mesh vertices
        R = cv2.Rodrigues(np.array([0, np.radians(90.), 0]))[0]
        rot_vertex_list = [np.dot(vert, R) for vert in vertex_list]

        # get the 3D bbox containing all meshes
        rot_vertices = np.concatenate(rot_vertex_list, axis=0)
        min_corner = rot_vertices.min(0)
        max_corner = rot_vertices.max(0)

        center_3d = 0.5 * (min_corner + max_corner)
        ratio = 0.8
        bbox3d_size = max_corner - min_corner

        # set appropriate translation to make all meshes appear in the image
        z_x = bbox3d_size[0] * focal_length / (ratio * W) - min_corner[2]
        z_y = bbox3d_size[1] * focal_length / (ratio * H) - min_corner[2]
        z = max(z_x, z_y)
        translation = -center_3d
        translation[2] = z
        translation = translation[None, :]
        rot_vertex_list = [
            rot_vert + translation for rot_vert in rot_vertex_list
        ]

        # render from side view
        img_side = imshow_mesh_3d(
            np.ones_like(img) * 255, rot_vertex_list, face_list, img_center,
            [focal_length, focal_length])

        # merger images from front view and side view
        img_vis = np.concatenate([img_vis, img_side], axis=1)

        if show:
            mmcv.visualization.imshow(img_vis, win_name, wait_time)

        if out_file is not None:
            mmcv.imwrite(img_vis, out_file)

        return img_vis