Ejemplo n.º 1
0
def flowwrite(flow, filename, quantize=False, concat_axis=0, *args, **kwargs):
    """Write optical flow to file.

    If the flow is not quantized, it will be saved as a .flo file losslessly,
    otherwise a jpeg image which is lossy but of much smaller size. (dx and dy
    will be concatenated horizontally into a single image if quantize is True.)

    Args:
        flow (ndarray): (h, w, 2) array of optical flow.
        filename (str): Output filepath.
        quantize (bool): Whether to quantize the flow and save it to 2 jpeg
            images. If set to True, remaining args will be passed to
            :func:`quantize_flow`.
        concat_axis (int): The axis that dx and dy are concatenated,
            can be either 0 or 1. Ignored if quantize is False.
    """
    if not quantize:
        with open(filename, 'wb') as f:
            f.write('PIEH'.encode('utf-8'))
            np.array([flow.shape[1], flow.shape[0]], dtype=np.int32).tofile(f)
            flow = flow.astype(np.float32)
            flow.tofile(f)
            f.flush()
    else:
        assert concat_axis in [0, 1]
        dx, dy = quantize_flow(flow, *args, **kwargs)
        dxdy = np.concatenate((dx, dy), axis=concat_axis)
        imwrite(dxdy, filename)
Ejemplo n.º 2
0
    def imshow_det_bboxes(self,
                          img,
                          bboxes,
                          texts,
                          out_file):
        # draw text
        def change_cv2_draw(image, strs, local, sizes, color):
            cv2img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            pilimg = Image.fromarray(cv2img)
            draw = ImageDraw.Draw(pilimg)
            font = ImageFont.truetype('resource/simsun.ttc', sizes, encoding="utf-8")
            draw.text(local, strs, color, font=font)
            image = cv2.cvtColor(np.array(pilimg), cv2.COLOR_RGB2BGR)
            return image

        img = imread(img)

        bbox_color = (255, 0, 0)
        text_color = (0, 0, 255)
        for i in range(len(texts)):
            bbox = bboxes[i].astype(np.int32)
            text = texts[i]
            cv2.drawContours(img, [bbox], -1, bbox_color, 2)
            tl = np.min(bbox, 0)
            img = change_cv2_draw(img, text, (tl[0], tl[1]), 20, text_color)
        imwrite(img, out_file)
Ejemplo n.º 3
0
def imshow_bboxes(img,
                  bboxes,
                  bbox_color=(255, 255, 255),
                  thickness=1,
                  show=True,
                  win_name='',
                  wait_time=0,
                  out_file=None):
    """Draw bboxes on an image.

    Args:
        img (str or ndarray): The image to be displayed.
        bboxes (ndarray): Bounding boxes (with scores), shaped (n, 4).
        bbox_color (RGB value): Color of bbox lines.
        thickness (int): Thickness of lines.
        show (bool): Whether to show the image.
        win_name (str): The window name.
        wait_time (int): Value of waitKey param.
        out_file (str or None): The filename to write the image.
    """
    assert bboxes.ndim == 2
    assert bboxes.shape[1] == 4 or bboxes.shape[1] == 5

    for bbox in bboxes:
        bbox_int = bbox.astype(np.int32)
        left_top = (bbox_int[0], bbox_int[1])
        right_bottom = (bbox_int[2], bbox_int[3])

        img = cv2.rectangle(img, left_top, right_bottom, bbox_color, thickness)

    if show:
        cv2.imshow(win_name, img)
        cv2.waitKey(wait_time)
    if out_file is not None:
        imwrite(img, out_file)
Ejemplo n.º 4
0
def show_seg_result(
        img,
        result,  # list masks[cls]->list
        class_names,
        score_thr=0.3,
        wait_time=0,
        show=True,
        out_file=None):
    """Visualize the detection results on the image.

    Args:
        img (str or np.ndarray): Image filename or loaded image.
        result (tuple[list] or list): The detection result, can be either
            (bbox, segm) or just bbox.
        class_names (list[str] or tuple[str]): A list of class names.
        score_thr (float): The threshold to visualize the bboxes and masks.
        wait_time (int): Value of waitKey param.
        show (bool, optional): Whether to show the image with opencv or not.
        out_file (str, optional): If specified, the visualization result will
            be written to the out file instead of shown in a window.

    Returns:
        np.ndarray or None: If neither `show` nor `out_file` is specified, the
            visualized image is returned, otherwise None is returned.
    """
    assert isinstance(class_names, (tuple, list))
    img = mmcv.imread(img)
    img = img.copy()
    segm_result = result
    labels = [
        np.full(len(s), i, dtype=np.int32) for i, s in enumerate(segm_result)
    ]
    labels = np.concatenate(labels)  # 100
    if segm_result is not None:
        segms = mmcv.concat_list(segm_result)
        segms = np.vstack(segms)
        inds = np.where(segms[:, -1] > score_thr)[0]
        np.random.seed(42)
        color_masks = [
            np.random.randint(0, 256, (1, 3), dtype=np.uint8)
            for _ in range(max(labels) + 1)
        ]

        for i in inds:
            i = int(i)
            color_mask = color_masks[labels[i]]
            mask = mask_util.decode(segms[i][0]).astype(np.bool)
            img[mask] = img[mask] * 0.5 + color_mask * 0.5

            cur_label = labels[i]
            label_text = class_names[cur_label]

            center_y, center_x = ndimage.measurements.center_of_mass(mask)
            vis_pos = (max(int(center_x) - 10, 0), int(center_y))
            cv2.putText(img, label_text, vis_pos, cv2.FONT_HERSHEY_COMPLEX,
                        0.3, (255, 255, 255))  # green
        if out_file is not None:
            imwrite(img, out_file)
Ejemplo n.º 5
0
def imshow_tracklets(img,
                     bboxes,
                     labels=None,
                     ids=None,
                     thickness=2,
                     font_scale=0.4,
                     show=False,
                     win_name='',
                     color=None,
                     out_file=None):
    assert bboxes.ndim == 2
    assert labels.ndim == 1
    assert bboxes.shape[0] == labels.shape[0]
    assert bboxes.shape[1] == 4 or bboxes.shape[1] == 5
    if isinstance(img, str):
        img = imread(img)
    i = 0
    for bbox, label in zip(bboxes, labels):
        x1, y1, x2, y2, _ = bbox.astype(np.int32)
        if ids is not None:
            if color is None:
                bbox_color = random_color(ids[i])
                bbox_color = [int(255 * _c) for _c in bbox_color][::-1]
            else:
                bbox_color = mmcv.color_val(color)
            img[y1:y1 + 12, x1:x1 + 20, :] = bbox_color
            cv2.putText(img,
                        str(ids[i]), (x1, y1 + 10),
                        cv2.FONT_HERSHEY_COMPLEX,
                        font_scale,
                        color=color_val('black'))
        else:
            if color is None:
                bbox_color = color_val('green')
            else:
                bbox_color = mmcv.color_val(color)

        cv2.rectangle(img, (x1, y1), (x2, y2), bbox_color, thickness=thickness)

        if bbox[-1] < 0:
            bbox[-1] = np.nan
        label_text = '{:.02f}'.format(bbox[-1])
        img[y1 - 12:y1, x1:x1 + 30, :] = bbox_color
        cv2.putText(img,
                    label_text, (x1, y1 - 2),
                    cv2.FONT_HERSHEY_COMPLEX,
                    font_scale,
                    color=color_val('black'))

        i += 1

    if show:
        imshow(img, win_name)
    if out_file is not None:
        imwrite(img, out_file)

    return img
