Skip to content

Example outputs:


API Reference

paint_ground_truth_segmentation(ground_truth_file, ground_truth_image_folder, output_folder, categories='categories', category_id='category_id', opacity=0.5)

Paint ground truth in segmentation format.

Parameters:

Name Type Description Default
ground_truth_file str

path to file in segmentation format.

required
ground_truth_image_folder str

path to file of images associated with ground_truth_file.

required
output_folder str

plotting results will be generated in this folder. If it does not exists, it will be created.

required
categories str

name of the top level key holding information about the list of categories.

'categories'
category_id str

name of the annotation level key holding information about the category index.

'category_id'
opacity float

Set level of opacity of the annotations painted on top of the image. From 0 (transparent) to 1 (opaque).

0.5
Source code in src/stages/data/explore/paint_ground_truth_segmentation.py
def paint_ground_truth_segmentation(
    ground_truth_file: str,
    ground_truth_image_folder: str,
    output_folder: str,
    categories: str = "categories",
    category_id: str = "category_id",
    opacity: float = 0.5,
) -> None:
    """Paint ground truth in [segmentation](https://gradiant.github.io/ai-dataset-template/supported_tasks/#segmentation) format.

    Args:
        ground_truth_file:
            path to file in [segmentation](https://gradiant.github.io/ai-dataset-template/supported_tasks/#segmentation) format.
        ground_truth_image_folder:
            path to file of images associated with `ground_truth_file`.
        output_folder:
            plotting results will be generated in this folder.
            If it does not exists, it will be created.
        categories:
            name of the top level key holding information about the list of categories.
        category_id:
            name of the annotation level key holding information about the category index.
        opacity:
            Set level of opacity of the annotations painted on top of the image.
            From 0 (transparent) to 1 (opaque).
    """
    from PIL import Image
    from plotly import express as px
    from plotly import graph_objects as go

    Path(output_folder).mkdir(parents=True, exist_ok=True)
    ground_truth = json.load(open(ground_truth_file))

    category_id_to_name = {
        category["id"]: category["name"] for category in ground_truth[categories]
    }

    image_id_to_file_name = {
        image["id"]: image["file_name"] for image in ground_truth["images"]
    }

    image_id_to_annotations = defaultdict(list)
    for annotation in ground_truth["annotations"]:
        image_id_to_annotations[annotation["image_id"]].append(annotation)

    for image_id, annotation in image_id_to_annotations.items():
        file_name = image_id_to_file_name[image_id]

        logger.info(f"Loading {Path(ground_truth_image_folder) / file_name}")
        try:
            image_pil = Image.open(Path(ground_truth_image_folder) / file_name)
        except FileNotFoundError:
            logger.warning(f"{file_name} not found")
            continue

        width, height = image_pil.size
        fig = px.imshow(image_pil)
        fig.update_layout(title=Path(file_name).name)

        for annotation in image_id_to_annotations[image_id]:
            polygons = annotation_to_polygons(annotation, width, height)
            color = px.colors.qualitative.Light24[annotation[category_id] % 24]

            for polygon in polygons:
                fig.add_trace(
                    go.Scatter(
                        x=polygon[:, 0],
                        y=polygon[:, 1],
                        fill="toself",
                        name=category_id_to_name[annotation[category_id]],
                        fillcolor=color,
                        line_color=color,
                        opacity=opacity,
                    )
                )

        output_name = Path(output_folder) / Path(file_name).name
        logger.info(f"Saving to {output_name}")
        fig.write_html(str(output_name.with_suffix(".html")))