示例#1
0
def imshow_bboxes(img,
                  bboxes,
                  bbox_color=(0, 255, 0),
                  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

    img = imread(img)

    for bbox in bboxes:
        left_top = (bbox[0], bbox[1])
        right_bottom = (bbox[2], bbox[3])
        cv2.rectangle(img, left_top, right_bottom, bbox_color, thickness)

    if show:
        cv2.imshow(win_name, imread(img))
        cv2.waitKey(wait_time)
    if out_file is not None:
        imwrite(img, out_file)
示例#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)
def show_rmask(data,
               result,
               img_norm_cfg,
               class_names,
               score_thr=0.3,
               file_name='0.png'):

    bbox_result, segm_result, rbbox_result = result
    img_tensor = data['img'][0]
    img_metas = data['img_meta'][0].data[0]
    imgs = tensor2imgs(img_tensor, **img_norm_cfg)

    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)
        rbboxes = np.vstack(rbbox_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 rbbox
        labels = [
            np.full(bbox.shape[0], i, dtype=np.int32)
            for i, bbox in enumerate(bbox_result)
        ]
        labels = np.concatenate(labels)

        img = imread(img_show)

        scores = rbboxes[:, -1]
        inds = scores > score_thr
        bboxes = bboxes[inds, :]
        rbboxes = rbboxes[inds, :8]
        labels = labels[inds]

        rbbox_color = (0, 0, 255)
        text_color = (0, 255, 0)
        font_scale = 0.5
        '''
        for rbbox, bbox, label in zip(rbboxes, bboxes, labels):
            bbox_int = bbox.astype(np.int32)
            rbbox_int = rbbox.astype(np.int32)
            rbbox_int = rbbox_int.reshape(4,2)
            cv2.drawContours(img,[rbbox_int],0,rbbox_color,2)
            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)
        '''
        cv2.imwrite(file_name, img)
示例#4
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
示例#5
0
def imshow(img, win_name='', wait_time=0):
    """Show an image.

    Args:
        img (str or ndarray): The image to be displayed.
        win_name (str): The window name.
        wait_time (int): Value of waitKey param.
    """
    cv2.imshow(win_name, imread(img))
    cv2.waitKey(wait_time)
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):
    """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.
    """
    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)
    return img
示例#7
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
示例#8
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)
示例#9
0
def imshow_polygons(img,
                    bboxes,
                    polygons,
                    labels,
                    class_names=None,
                    score_thr=0,
                    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)

    im = img[:, :, (2, 1, 0)]
    fig, ax = plt.subplots(figsize=(12, 12))
    fig = ax.imshow(im, aspect='equal')
    plt.axis('off')
    fig.axes.get_xaxis().set_visible(False)
    fig.axes.get_yaxis().set_visible(False)

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

    for bbox, label, polygon in zip(bboxes, labels, polygons):
        bbox_int = bbox.astype(np.int32)
        left_top = (bbox_int[0], bbox_int[1])
        right_bottom = (bbox_int[2], bbox_int[3])
        poly = polygon.reshape(-1, 2).astype(np.int32)

        color = np.random.rand(3)
        ax.add_patch(
            plt.Polygon(poly,
                        fill=True,
                        color=color,
                        alpha=0.5,
                        edgecolor=None))
        ax.add_patch(
            plt.Polygon(poly, fill=False, edgecolor='w', linewidth=1.0))

        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 out_file is not None:
        plt.savefig(out_file)
        plt.savefig(out_file.replace('jpg', 'pdf'))
        plt.cla()
        plt.close('all')
示例#10
0
def imshow(img, win_name='', wait_time=0):
    """Show an image.

    Args:
        img (str or ndarray): The image to be displayed.
        win_name (str): The window name.
        wait_time (int): Value of waitKey param.
    """
    # cv2.namedWindow("result",0)
    # cv2.resizeWindow("result",1920,1080)
    cv2.imshow('result', imread(img))
    cv2.waitKey(wait_time)