Ejemplo n.º 6
0
def imshow_bboxes(img,
                  bboxes,
                  colors='green',
                  top_k=-1,
                  thickness=1,
                  show=True,
                  win_name='',
                  wait_time=0,
                  out_file=None):
    """Draw bboxes on an image.

    Args:
        img (str or ndarray): The image to be displayed.
        bboxes (list or ndarray): A list of ndarray of shape (k, 4).
        colors (list[str or tuple or Color]): A list of colors.
        top_k (int): Plot the first k bboxes only if set positive.
        thickness (int): Thickness of lines.
        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.
    """
    img = imread(img)
    img = np.ascontiguousarray(img)

    if isinstance(bboxes, np.ndarray):
        bboxes = [bboxes]
    if not isinstance(colors, list):
        colors = [colors for _ in range(len(bboxes))]
    colors = [color_val(c) for c in colors]
    assert len(bboxes) == len(colors)

    for i, _bboxes in enumerate(bboxes):
        _bboxes = _bboxes.astype(np.int32)
        if top_k <= 0:
            _top_k = _bboxes.shape[0]
        else:
            _top_k = min(top_k, _bboxes.shape[0])
        for j in range(_top_k):
            left_top = (_bboxes[j, 0], _bboxes[j, 1])
            right_bottom = (_bboxes[j, 2], _bboxes[j, 3])
            cv2.rectangle(img,
                          left_top,
                          right_bottom,
                          colors[i],
                          thickness=thickness)

    if show:
        imshow(img, win_name, wait_time)
    if out_file is not None:
        imwrite(img, out_file)
    return img
Ejemplo n.º 7
0
def imshow_bboxes_custom(img,
                         bboxes,
                         colors='green',
                         top_k=-1,
                         thickness=1,
                         show=True,
                         win_name='',
                         wait_time=0,
                         out_file=None):
    """Draw bboxes on an image.
    Args:
        img (str or ndarray): The image to be displayed.
        bboxes (list or ndarray): A list of ndarray of shape (k, 4).
        colors (list[str or tuple or Color]): A list of colors.
        top_k (int): Plot the first k bboxes only if set positive.
        thickness (int): Thickness of lines.
        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.
    """
    img = imread(img)

    if isinstance(bboxes, np.ndarray):
        bboxes = [bboxes]
    if not isinstance(colors, list):
        colors = [colors for _ in range(len(bboxes))]
    colors = [color_val(c) for c in colors]
    assert len(bboxes) == len(colors)

    for i, _bboxes in enumerate(bboxes):
        label_text = _bboxes[:, 4]
        color_text = color_val('green')
        _bboxes = _bboxes.astype(np.int32)
        if top_k <= 0:
            _top_k = _bboxes.shape[0]
        else:
            _top_k = min(top_k, _bboxes.shape[0])
        for j in range(_top_k):
            left_top = (_bboxes[j, 0], _bboxes[j, 1])
            right_bottom = (_bboxes[j, 2], _bboxes[j, 3])
            cv2.rectangle(img,
                          left_top,
                          right_bottom,
                          colors[i],
                          thickness=thickness)
            cv2.putText(img, '{:.02f}'.format(label_text[j]),
                        (_bboxes[j, 0], _bboxes[j, 3] - 2),
                        cv2.FONT_HERSHEY_COMPLEX, 1, color_text)

    if show:
        imshow(img, win_name, wait_time)
    if out_file is not None:
        imwrite(img, out_file)
Ejemplo n.º 8
0
def single_gpu_test(model, data_loader, show=False, eval_num=None):
    model.eval()
    results = []
    dataset = data_loader.dataset
    prog_bar = mmcv.ProgressBar(len(dataset))
    for i, data in enumerate(data_loader):
        if eval_num is not None and i >= eval_num:
            break
        if show:
            # model.module.show_result(data, result)
            # model.module.show_result(data, result, dataset.img_norm_cfg)
            ins_all, cls_all, img_name = ins_gt_color(data)

            level_num = len(ins_all)
            out_folder = os.path.join('vis_tmp_gt/', img_name)
            out_folder_cls = os.path.join('vis_tmp_gt/', img_name + '_cls')

            if not os.path.exists(out_folder):
                os.makedirs(out_folder)
            if not os.path.exists(out_folder_cls):
                os.makedirs(out_folder_cls)

            for j in range(level_num):
                out_name = os.path.join(out_folder, "%02d" % (j) + '.jpg')
                out_name_cls = os.path.join(out_folder_cls,
                                            "%02d" % (j) + '.jpg')
                imwrite(ins_all[j], out_name)
                if len(cls_all) > 0:
                    imwrite(cls_all[j], out_name_cls)

        del data['gt_labels']
        del data['gt_masks']
        del data['gt_bboxes']
        del data['category_targets']
        del data['point_ins']
        with torch.no_grad():
            result = model(return_loss=False, rescale=not show, **data)
        results.append(result)

        batch_size = data['img'][0].size(0)
        for _ in range(batch_size):
            prog_bar.update()

    return results
Ejemplo n.º 9
0
Archivo: base.py Proyecto: daniel616/DL
def show_det_bboxes(img,
                    bboxes,
                    labels,
                    out_file,
                    gt_bboxes=None,
                    class_names=None,
                    score_thr=0.05,
                    bbox_color='yellow',
                    gt_color='green',
                    top_k=5):

    assert bboxes.ndim == 2
    assert labels.ndim == 1
    assert bboxes.shape[0] == labels.shape[0]
    assert bboxes.shape[1] == 4 or bboxes.shape[1] == 5
    img = imread(img)
    if score_thr > 0:
        assert bboxes.shape[1] == 5
        scores = bboxes[:, -1]
        inds = scores > score_thr
        bboxes = bboxes[inds, :]
        labels = labels[inds]

    topinds = np.argsort(bboxes[:, 4])
    topinds = topinds[:top_k] if len(topinds) >= top_k else topinds
    bboxes = bboxes[topinds, :]
    labels = labels[topinds]

    bbox_color = color_val(bbox_color)
    gt_color = color_val(gt_color)

    for bbox, label in zip(bboxes, labels):
        addBox(img, bbox, bbox_color, label, class_names=class_names)

    if gt_bboxes is not None:
        assert len(gt_bboxes) == 1
        gt_bboxes = gt_bboxes[0]
        for gt_box in gt_bboxes:
            gt_box = gt_box.numpy()
            addBox(img, gt_box, gt_color, 0, class_names=class_names)

    imwrite(img, out_file)
