Пример #1
0
def vis_one_image_opencv(
    image: np.ndarray,
    boxes: np.ndarray,
    segms: Optional[Sequence[Segment]] = None,
    show_box: bool = False,
    show_class: bool = True,
) -> np.ndarray:
    """Constructs a numpy array with the detections visualized.

    Args:
        image: The image data
        boxes: The box data
        segms: Segmentations
        show_box: Whether to show the boxes
        show_class: Whether to show the object classes

    Return:
        The newly constructed image
    """

    if boxes is None or boxes.shape[0] == 0:
        return image

    if segms:
        color_list = colormap()
        mask_color_id = 0

    # Display in largest to smallest order to reduce occlusion
    areas = (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1])
    sorted_inds = np.argsort(-areas)

    for i in sorted_inds:
        bbox = boxes[i, :4]
        boxes[i, -1]

        # show box (off by default)
        if show_box:
            image = vis_bbox(
                image,
                (bbox[0], bbox[1], bbox[2] - bbox[0], bbox[3] - bbox[1]))

        # show class (off by default)
        if show_class:
            class_str = "hello"
            image = vis_class(image, (bbox[0], bbox[1] - 2), class_str)

        # show mask
        if segms and len(segms) > i:
            color_mask = color_list[mask_color_id % len(color_list), 0:3]
            mask_color_id += 1

            mask = decode_segment_to_mask(segms[i], image)
            image = vis_mask(image, mask, color_mask)

    return image
Пример #2
0
def vis_one_image(
    image: np.ndarray,
    image_name: str,
    output_dir: str,
    boxes: np.ndarray,
    segms: Optional[Sequence[Segment]] = None,
    dpi: int = 200,
    box_alpha: float = 0.0,
    show_class: bool = True,
    extension: str = "pdf",
) -> None:
    """Visual debugging of detections.

    Args:
        image: The image data
        image_name: The name of the image
        output_dir: Directory to output to
        boxes: Boxes
        segms: Segmentations
        dpi: DPI
        box_alpha: Alpha channel of the boxes
        show_class: Whether to show object classes
        extension: Extension of the output file
    """
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    if boxes is None or boxes.shape[0] == 0:
        return

    color_list = colormap(rgb=True) / 255
    plt.get_cmap("rainbow")

    fig = plt.figure(frameon=False)
    fig.set_size_inches(image.shape[1] / dpi, image.shape[0] / dpi)
    ax = plt.Axes(fig, [0.0, 0.0, 1.0, 1.0])
    ax.axis("off")
    fig.add_axes(ax)
    ax.imshow(image)

    sorted_inds: Union[List[Any], np.ndarray]
    if boxes is None:
        sorted_inds = []  # avoid crash when 'boxes' is None
    else:
        # Display in largest to smallest order to reduce occlusion
        areas = (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1])
        sorted_inds = np.argsort(-areas)

    mask_color_id = 0
    for i in sorted_inds:
        bbox = boxes[i, :4]

        # show box (off by default)
        ax.add_patch(
            plt.Rectangle(
                (bbox[0], bbox[1]),
                bbox[2] - bbox[0],
                bbox[3] - bbox[1],
                fill=False,
                edgecolor="g",
                linewidth=0.5,
                alpha=box_alpha,
            )
        )

        if show_class:
            ax.text(
                bbox[0],
                bbox[1] - 2,
                "WHERE IS THE TEXT car",
                fontsize=30,
                family="serif",
                bbox=dict(facecolor="g", alpha=0.4, pad=0, edgecolor="none"),
                color="white",
            )

        # show mask
        if segms is not None and len(segms) > i:
            img = np.ones(image.shape)
            color_mask = color_list[mask_color_id % len(color_list), 0:3]
            mask_color_id += 1

            w_ratio = 0.4
            for c in range(3):
                color_mask[c] = color_mask[c] * (1 - w_ratio) + w_ratio
            for c in range(3):
                img[:, :, c] = color_mask[c]

            e = decode_segment_to_mask(segms[i], image)
            e = e.astype(np.uint8)

            _, contours, hier = cv2.findContours(e.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)

            for contour in contours:
                polygon = Polygon(
                    contour.reshape((-1, 2)),
                    fill=True,
                    facecolor=color_mask,
                    edgecolor="w",
                    linewidth=1.2,
                    alpha=0.5,
                )
                ax.add_patch(polygon)

    output_name = os.path.basename(image_name) + "." + extension
    fig.savefig(os.path.join(output_dir, "{}".format(output_name)), dpi=dpi)
    plt.close("all")