def show_rbbox_color(data,
                     result,
                     img_norm_cfg,
                     class_names,
                     score_thr=0.3,
                     file_name='0.png'):

    rbbox_result = result
    img_tensor = data['img'][0]
    img_metas = data['img_meta'][0].data[0]
    imgs = tensor2imgs(img_tensor, **img_norm_cfg)

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

        rbboxes = np.vstack(rbbox_result)

        # draw rbbox
        labels = [
            np.full(rbbox.shape[0], i, dtype=np.int32)
            for i, rbbox in enumerate(rbbox_result)
        ]
        labels = np.concatenate(labels)

        img = imread(img_show)

        scores = rbboxes[:, -1]
        inds = scores > score_thr
        rbboxes = rbboxes[inds, :8]
        labels = labels[inds]

        # rbbox_color = ncolors(16)
        rbbox_color = [[247, 11, 11], [244, 103, 19], \
            [250, 199, 48], [220, 245, 45], [150, 247, 52], \
                [69, 244, 44], [18, 243, 75], [27, 251, 167], \
                    [38, 248, 248], [18, 158, 242], [15, 74, 249], \
                        [33, 2, 253], [147, 44, 250], [220, 29, 248], \
                            [243, 16, 186], [250, 43, 121]]
        text_color = (0, 255, 0)
        font_scale = 0.5

        for rbbox, score, label in zip(rbboxes, scores, labels):
            rbbox_int = rbbox.astype(np.int32)
            rbbox_int = rbbox_int.reshape(4, 2)
            cv2.drawContours(img, [rbbox_int], 0, rbbox_color[label], 2)
            # label_text = class_names[
            #     label] if class_names is not None else 'cls {}'.format(label)
            # label_text += '|{:.02f}'.format(score)
            # cv2.putText(img, label_text, (rbbox_int[0][0], rbbox_int[0][1] - 2),
            #         cv2.FONT_HERSHEY_COMPLEX, font_scale, text_color)

        cv2.imwrite(file_name, img)
示例#12
0
def imshow(img, win_name='', wait_time=0):
    cv2.imshow(win_name, imread(img))
    if wait_time == 0:  # prevent from hangning if windows was closed
        while True:
            ret = cv2.waitKey(1)

            closed = cv2.getWindowProperty(win_name, cv2.WND_PROP_VISIBLE) < 1
            # if user closed window or if some key pressed
            if closed or ret != -1:
                break
    else:
        ret = cv2.waitKey(wait_time)
def imshow(img, win_name='', wait_time=0):
    """Show an image.

    Args:
        img (str or ndarray): The image to be displayed.
        win_name (str): The window name.
        wait_time (int): Value of waitKey param.
    """
    while True:
        cv2.imshow(win_name, imread(img))
        k = cv2.waitKey(wait_time) & 0xff
        if k == 27:
            break
    cv2.destroyAllWindows()
示例#14
0
文件: optflow.py 项目: zm66260/mmcv
def flowread(flow_or_path, quantize=False, concat_axis=0, *args, **kwargs):
    """Read an optical flow map.

    Args:
        flow_or_path (ndarray or str): A flow map or filepath.
        quantize (bool): whether to read quantized pair, if set to True,
            remaining args will be passed to :func:`dequantize_flow`.
        concat_axis (int): The axis that dx and dy are concatenated,
            can be either 0 or 1. Ignored if quantize is False.

    Returns:
        ndarray: Optical flow represented as a (h, w, 2) numpy array
    """
    if isinstance(flow_or_path, np.ndarray):
        if (flow_or_path.ndim != 3) or (flow_or_path.shape[-1] != 2):
            raise ValueError('Invalid flow with shape {}'.format(
                flow_or_path.shape))
        return flow_or_path
    elif not is_str(flow_or_path):
        raise TypeError(
            '"flow_or_path" must be a filename or numpy array, not {}'.format(
                type(flow_or_path)))

    if not quantize:
        with open(flow_or_path, 'rb') as f:
            try:
                header = f.read(4).decode('utf-8')
            except Exception:
                raise IOError('Invalid flow file: {}'.format(flow_or_path))
            else:
                if header != 'PIEH':
                    raise IOError(
                        'Invalid flow file: {}, header does not contain PIEH'.
                        format(flow_or_path))

            w = np.fromfile(f, np.int32, 1).squeeze()
            h = np.fromfile(f, np.int32, 1).squeeze()
            flow = np.fromfile(f, np.float32, w * h * 2).reshape((h, w, 2))
    else:
        assert concat_axis in [0, 1]
        cat_flow = imread(flow_or_path, flag='unchanged')
        if cat_flow.ndim != 2:
            raise IOError(
                '{} is not a valid quantized flow file, its dimension is {}.'.
                format(flow_or_path, cat_flow.ndim))
        assert cat_flow.shape[concat_axis] % 2 == 0
        dx, dy = np.split(cat_flow, 2, axis=concat_axis)
        flow = dequantize_flow(dx, dy, *args, **kwargs)

    return flow.astype(np.float32)