Ejemplo n.º 10
0
    def imshow_det_bboxes(self, img, bboxes, labels, show=True, out_file=None):

        assert bboxes.ndim == 2
        assert labels.ndim == 1
        assert bboxes.shape[0] == labels.shape[0]
        assert bboxes.shape[1] == 4 or bboxes.shape[1] == 5
        img = imread(img)

        if self.score_thr > 0:
            assert bboxes.shape[1] == 5
            scores = bboxes[:, -1]
            inds = scores > self.score_thr
            bboxes = bboxes[inds, :]
            labels = labels[inds]

        img = np.ascontiguousarray(img)
        for bbox, label in zip(bboxes, labels):
            bbox_int = bbox.astype(np.int32)
            left_top = (bbox_int[0], bbox_int[1])
            right_bottom = (bbox_int[2], bbox_int[3])
            cv2.rectangle(img,
                          left_top,
                          right_bottom,
                          self.colors[label],
                          thickness=self.thickness)
            label_text = self.classes[
                label] if self.classes is not None else f'cls {label}'
            if len(bbox) > 4:
                label_text += f'|{bbox[-1]:.02f}'
            cv2.putText(img, label_text, (bbox_int[0], bbox_int[1] -
                                          (label * 2 * randint(0, 1))),
                        cv2.FONT_HERSHEY_COMPLEX, self.font_scale,
                        self.colors[label])

        if show:
            imshow(img, self.win_name, self.wait_time)
        if out_file is not None:
            imwrite(img, out_file)
        return img
Ejemplo n.º 11
0
def imshow_det_bboxes(img,
                      bboxes,
                      labels,
                      class_names=None,
                      score_thr=0,
                      bbox_color='green',
                      text_color='green',
                      thickness=3,
                      font_scale=0.8,
                      show=True,
                      win_name='',
                      wait_time=0,
                      out_file=None):
    """Draw bboxes and class labels (with scores) on an image.

    Args:
        img (str or ndarray): The image to be displayed.
        bboxes (ndarray): Bounding boxes (with scores), shaped (n, 4) or
            (n, 5).
        labels (ndarray): Labels of bboxes.
        class_names (list[str]): Names of each classes.
        score_thr (float): Minimum score of bboxes to be shown.
        bbox_color (str or tuple or :obj:`Color`): Color of bbox lines.
        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 or None): The filename to write the image.
    """
    assert bboxes.ndim == 2
    assert labels.ndim == 1
    assert bboxes.shape[0] == labels.shape[0]
    assert bboxes.shape[1] == 4 or bboxes.shape[1] == 5
    img = imread(img)

    if score_thr > 0:
        assert bboxes.shape[1] == 5
        scores = bboxes[:, -1]
        inds = scores > score_thr
        bboxes = bboxes[inds, :]
        labels = labels[inds]

    bbox_color = random_colors(80)  # color_val(bbox_color)
    text_color = random_colors(80)  # color_val(text_color)

    for bbox, label in zip(bboxes, labels):
        bbox_int = bbox.astype(np.int32)
        left_top = (bbox_int[0], bbox_int[1])
        right_bottom = (bbox_int[2], bbox_int[3])
        tmp_bbox_color = bbox_color[label]
        tmp_bbox_color = (int(tmp_bbox_color[2] * 255),
                          int(tmp_bbox_color[1] * 255),
                          int(tmp_bbox_color[0] * 255))
        cv2.rectangle(img,
                      left_top,
                      right_bottom,
                      tmp_bbox_color,
                      thickness=thickness)
        label_text = class_names[
            label] if class_names is not None else 'cls {}'.format(label)
        if len(bbox) > 4:
            label_text += '|{:.02f}'.format(bbox[-1])
        cv2.putText(img, label_text, (bbox_int[0], bbox_int[1] - 2),
                    cv2.FONT_HERSHEY_COMPLEX, font_scale, tmp_bbox_color)

    if show:
        imshow(img, win_name, wait_time)
    if out_file is not None:
        imwrite(img, out_file)
Ejemplo n.º 12
0
    def show_result(self,
                    data,
                    result,
                    img_norm_cfg,
                    dataset=None,
                    score_thr=0.3,
                    out_file=None):
        with_sema = False
        if isinstance(result, tuple):
            if len(result) == 2:
                bbox_result, segm_result = result
            else:
                bbox_result, segm_result, sema_result = result
                with_sema = True
        else:
            bbox_result, segm_result = result, None

        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)

        if dataset is None:
            class_names = self.CLASSES
        elif isinstance(dataset, str):
            class_names = get_classes(dataset)
        elif isinstance(dataset, (list, tuple)):
            class_names = dataset
        else:
            raise TypeError(
                'dataset must be a valid dataset name or a sequence'
                ' of class names, not {}'.format(type(dataset)))

        for img, img_meta in zip(imgs, img_metas):
            h, w, _ = img_meta['img_shape']
            img_show = img[:h, :w, :]

            bboxes = np.vstack(bbox_result)
            # draw segmentation masks
            if segm_result is not None:
                segms = mmcv.concat_list(segm_result)
                inds = np.where(bboxes[:, -1] > score_thr)[0]
                for i in inds:
                    color_mask = np.random.randint(
                        0, 256, (1, 3), dtype=np.uint8)
                    mask = maskUtils.decode(segms[i]).astype(np.bool)
                    img_show[mask] = img_show[mask] * 0.5 + color_mask * 0.5
            # draw bounding boxes
            labels = [
                np.full(bbox.shape[0], i, dtype=np.int32)
                for i, bbox in enumerate(bbox_result)
            ]
            labels = np.concatenate(labels)
            mmcv.imshow_det_bboxes(
                img_show,
                bboxes,
                labels,
                bbox_color='red',
                text_color='red',
                show=out_file is None,
                class_names=class_names,
                score_thr=score_thr,
                out_file=out_file)

            if with_sema is True:
                sema_result = mmcv.imresize(sema_result, (w, h),
                                            interpolation='nearest')
                imwrite(sema_result, out_file[:-4] + '-sema.png')