def imshow_det_bboxes_many(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')
示例#16
0
def test_model_inference(cfg_file):
    tmp_dir = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
    config_file = os.path.join(tmp_dir, cfg_file)
    model = build_model(config_file)
    with pytest.raises(AssertionError):
        model_inference(model, 1)

    sample_img_path = os.path.join(tmp_dir, '../demo/demo_text_det.jpg')
    model_inference(model, sample_img_path)

    # numpy inference
    img = imread(sample_img_path)

    model_inference(model, img)
示例#17
0
def test_model_batch_inference_det(cfg_file):
    tmp_dir = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
    config_file = os.path.join(tmp_dir, cfg_file)
    model = build_model(config_file)

    sample_img_path = os.path.join(tmp_dir, '../demo/demo_text_det.jpg')
    results = model_inference(model, [sample_img_path], batch_mode=True)

    assert len(results) == 1

    # numpy inference
    img = imread(sample_img_path)
    results = model_inference(model, [img], batch_mode=True)

    assert len(results) == 1
def show_bbox(data,
              result,
              img_norm_cfg,
              class_names,
              score_thr=0.3,
              file_name='0.png'):

    bbox_result = result
    img_tensor = data['img'][0]
    img_metas = data['img_meta'][0].data[0]
    imgs = tensor2imgs(img_tensor, **img_norm_cfg)

    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 rbbox
        labels = [
            np.full(bbox.shape[0], i, dtype=np.int32)
            for i, bbox in enumerate(bbox_result)
        ]
        labels = np.concatenate(labels)

        img = imread(img_show)

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

        bbox_color = (0, 255, 0)
        text_color = (0, 255, 0)
        font_scale = 0.5

        for bbox, label in zip(bboxes, labels):
            bbox_int = bbox.astype(np.int32)
            cv2.rectangle(img, (bbox_int[0], bbox_int[1]),
                          (bbox_int[2], bbox_int[3]), bbox_color, 1)
            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)

        cv2.imwrite(file_name, img)
def show_rbbox(data,
               result,
               img_norm_cfg,
               class_names,
               score_thr=0.3,
               file_name='0.png'):

    rbbox_result = result
    img_tensor = data['img'][0]
    img_metas = data['img_meta'][0].data[0]
    imgs = tensor2imgs(img_tensor, **img_norm_cfg)

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

        rbboxes = np.vstack(rbbox_result)

        # draw rbbox
        labels = [
            np.full(rbbox.shape[0], i, dtype=np.int32)
            for i, rbbox in enumerate(rbbox_result)
        ]
        labels = np.concatenate(labels)

        img = imread(img_show)

        scores = rbboxes[:, -1]
        inds = scores > score_thr
        rbboxes = rbboxes[inds, :8]
        labels = labels[inds]

        rbbox_color = (0, 255, 0)
        text_color = (0, 255, 0)
        font_scale = 0.5

        for rbbox, score, label in zip(rbboxes, scores, labels):
            rbbox_int = rbbox.astype(np.int32)
            rbbox_int = rbbox_int.reshape(4, 2)
            cv2.drawContours(img, [rbbox_int], 0, rbbox_color, 1)
            label_text = class_names[
                label] if class_names is not None else 'cls {}'.format(label)
            label_text += '|{:.02f}'.format(score)
            cv2.putText(img, label_text,
                        (rbbox_int[0][0], rbbox_int[0][1] - 2),
                        cv2.FONT_HERSHEY_COMPLEX, font_scale, text_color)

        cv2.imwrite(file_name, img)
示例#20
0
def test_model_batch_inference_raises_exception_error_aug_test_recog(cfg_file):
    tmp_dir = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
    config_file = os.path.join(tmp_dir, cfg_file)
    model = build_model(config_file)

    with pytest.raises(
            Exception,
            match='aug test does not support inference with batch size'):
        sample_img_path = os.path.join(tmp_dir, '../demo/demo_text_det.jpg')
        model_inference(model, [sample_img_path, sample_img_path])

    with pytest.raises(
            Exception,
            match='aug test does not support inference with batch size'):
        img = imread(sample_img_path)
        model_inference(model, [img, img])
示例#21
0
def imshow(img, win_name='', wait_time=0):
    """Show an image.
    Args:
        img (str or ndarray): The image to be displayed.
        win_name (str): The window name.
        wait_time (int): Value of waitKey param.
    """
    cv2.imshow(win_name, imread(img))
    if wait_time == 0:  # prevent from hangning if windows was closed
        while True:
            ret = cv2.waitKey(1)

            closed = cv2.getWindowProperty(win_name, cv2.WND_PROP_VISIBLE) < 1
            # if user closed window or if some key pressed
            if closed or ret != -1:
                break
    else:
        ret = cv2.waitKey(wait_time)