Ejemplo n.º 13
0
def draw_det_bboxes_A(img_name,
                      bboxes,
                      labels,
                      imgd,
                      colors,
                      width=None,
                      class_names=None,
                      score_thr=0.5,
                      out_file=None):
    """Draw bboxes and class labels (with scores) on an image.

    Args:
        img (str or ndarray): The image to be displayed.
        bboxes (ndarray): Bounding boxes (with scores), shaped (n, 4) or
            (n, 5).
        labels (ndarray): Labels of bboxes.
        class_names (list[str]): Names of each classes.
        score_thr (float): Minimum score of bboxes to be shown.
        out_file (str or None): The filename to write the image.
    """
    assert bboxes.ndim == 2
    assert labels.ndim == 1
    assert bboxes.shape[0] == labels.shape[0]
    assert bboxes.shape[1] == 4 or bboxes.shape[1] == 5

    img = imread(img_name)
    img = img.copy()

    ori_size = img.shape

    ratio = width / ori_size[0]
    img = cv2.resize(img, (int(ori_size[1] * ratio), int(ori_size[0] * ratio)))

    scores = bboxes[:, -1]

    if score_thr > 0.0:
        assert bboxes.shape[1] == 5
        inds = scores > score_thr
        bboxes = bboxes[inds, :]
        labels = labels[inds]
        scores = scores[inds]

    # Pred = Prediction(img_name = str(imgd),
    #                     pred_num = labels.shape[0],
    #                     img = imgd)
    # Pred.save()

    imgd.pred_num = labels.shape[0]
    imgd.save()

    if labels.shape[0] == 0:
        write_det(imgd,
                  box_id='',
                  pred_cls='',
                  score=0,
                  bbox_int=None,
                  nodet=True)

    ABC = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    i = 0

    for bbox, label, score in zip(bboxes, labels, scores):

        pred_cls = class_names[label]
        color = colors[pred_cls]
        box_id = ABC[i]

        bbox = bbox * ratio
        bbox_int = bbox.astype(np.int32)

        write_det(imgd, box_id, pred_cls, score, bbox_int)

        left_top = (bbox_int[0], bbox_int[1])
        right_bottom = (bbox_int[2], bbox_int[3])

        cv2.rectangle(img, (left_top[0], left_top[1]),
                      (right_bottom[0], right_bottom[1]), color, 4)
        text_size, baseline = cv2.getTextSize(box_id, cv2.FONT_HERSHEY_SIMPLEX,
                                              1.3, 2)
        p1 = (left_top[0], left_top[1] + text_size[1])
        cv2.rectangle(img, tuple(left_top), (p1[0] + text_size[0], p1[1] + 1),
                      color, -1)
        cv2.putText(img, box_id, (p1[0], p1[1]), cv2.FONT_HERSHEY_SIMPLEX, 1.3,
                    (255, 255, 255), 2, 8)

        i += 1

    print('done   ' + str(out_file))

    if out_file is not None:
        imwrite(img, out_file)
    return i
    def load_anno_idx(
        self,
        idx,
        img_concat,
        train,
        draw_dir='/data/home/yyj/code/kaggle/new_code/Kaggle_PKU_Baidu/data/pku_data/crop_visualization/crop_mesh'
    ):

        bboxes = []
        img1, img2, img3 = img_concat
        mask_all = np.zeros(img1.shape)
        merged_image1 = img1.copy()
        merged_image2 = img2.copy()
        merged_image3 = img3.copy()
        alpha = 0.8  # transparency

        gt = self._str2coords(train['PredictionString'].iloc[idx])
        for gt_pred in gt:
            eular_angle = np.array(
                [gt_pred['yaw'], gt_pred['pitch'], gt_pred['roll']])

            translation = np.array([gt_pred['x'], gt_pred['y'], gt_pred['z']])
            quaternion = euler_angles_to_quaternions(eular_angle)
            quaternion_semisphere = quaternion_upper_hemispher(quaternion)

            new_eular_angle = quaternion_to_euler_angle(quaternion_semisphere)

            # rendering the car according to:
            # https://www.kaggle.com/ebouteillon/augmented-reality

            # car_id2name is from:
            # https://github.com/ApolloScapeAuto/dataset-api/blob/master/car_instance/car_models.py
            car_name = car_id2name[gt_pred['id']].name
            vertices = np.array(self.car_model_dict[car_name]['vertices'])
            vertices[:, 1] = -vertices[:, 1]
            triangles = np.array(self.car_model_dict[car_name]['faces']) - 1

            # project 3D points to 2d image plane
            yaw, pitch, roll = gt_pred['yaw'], gt_pred['pitch'], gt_pred[
                'roll']
            # I think the pitch and yaw should be exchanged
            yaw, pitch, roll = -pitch, -yaw, -roll
            Rt = np.eye(4)
            t = np.array([gt_pred['x'], gt_pred['y'], gt_pred['z']])
            Rt[:3, 3] = t
            Rt[:3, :3] = euler_to_Rot(yaw, pitch, roll).T
            Rt = Rt[:3, :]
            P = np.ones((vertices.shape[0], vertices.shape[1] + 1))
            P[:, :-1] = vertices
            P = P.T

            img_cor_points = np.dot(self.camera_matrix, np.dot(Rt, P))
            img_cor_points = img_cor_points.T
            img_cor_points[:, 0] /= img_cor_points[:, 2]
            img_cor_points[:, 1] /= img_cor_points[:, 2]

            # project 3D points to 2d image plane
            x1, y1, x2, y2 = img_cor_points[:,
                                            0].min(), img_cor_points[:, 1].min(
                                            ), img_cor_points[:, 0].max(
                                            ), img_cor_points[:, 1].max()
            bboxes.append([x1, y1, x2, y2])

            # project 3D points to 2d image plane
            mask_seg = np.zeros(img1.shape, dtype=np.uint8)
            mask_seg_mesh = np.zeros(img1.shape, dtype=np.uint8)
            for t in triangles:
                coord = np.array([
                    img_cor_points[t[0]][:2], img_cor_points[t[1]][:2],
                    img_cor_points[t[2]][:2]
                ],
                                 dtype=np.int32)
                # This will draw the mask for segmenation
                cv2.drawContours(mask_seg, np.int32([coord]), 0, (0, 0, 255),
                                 -1)
                # cv2.polylines(mask_seg_mesh, np.int32([coord]), 1, (0, 255, 0))

            mask_all += mask_seg

        # if False:
        mask_all = mask_all * 255 / mask_all.max()
        cv2.addWeighted(img1.astype(np.uint8), 1.0, mask_all.astype(np.uint8),
                        alpha, 0, merged_image1)
        cv2.addWeighted(img2.astype(np.uint8), 1.0, mask_all.astype(np.uint8),
                        alpha, 0, merged_image2)
        cv2.addWeighted(img3.astype(np.uint8), 1.0, mask_all.astype(np.uint8),
                        alpha, 0, merged_image3)

        imwrite(merged_image1,
                os.path.join(draw_dir, train['ImageId'].iloc[idx] + '_1.jpg'))
        imwrite(merged_image2,
                os.path.join(draw_dir, train['ImageId'].iloc[idx] + '_2.jpg'))
        imwrite(merged_image3,
                os.path.join(draw_dir, train['ImageId'].iloc[idx] + '_3.jpg'))
Ejemplo n.º 15
0
def show_det_lines(img,
                   lines,
                   labels,
                   class_names=None,
                   score_thr=0,
                   line_color='green',
                   text_color='green',
                   thickness=1,
                   font_scale=0.5,
                   show=True,
                   win_name='',
                   wait_time=0,
                   out_file=None,
                   key_points=None,
                   point_color='red',
                   scores=None):
    '''
  img: [h,w,3]
  lines: [n,6]
  labels: [n]
  scores: [n] or None
  Use lines[:,-1] as score when scores is None, otherwise use scores as the score for filtering lines.
  Always show lines[:,-1] in the image.
  '''
    assert lines.ndim == 2
    assert lines.shape[1] == 6 or lines.shape[1] == 5
    assert labels.ndim == 1
    assert labels.shape[0] == lines.shape[0]
    if key_points is not None:
        assert key_points.shape[0] == lines.shape[0]

    img = imread(img.copy())

    if score_thr > 0:
        assert lines.shape[1] == 6
        if scores is None:
            inds = lines[:, -1] > score_thr
        else:
            inds = scores > score_thr
        lines = lines[inds, :]
        labels = labels[inds]
        if key_points is not None:
            key_points = key_points[inds, :]

    line_color = color_val(line_color)
    text_color = color_val(text_color)
    point_color = color_val(point_color)

    i = -1
    for line, label in zip(lines, labels):
        i += 1
        istopleft = line[4] >= 0
        if not istopleft:
            line[0], line[2] = line[2], line[0]
        line_int = line.astype(np.int32)
        s = line_int[0:2]
        e = line_int[2:4]

        line_color = get_random_color()
        cv2.line(img, (s[0], s[1]), (e[0], e[1]),
                 line_color,
                 thickness=thickness)

        if key_points is not None:
            for j in range(key_points.shape[1]):
                p = key_points[i][j].astype(np.int32)
                cv2.circle(img, (p[0], p[1]),
                           2,
                           point_color,
                           thickness=thickness)

        label_text = class_names[
            label] if class_names is not None else 'cls {}'.format(label)
        label_text = ''
        if len(line) == 6:
            if DEBUG_CFG.OBJ_LEGEND == 'score':
                label_text += '{:.01f}'.format(line[-1])  # score
            else:
                label_text += '{:.01f}'.format(line[-2])  # rotation

        m = ((s + e) / 2).astype(np.int32)
        cv2.putText(img, label_text, (m[0] - 2, m[1] - 2),
                    cv2.FONT_HERSHEY_COMPLEX, font_scale, text_color)

    if show:
        imshow(img, win_name, wait_time)
    if out_file is not None:
        imwrite(img, out_file)
        print('\twrite {}'.format(out_file))
    pass
Ejemplo n.º 16
0
def imshow_det_bboxes(img,
                      bboxes,
                      labels,
                      class_names=None,
                      score_thr=0.3,
                      bbox_color='green',
                      text_color='white',
                      thickness=2,
                      font_scale=0.5,
                      show=True,
                      win_name='',
                      wait_time=0,
                      out_file=None,
                      image_id=0):
    assert bboxes.ndim == 2
    assert labels.ndim == 1
    assert bboxes.shape[0] == labels.shape[0]
    assert bboxes.shape[1] == 4 or bboxes.shape[1] == 5
    img = imread(img)

    if score_thr > 0:
        assert bboxes.shape[1] == 5
        scores = bboxes[:, -1]
        inds = scores > score_thr
        bboxes = bboxes[inds, :]
        labels = labels[inds]

    bbox_color = color_val(bbox_color)
    text_color = color_val(text_color)
    # results[image_id] = []

    # do tracking
    bbox_xyxy = bboxes[:,:4]
    cls_conf = bboxes[..., 4]
    w = bbox_xyxy[..., 2] - bbox_xyxy[..., 0]
    h = bbox_xyxy[..., 3] - bbox_xyxy[..., 1]
    bbox_xyxy[:, 2] = w
    bbox_xyxy[:, 3] = h

    index = 0
    outbb = np.copy(bbox_xyxy)
    dd = []
    for bbox, label in zip(bboxes, labels):
        if label != 0:
            dd.append(index)
        index += 1
    outbb = np.delete(outbb, dd, 0)
    cls_conf = np.delete(cls_conf, dd, 0)
    labels = np.delete(labels, dd, 0)

    if image_id ==1:
        for i in range(2):
            outputs = deepsort.update(outbb, cls_conf, img)
    else:
        outputs = deepsort.update(outbb, cls_conf, img)

    if len(outputs) > 0:
        bboxes = outputs[:, :4]
        ids = outputs[:, -1]

        for bbox, label, id in zip(bboxes, labels, ids):
            if label == 0:
                bbox_int = bbox.astype(np.int32)
                left_top = (bbox_int[0], bbox_int[1])
                right_bottom = (bbox_int[2], bbox_int[3])
                cv2.rectangle(
                    img, left_top, right_bottom, bbox_color, thickness=thickness)
                label_text = 'ID {}'.format(id)
                if len(bbox) > 4:
                    label_text += '|{:.02f}'.format(bbox[-1])

                _height_half = int((bbox_int[3]-bbox_int[1])/2)
                cv2.putText(img, label_text, (bbox_int[0], bbox_int[1] + _height_half),
                            cv2.FONT_HERSHEY_COMPLEX, font_scale, text_color)

                mybbox = bbox.astype(np.float).tolist()
                results.append((image_id, mybbox, id))



        if show:
            imshow(img, win_name, wait_time)
        if out_file is not None:
            imwrite(img, out_file)
def imshow_det_pts(img,
                   bboxes,
                   pts,
                   labels,
                   class_names=None,
                   score_thr=0,
                   thickness=1,
                   font_scale=0.5,
                   show=True,
                   win_name='',
                   wait_time=0,
                   draw_pts=True,
                   out_file=None):
    """Draw bboxes and class labels (with scores) on an image.

    Args:
        img (str or ndarray): The image to be displayed.
        bboxes (ndarray): Bounding boxes (with scores), shaped (n, 4) or
            (n, 5).
        labels (ndarray): Labels of bboxes.
        class_names (list[str]): Names of each classes.
        score_thr (float): Minimum score of bboxes to be shown.
        bbox_color (str or tuple or :obj:`Color`): Color of bbox lines.
        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 or None): The filename to write the image.
    """
    assert pts.ndim == 2
    assert labels.ndim == 1
    assert pts.shape[0] == labels.shape[0]
    img = imread(img)

    if score_thr > 0:
        scores = pts[:, -1]
        inds = scores > score_thr
        pts = pts[inds, :]
        bboxes = bboxes[inds, :]
        labels = labels[inds]

    for pt, bbox, label in zip(pts, bboxes, labels):
        r_color = (np.random.randint(0, 256), np.random.randint(0, 256), np.random.randint(0, 256))
        pt_x = pt[0:-1:3]
        pt_y = pt[1:-1:3]
        pt_score = pt[2:-1:3]
        left_top = (int(bbox[0]), int(bbox[1]))
        right_bottom = (int(bbox[2]), int(bbox[3]))
        cv2.rectangle(
            img, left_top, right_bottom, r_color, thickness=thickness)
        if draw_pts:
            for i in range(pt_x.shape[0]):
                r_fore_color = r_color
                r_back_color = [int(i / 2) for i in r_color]
                r_show_color = r_fore_color if pt_score[i] > 0.5 else r_back_color
                cv2.circle(img, (pt_x[i], pt_y[i]), 1, r_show_color, thickness=2)
        label_text = class_names[
            label] if class_names is not None else 'cls {}'.format(label)
        if len(pt) % 2 == 1:
            label_text += '|{:.02f}'.format(pt[-1])
        cv2.putText(img, label_text, (int(bbox[0]), int(bbox[1]) - 2),
                    cv2.FONT_HERSHEY_COMPLEX, font_scale, r_color)

    if show:
        imshow(img, win_name, wait_time)
    if out_file is not None:
        imwrite(img, out_file)