示例#22
0
def test_model_inference(cfg_file):
    tmp_dir = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
    config_file = os.path.join(tmp_dir, cfg_file)
    device = 'cpu'
    model = init_detector(config_file, checkpoint=None, device=device)
    if model.cfg.data.test['type'] == 'ConcatDataset':
        model.cfg.data.test.pipeline = model.cfg.data.test['datasets'][
            0].pipeline
    with pytest.raises(AssertionError):
        model_inference(model, 1)

    sample_img_path = os.path.join(tmp_dir, '../demo/demo_text_det.jpg')
    model_inference(model, sample_img_path)

    # numpy inference
    img = imread(sample_img_path)

    model_inference(model, img)
示例#23
0
def show_one_image(result, file_path, output_dir):

    found_image_bboxes = []
    found_image_bbox_labels = []

    file_name = file_path.split('/')[-1]

    for class_label, per_class_result in enumerate(result):
        # class_label: int
        # per_class_result: number-of-bboxes x [x, y, x, y, prob]
        for per_box_per_class_result in per_class_result:
            found_image_bbox_labels.append(class_label)
            found_image_bboxes.append(per_box_per_class_result)

    if len(found_image_bbox_labels) == 0:
        found_image_bboxes = np.zeros((0, 5))
        found_image_bbox_labels = np.zeros((0, ))
    else:
        found_image_bboxes = np.asarray(found_image_bboxes).reshape((-1, 5))
        found_image_bbox_labels = np.asarray(found_image_bbox_labels).reshape(
            (-1))

    img = imread(file_path)
    height, width, _ = img.shape

    imshow_det_bboxes(
        img=file_path,
        bboxes=found_image_bboxes,
        # labels should be 0 based.
        labels=found_image_bbox_labels,
        class_names=CLASSES,
        # no thr. show all.
        score_thr=threshold,
        bbox_color='green',
        text_color='green',
        # thickness (int): Thickness of both lines and fonts.
        thickness=np.int((height * width / 480 / 480)**0.5),
        # font_scale (float): Font scales of texts.
        font_scale=np.float((height * width / 480 / 480)**0.5) / 2,
        show=False,
        win_name='',
        wait_time=0,
        # save result to a file.
        out_file=output_dir + file_name)
示例#24
0
文件: base.py 项目: 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)
示例#25
0
def flowread(flow_or_path, quantize=False, concat_axis=0, *args, **kwargs):
    'Read an optical flow map.\n\n    Args:\n        flow_or_path (ndarray or str): A flow map or filepath.\n        quantize (bool): whether to read quantized pair, if set to True,\n            remaining args will be passed to :func:`dequantize_flow`.\n        concat_axis (int): The axis that dx and dy are concatenated,\n            can be either 0 or 1. Ignored if quantize is False.\n\n    Returns:\n        ndarray: Optical flow represented as a (h, w, 2) numpy array\n    '
    if isinstance(flow_or_path, np.ndarray):
        if ((flow_or_path.ndim != 3) or (flow_or_path.shape[(-1)] != 2)):
            raise ValueError(''.join(
                ['Invalid flow with shape ', '{}'.format(flow_or_path.shape)]))
        return flow_or_path
    elif (not is_str(flow_or_path)):
        raise TypeError(''.join([
            '"flow_or_path" must be a filename or numpy array, not ',
            '{}'.format(type(flow_or_path))
        ]))
    if (not quantize):
        with open(flow_or_path, 'rb') as f:
            try:
                header = f.read(4).decode('utf-8')
            except Exception:
                raise IOError(''.join(
                    ['Invalid flow file: ', '{}'.format(flow_or_path)]))
            else:
                if (header != 'PIEH'):
                    raise IOError(''.join([
                        'Invalid flow file: ', '{}'.format(flow_or_path),
                        ', header does not contain PIEH'
                    ]))
            w = np.fromfile(f, np.int32, 1).squeeze()
            h = np.fromfile(f, np.int32, 1).squeeze()
            flow = np.fromfile(f, np.float32, ((w * h) * 2)).reshape((h, w, 2))
    else:
        assert (concat_axis in [0, 1])
        cat_flow = imread(flow_or_path, flag='unchanged')
        if (cat_flow.ndim != 2):
            raise IOError(''.join([
                '{}'.format(flow_or_path),
                ' is not a valid quantized flow file, its dimension is ',
                '{}'.format(cat_flow.ndim), '.'
            ]))
        assert ((cat_flow.shape[concat_axis] % 2) == 0)
        (dx, dy) = np.split(cat_flow, 2, axis=concat_axis)
        flow = dequantize_flow(dx, dy, *args, **kwargs)
    return flow.astype(np.float32)