def imshow_det_rbboxes(img,
                       bboxes,
                       labels,
                       class_names=None,
                       score_thr=0,
                       bbox_color='green',
                       text_color='green',
                       thickness=1,
                       font_scale=0.5,
                       show=True,
                       win_name='',
                       wait_time=0,
                       out_file=None):
    """Draw bboxes and class labels (with scores) on an image.

    Args:
        img (str or ndarray): The image to be displayed.
        bboxes (ndarray): Bounding boxes (with scores), shaped (n, 5) or
            (n, 6).
        labels (ndarray): Labels of bboxes.
        class_names (list[str]): Names of each classes.
        score_thr (float): Minimum score of bboxes to be shown.
        bbox_color (str or tuple or :obj:`Color`): Color of bbox lines.
        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 or None): The filename to write the image.
    """
    assert bboxes.ndim == 2
    assert labels.ndim == 1
    assert bboxes.shape[0] == labels.shape[0]
    assert bboxes.shape[1] == 5 or bboxes.shape[1] == 6
    img = imread(img)

    if score_thr > 0:
        assert bboxes.shape[1] == 6
        scores = bboxes[:, -1]
        inds = scores > score_thr
        bboxes = bboxes[inds, :]
        labels = labels[inds]

    bbox_color = color_val(bbox_color)
    text_color = color_val(text_color)

    for bbox, label in zip(bboxes, labels):
        xc, yc, w, h, ag, p = bbox.tolist()
        wx, wy = w / 2 * math.cos(ag), w / 2 * math.sin(ag)
        hx, hy = -h / 2 * math.sin(ag), h / 2 * math.cos(ag)
        p1 = (xc - wx - hx, yc - wy - hy)
        p2 = (xc + wx - hx, yc + wy - hy)
        p3 = (xc + wx + hx, yc + wy + hy)
        p4 = (xc - wx + hx, yc - wy + hy)
        ps = np.int0(np.array([p1, p2, p3, p4]))
        cv2.drawContours(img, [ps], -1, bbox_color, thickness=thickness)
        label_text = class_names[
            label] if class_names is not None else 'cls {}'.format(label)
        if len(bbox) > 5:
            label_text += '|{:.02f}'.format(bbox[-1])
        cv2.putText(img, label_text, (int(p1[0]), int(p1[1])),
                    cv2.FONT_HERSHEY_COMPLEX, font_scale, text_color)

    if show:
        imshow(img, win_name, wait_time)
    if out_file is not None:
        imwrite(img, out_file)
def imshow_det_rbboxes_360(img,
                           bboxes,
                           labels,
                           class_names=None,
                           score_thr=0,
                           bbox_color='green',
                           text_color='red',
                           thickness=1,
                           font_scale=0.5,
                           show=True,
                           win_name='',
                           wait_time=0,
                           out_file=None):
    """Draw bboxes and class labels (with scores) on an image.

    Args:
        img (str or ndarray): The image to be displayed.
        bboxes (ndarray): Bounding boxes (with scores), shaped (n, 5) or
            (n, 6).
        labels (ndarray): Labels of bboxes.
        class_names (list[str]): Names of each classes.
        score_thr (float): Minimum score of bboxes to be shown.
        bbox_color (str or tuple or :obj:`Color`): Color of bbox lines.
        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 or None): The filename to write the image.
    """
    assert bboxes.ndim == 2
    assert labels.ndim == 1
    assert bboxes.shape[0] == labels.shape[0]
    assert bboxes.shape[1] == 5 or bboxes.shape[1] == 6
    img = imread(img)

    if score_thr > 0:
        assert bboxes.shape[1] == 6
        scores = bboxes[:, -1]
        inds = scores > score_thr
        bboxes = bboxes[inds, :]
        labels = labels[inds]

    bbox_color = color_val(bbox_color)
    text_color = color_val(text_color)

    for bbox, label in zip(bboxes, labels):
        xc, yc, w, h, ag, p = bbox.tolist()
        wx, wy = w / 2 * math.cos(ag), w / 2 * math.sin(ag)
        hx, hy = -h / 2 * math.sin(ag), h / 2 * math.cos(ag)
        p1 = (xc - wx - hx, yc - wy - hy)
        p2 = (xc + wx - hx, yc + wy - hy)
        p3 = (xc + wx + hx, yc + wy + hy)
        p4 = (xc - wx + hx, yc - wy + hy)
        ps = np.int0(np.array([p1, p2, p3, p4]))
        cv2.drawContours(img, [ps], -1, bbox_color, thickness=thickness)

        label_text = class_names[
            label] if class_names is not None else 'cls {}'.format(label)

        ##############
        # transfer 4 directions to 360
        ag = ag / math.pi * 180

        if "RUP" in label_text:
            ag += 90
        elif "RDOWN" in label_text:
            ag += 180
        elif "LDOWN" in label_text:
            ag += 270
        else:
            ag += 360

        if ag < 0:
            ag += 360
            print("negative:{},{}".format(ag, out_file))

        # Draw direction line
        p1_x, p1_y = xc, yc
        p2_x, p2_y = xc, yc - 80
        rotateMat = cv2.getRotationMatrix2D((p1_x, p1_y), -ag, 1)
        [[p1_x], [p1_y]] = np.dot(rotateMat, np.array([[p1_x], [p1_y], [1]]))
        [[p2_x], [p2_y]] = np.dot(rotateMat, np.array([[p2_x], [p2_y], [1]]))
        cv2.line(img, (int(p1_x), int(p1_y)), (int(p2_x), int(p2_y)),
                 text_color,
                 thickness=thickness)

        label_text = label_text.split('-')[0]
        ##############

        if len(bbox) > 5:
            label_text += '|{:.02f}, {:.02f}'.format(bbox[-1], ag)

        cv2.putText(img, label_text, (int(p1[0]), int(p1[1])),
                    cv2.FONT_HERSHEY_COMPLEX, font_scale, text_color)

    if show:
        imshow(img, win_name, wait_time)
    if out_file is not None:
        imwrite(img, out_file)
Ejemplo n.º 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
Ejemplo n.º 21
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,
                               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)
                                # custom index label
                                # font
                                font = cv2.FONT_HERSHEY_SIMPLEX

                                # org
                                org = (50, 50)

                                # fontScale
                                fontScale = 1

                                # Blue color in BGR
                                color = (255, 0, 0)

                                # Line thickness of 2 px
                                thickness = 2
                                cv2.putText(img_copy, str(kid),
                                            (int(x_coord), int(y_coord) - 10),
                                            font, fontScale, color, thickness,
                                            cv2.LINE_AA)
                                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)
                                # custom index label
                                # font
                                font = cv2.FONT_HERSHEY_SIMPLEX

                                # org
                                org = (50, 50)

                                # fontScale
                                fontScale = 1

                                # Blue color in BGR
                                color = (255, 0, 0)

                                # Line thickness of 2 px
                                thickness = 2
                                cv2.putText(img, str(kid),
                                            (int(x_coord), int(y_coord) - 10),
                                            font, fontScale, color, thickness,
                                            cv2.LINE_AA)

                # draw limbs
                if skeleton is not None and pose_limb_color is not None:
                    #print('color: ', len(pose_limb_color), len(skeleton))
                    #print(skeleton)
                    assert len(pose_limb_color) == len(skeleton)
                    #print(kpts)
                    for sk_id, sk in enumerate(skeleton):
                        #print(sk_id)
                        #print(sk)
                        #print('************')
                        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