示例#26
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
    def restore_xyz_withIOU_single(self, idx, output_origin, car_cls_coco=2):
        output = copy.deepcopy(output_origin)
        print('idx', idx)
        img_name = os.path.join(self.test_image_folder,
                                os.path.basename(output[2]['file_name']))
        image = imread(img_name)
        bboxes, segms, six_dof = output[0], output[1], output[2]
        car_cls_score_pred = six_dof['car_cls_score_pred']
        quaternion_pred = six_dof['quaternion_pred']
        trans_pred_world = six_dof['trans_pred_world'].copy()
        euler_angle = np.array(
            [quaternion_to_euler_angle(x) for x in quaternion_pred])
        car_labels = np.argmax(car_cls_score_pred, axis=1)
        kaggle_car_labels = [self.unique_car_mode[x] for x in car_labels]
        car_names = np.array([car_id2name[x].name for x in kaggle_car_labels])

        assert len(bboxes[car_cls_coco]) == len(segms[car_cls_coco]) == len(kaggle_car_labels) \
               == len(trans_pred_world) == len(euler_angle) == len(car_names)
        # now we start to plot the image from kaggle
        quaternion_semisphere_refined, flag = refine_yaw_and_roll(
            image, bboxes[car_cls_coco], segms[car_cls_coco], car_names,
            euler_angle, quaternion_pred, trans_pred_world,
            self.car_model_dict, self.camera_matrix)
        if flag:
            output[2]['quaternion_pred'] = quaternion_semisphere_refined
            euler_angle = np.array([
                quaternion_to_euler_angle(x)
                for x in output[2]['quaternion_pred']
            ])

        trans_pred_world_refined = restore_x_y_from_z_withIOU(
            image, bboxes[car_cls_coco], segms[car_cls_coco], car_names,
            euler_angle, trans_pred_world, self.car_model_dict,
            self.camera_matrix)

        # print('change ',trans_pred_world,trans_pred_world_refined)
        output[2]['trans_pred_world'] = trans_pred_world_refined

        return output
示例#28
0
def show_one_image(truth, file_path, output_dir):

    found_image_bboxes = []
    found_image_bbox_labels = []

    file_name = file_path.split('/')[-1].split('.')[0]

    for per_truth in truth:
        if per_truth['filename'] == file_name:
            found_image_bboxes = per_truth['ann']['bboxes']
            found_image_bbox_labels = per_truth['ann']['labels'] - 1

    if len(found_image_bbox_labels) == 0:
        found_image_bboxes = np.zeros((0, 4))
        found_image_bbox_labels = np.zeros((0, ))

    img = imread(file_path)
    height, width, _ = img.shape

    imshow_det_bboxes(
        img=file_path,
        bboxes=found_image_bboxes,
        # labels should be 0 based.
        labels=found_image_bbox_labels,
        class_names=dental_detection_classes(),
        # no thr. show all.
        score_thr=0,
        bbox_color='green',
        text_color='green',
        # thickness (int): Thickness of both lines and fonts.
        thickness=np.int((height * width / 480 / 480)**0.5),
        # font_scale (float): Font scales of texts.
        font_scale=np.float((height * width / 480 / 480)**0.5) / 2,
        show=False,
        win_name='',
        wait_time=0,
        # save result to a file.
        out_file=output_dir + '{}_result.jpg'.format(file_name))
示例#29
0
def test_model_batch_inference_raises_exception_error_free_resize_recog(
        cfg_file):
    tmp_dir = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
    config_file = os.path.join(tmp_dir, cfg_file)
    model = build_model(config_file)

    with pytest.raises(
            Exception,
            match='Free resize do not support batch mode '
            'since the image width is not fixed, '
            'for resize keeping aspect ratio and '
            'max_width is not give.'):
        sample_img_path = os.path.join(tmp_dir, '../demo/demo_text_recog.jpg')
        model_inference(
            model, [sample_img_path, sample_img_path], batch_mode=True)

    with pytest.raises(
            Exception,
            match='Free resize do not support batch mode '
            'since the image width is not fixed, '
            'for resize keeping aspect ratio and '
            'max_width is not give.'):
        img = imread(sample_img_path)
        model_inference(model, [img, img], batch_mode=True)
示例#30
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