Ejemplo n.º 22
0
def imshow_det_bboxes(img,
                      bboxes,
                      labels,
                      class_names=None,
                      score_thr=0,
                      bbox_color='green',
                      text_color='green',
                      thickness=1,
                      font_scale=0.5,
                      show=True,
                      win_name='',
                      wait_time=0,
                      out_file=None):
    """Draw bboxes and class labels (with scores) on an image.

    Args:
        img (str or ndarray): The image to be displayed.
        bboxes (ndarray): Bounding boxes (with scores), shaped (n, 4) or
            (n, 5).
        labels (ndarray): Labels of bboxes.
        class_names (list[str]): Names of each classes.
        score_thr (float): Minimum score of bboxes to be shown.
        bbox_color (str or tuple or :obj:`Color`): Color of bbox lines.
        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 or None): The filename to write the image.

    Returns:
        ndarray: The image with bboxes drawn on it.
    """
    assert bboxes.ndim == 2
    assert labels.ndim == 1
    assert bboxes.shape[0] == labels.shape[0]
    assert bboxes.shape[1] == 4 or bboxes.shape[1] == 5
    img = imread(img)

    if score_thr > 0:
        assert bboxes.shape[1] == 5
        scores = bboxes[:, -1]
        inds = scores > score_thr
        bboxes = bboxes[inds, :]
        labels = labels[inds]

    bbox_color01 = color_val(bbox_color)
    bbox_color02 = color_val('red')
    bbox_color03 = color_val('blue')
    text_color = color_val(text_color)
    img = np.ascontiguousarray(img)
    for bbox, label in zip(bboxes, labels):
        bbox_int = bbox.astype(np.int32)
        left_top = (bbox_int[0], bbox_int[1])
        right_bottom = (bbox_int[2], bbox_int[3])
        if label == 1:
            cv2.rectangle(img, left_top, right_bottom, bbox_color01, thickness=thickness)
        elif label == 2:
            cv2.rectangle(img, left_top, right_bottom, bbox_color02, thickness=thickness)
        else:
            cv2.rectangle(img, left_top, right_bottom, bbox_color03, thickness=thickness)
        label_text = class_names[
            label] if class_names is not None else f'cls {label}'
        if len(bbox) > 4:
            label_text += f'|{bbox[-1]:.02f}'
        
        if label == 1:
            cv2.putText(img, label_text, (bbox_int[0], bbox_int[1] - 2),
                cv2.FONT_HERSHEY_COMPLEX, font_scale, bbox_color02)
        elif label == 2:
            cv2.putText(img, label_text, (bbox_int[0], bbox_int[1] - 2),
                cv2.FONT_HERSHEY_COMPLEX, font_scale, bbox_color03)
        else:
            cv2.putText(img, label_text, (bbox_int[0], bbox_int[1] - 2),
                cv2.FONT_HERSHEY_COMPLEX, font_scale, bbox_color01)
    if show:
        imshow(img, win_name, wait_time)
    if out_file is not None:
        imwrite(img, out_file)
    return img
Ejemplo n.º 23
0
            else:
                if inter >= iou_threshold:
                    suppressed[j] = 1
    return keep  # 返回保留下来的下�?


for i in range(len(imgs)):
    print(imgs[i])
    img = Image.open(imgs[i])
    img = np.asarray(img)
    result = inference_detector(model, img)
    img_data = copy.copy(img)
    print(result)
    indx, score, bbox = selectClsScoreBoxFromResult(result)
    # keep = NMS_alphaRatioConf(bbox, score, 0.7, 0.7)
    keep = nms(bbox, score, 0.3, 10, soft_nms=True)
    for n in keep:
        # if score[n] < 0.6*max(score):
        #     continue
        left_top = (bbox[n][0], bbox[n][1])
        right_bottom = (bbox[n][2], bbox[n][3])
        cv2.rectangle(img_data,
                      left_top,
                      right_bottom, (0, 0, 255),
                      thickness=2)
        strText = str(class_nums[indx[n]]) + ': ' + str(format(
            score[n], '.4f'))
        cv2.putText(img_data, strText, (bbox[n][0], bbox[n][1] - 20),
                    cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 0, 0), 1)
    imwrite(img_data, os.path.join(save_path, files[i]))
Ejemplo n.º 24
0
def show_result(img,
                result,
                class_names,
                score_thr=0.8,
                wait_time=0,
                show=True,
                out_file=None):
    """Visualize the detection results on the image.

    Args:
        img (str or np.ndarray): Image filename or loaded image.
        result (tuple[list] or list): The detection result, can be either
            (bbox, segm) or just bbox.
        class_names (list[str] or tuple[str]): A list of class names.
        score_thr (float): The threshold to visualize the bboxes and masks.
        wait_time (int): Value of waitKey param.
        show (bool, optional): Whether to show the image with opencv or not.
        out_file (str, optional): If specified, the visualization result will
            be written to the out file instead of shown in a window.

    Returns:
        np.ndarray or None: If neither `show` nor `out_file` is specified, the
            visualized image is returned, otherwise None is returned.
    """
    assert isinstance(class_names, (tuple, list))
    img = mmcv.imread(img)
    img = img.copy()
    img[img < 255] = 255
    if isinstance(result, tuple):
        bbox_result, segm_result = result
    else:
        bbox_result, segm_result = result, None
    bboxes = np.vstack(bbox_result)
    labels = [
        np.full(bbox.shape[0], i, dtype=np.int32)
        for i, bbox in enumerate(bbox_result)
    ]
    labels = np.concatenate(labels)
    # draw segmentation masks
    if segm_result is not None:
        segms = mmcv.concat_list(segm_result)
        inds = np.where(bboxes[:, -1] > score_thr)[0]
        np.random.seed(42)
        color_masks = [
            #np.random.randint(0, 256, (1, 3), dtype=np.uint8)
            [[0, 0, 0]] for _ in range(max(labels) + 1)
        ]
        for i in inds:
            i = int(i)
            color_mask = color_masks[labels[i]]
            mask = maskUtils.decode(segms[i]).astype(np.bool)
            img[mask] = color_mask
            #img[mask] = img[mask] * 0.5 + color_mask * 0.5
    # if out_file specified, do not show image in window
    if out_file is not None:
        show = False
    # draw bounding boxes
    """
    mmcv.imshow_det_bboxes(
        img,
        bboxes,
        labels,
        class_names=class_names,
        score_thr=score_thr,
        show=show,
        wait_time=wait_time,
        out_file=out_file)
    """
    if show:
        imshow(img, win_name='', wait_time=0)
    if out_file is not None:
        imwrite(img, out_file)

    if not (show or out_file):
        return img
Ejemplo n.º 25
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,
                    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.
            pose_limb_color (np.array[Mx3]): Color of M 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:
            img (Tensor): 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 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)
                        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)

                        # 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_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:
                            # cv2.line(img, pos1, pos2, pose_kpt_color, 2, 8)
                            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)

                            r, g, b = pose_limb_color[sk_id]
                            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)

        if show:
            imshow(img, win_name, wait_time)

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

        return img
Ejemplo n.º 26
0
    def show_result(self,
                    img,
                    result,
                    skeleton=None,
                    kpt_score_thr=0.3,
                    bbox_color=None,
                    pose_kpt_color=None,
                    pose_limb_color=None,
                    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.
            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.
            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 image only if not `show` or `out_file`
        """

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

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

        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
Ejemplo n.º 27
0
    def show_result(self,
                    img,
                    result,
                    skeleton=None,
                    kpt_score_thr=0.3,
                    pose_kpt_color=None,
                    pose_limb_color=None,
                    radius=4,
                    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.
            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.
            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 image only if not `show` or `out_file`
        """

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

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

        for person_id, 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:
                        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)

            # 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):
                        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)

                        r, g, b = pose_limb_color[sk_id]
                        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)

        if show:
            imshow(img, win_name, wait_time)

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

        return img
Ejemplo n.º 28
0
def imshow_det_bboxes(
    img,
    bboxes,
    labels,
    class_names=None,
    score_thr=0,
    bbox_color="green",
    text_color="green",
    thickness=1,
    font_scale=0.5,
    show=True,
    win_name="",
    wait_time=0,
    out_file=None,
    vis_tool="matplotlib",
):
    """Draw bboxes and class labels (with scores) on an image.

    Args:
        img (str or ndarray): The image to be displayed.
        bboxes (ndarray): Bounding boxes (with scores), shaped (n, 4) or
            (n, 5).
        labels (ndarray): Labels of bboxes.
        class_names (list[str]): Names of each classes.
        score_thr (float): Minimum score of bboxes to be shown.
        bbox_color (str or tuple or :obj:`Color`): Color of bbox lines.
        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 or None): The filename to write the image.
    """
    assert bboxes.ndim == 2
    assert labels.ndim == 1
    assert bboxes.shape[0] == labels.shape[0]
    assert bboxes.shape[1] == 4 or bboxes.shape[1] == 5
    img = imread(img)

    if score_thr > 0:
        assert bboxes.shape[1] == 5
        scores = bboxes[:, -1]
        inds = scores > score_thr
        bboxes = bboxes[inds, :]
        labels = labels[inds]

    bbox_color = color_val(bbox_color)
    text_color = color_val(text_color)

    for bbox, label in zip(bboxes, labels):
        bbox_int = bbox.astype(np.int32)
        left_top = (bbox_int[0], bbox_int[1])
        right_bottom = (bbox_int[2], bbox_int[3])
        cv2.rectangle(img, left_top, right_bottom, bbox_color, thickness=thickness)
        label_text = class_names[label] if class_names is not None else "cls {}".format(label)
        if len(bbox) > 4:
            label_text += "|{:.02f}".format(bbox[-1])
        cv2.putText(img, label_text, (bbox_int[0], bbox_int[1] - 2), cv2.FONT_HERSHEY_COMPLEX, font_scale, text_color)

    if show:
        if vis_tool == "matplotlib":
            fig = plt.figure(frameon=False, figsize=(8, 6), dpi=100)
            tmp = fig.add_subplot(1, 1, 1)
            tmp.set_title("{}".format(win_name))
            plt.axis("off")
            plt.imshow(img[:, :, [2, 1, 0]])
            plt.show()
        else:  # use 'mmcv'
            imshow(img, win_name, wait_time)
    if out_file is not None:
        imwrite(img, out_file)
Ejemplo n.º 29
0
    def show_result(self,
                    img,
                    result,
                    skeleton=None,
                    kpt_score_thr=0.3,
                    bbox_color='green',
                    pose_kpt_color=None,
                    pose_link_color=None,
                    text_color='white',
                    radius=4,
                    thickness=1,
                    font_scale=0.5,
                    bbox_thickness=1,
                    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.
                skeleton is 0-based indexing.
            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_link_color (np.array[Mx3]): Color of M links.
                If None, do not draw links.
            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()

        bbox_result = []
        bbox_labels = []
        pose_result = []
        for res in result:
            if 'bbox' in res:
                bbox_result.append(res['bbox'])
                bbox_labels.append(res.get('label', None))
            pose_result.append(res['keypoints'])

        if bbox_result:
            bboxes = np.vstack(bbox_result)
            # draw bounding boxes
            imshow_bboxes(img,
                          bboxes,
                          labels=bbox_labels,
                          colors=bbox_color,
                          text_color=text_color,
                          thickness=bbox_thickness,
                          font_scale=font_scale,
                          show=False)

        if pose_result:
            imshow_keypoints(img, pose_result, skeleton, kpt_score_thr,
                             pose_kpt_color, pose_link_color, radius,
                             thickness)

        if show:
            imshow(img, win_name, wait_time)

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

        return img
Ejemplo n.º 30
0
def imshow_det_bboxes_poses(
    img,
    bboxes,
    labels,
    class_names=None,
    score_thr=0,
    bbox_color="green",
    text_color="green",
    thickness=1,
    font_scale=0.5,
    show=True,
    win_name="",
    wait_time=0,
    out_file=None,
    poses=None,
    corners_3d=None,
    dataste_name=None,
    renderer=None,
    K=None,
    vis_tool="matplotlib",
):
    """Draw bboxes and class labels (with scores) on an image. Render the
    contours of poses to image. (or the 3d bounding box)

    Args:
        img (str or ndarray): The image to be displayed.
        bboxes (ndarray): Bounding boxes (with scores), shaped (n, 4) or
            (n, 5).
        labels (ndarray): Labels of bboxes. 0-based
        class_names (list[str]): Names of each classes.
        score_thr (float): Minimum score of bboxes to be shown.
        bbox_color (str or tuple or :obj:`Color`): Color of bbox lines.
        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 or None): The filename to write the image.
        ------
        poses:
        corners_3d: dict of 3d corners(un-transformed), key is cls_name
        dataset_name: camera intrinsic parameter
        renderer:
        K: camera intrinsic
    """
    # logger.info('poses: {}'.format(poses))
    assert bboxes.ndim == 2
    assert labels.ndim == 1
    assert bboxes.shape[0] == labels.shape[0]
    assert bboxes.shape[1] == 4 or bboxes.shape[1] == 5
    img = imread(img)

    if score_thr > 0:
        assert bboxes.shape[1] == 5
        scores = bboxes[:, -1]
        inds = scores > score_thr
        bboxes = bboxes[inds, :]
        labels = labels[inds]

    bbox_color = color_val(bbox_color)
    text_color = color_val(text_color)

    for bbox, label in zip(bboxes, labels):
        # pose
        if poses is not None:
            if poses[label]:
                pose = poses[label][0]  # TODO: handle multiple poses
                bgr, depth = renderer.render(label, pose[:, :3], pose[:, 3], r_type="mat")
                # img = img - bgr
                pose_mask = np.zeros(depth.shape)
                pose_mask[depth != 0] = 1
                edges_3 = mask_utils.get_edge(pose_mask, bw=3)
                edges_3[:, :, [0, 1]] = 0  # red
                img[edges_3 != 0] = 255
                cls_name = class_names[label]
                corners_2d, _ = misc_6d.points_to_2D(corners_3d[cls_name], pose[:, :3], pose[:, 3], K)
                img = misc_6d.draw_projected_box3d(img, corners_2d, thickness=thickness)

        bbox_int = bbox.astype(np.int32)
        left_top = (bbox_int[0], bbox_int[1])
        right_bottom = (bbox_int[2], bbox_int[3])
        cv2.rectangle(img, left_top, right_bottom, bbox_color, thickness=thickness)
        label_text = class_names[label] if class_names is not None else "cls {}".format(label)
        if len(bbox) > 4:
            label_text += "|{:.02f}".format(bbox[-1])
        cv2.putText(img, label_text, (bbox_int[0], bbox_int[1] - 2), cv2.FONT_HERSHEY_COMPLEX, font_scale, text_color)

    if show:
        if vis_tool == "matplotlib":
            fig = plt.figure(frameon=False, figsize=(8, 6), dpi=100)
            tmp = fig.add_subplot(1, 1, 1)
            tmp.set_title("{}".format(win_name))
            plt.axis("off")
            plt.imshow(img[:, :, [2, 1, 0]])
            plt.show()
        else:  # use 'mmcv'
            imshow(img, win_name, wait_time)
    if out_file is not None:
        imwrite(img, out_file)
    return img