Repository: roboflow/supervision
File: supervision/dataset/formats/pascal_voc.py (Python)

Code before:
        y2 = int(bbox.find("ymax").text)

        xyxy.append([x1, y1, x2, y2])

        with_masks = obj.find("polygon") is not None
        with_masks = force_masks if force_masks else with_masks

        for polygon in obj.findall("polygon"):
            polygon_points = parse_polygon_points(polygon)

            mask_from_polygon = polygon_to_mask(
                polygon=np.array(polygon_points),
                resolution_wh=resolution_wh,
            )
            masks.append(mask_from_polygon)

    xyxy = np.array(xyxy) if len(xyxy) > 0 else np.empty((0, 4))
    for k in set(class_names):
        if k not in extended_classes:
            extended_classes.append(k)
    class_id = np.array(
        [extended_classes.index(class_name) for class_name in class_names]
    )

    if with_masks:
        annotation = Detections(xyxy=xyxy, mask=np.array(masks), class_id=class_id)
    else:
        annotation = Detections(xyxy=xyxy, class_id=class_id)
    return annotation, extended_classes


def parse_polygon_points(polygon: Element) -> List[List[int]]:
    # Parses polygon points in format: <polygon><x1>...</x1><y1>...</y1><x2>...</x2><y2>...</y2>...</polygon>
    polygon_points = []
    coords = polygon.findall(".//*")
    for i in range(0, len(coords), 2):
        x = int(coords[i].text)
        y = int(coords[i + 1].text)
        polygon_points.append([x, y])
    return polygon_points


def load_pascal_voc_annotations_v1(
    annotation_path: str,
) -> Tuple[str, Detections, List[str]]:
    """
    Loads PASCAL VOC XML annotations and returns the image name, a Detections instance, and a list of class names.

    Args:
        annotation_path (str): The path to the PASCAL VOC XML annotations file.


Review comment (suggestion):
Let's pass `dtype` argument to `mask=np.array(masks)`. We want it to be `bool`, but it is `uint8`.

Code after:
        y2 = int(bbox.find("ymax").text)

        xyxy.append([x1, y1, x2, y2])

        with_masks = obj.find("polygon") is not None
        with_masks = force_masks if force_masks else with_masks

        for polygon in obj.findall("polygon"):
            polygon_points = parse_polygon_points(polygon)

            mask_from_polygon = polygon_to_mask(
                polygon=np.array(polygon_points),
                resolution_wh=resolution_wh,
            )
            masks.append(mask_from_polygon)

    xyxy = np.array(xyxy) if len(xyxy) > 0 else np.empty((0, 4))
    for k in set(class_names):
        if k not in extended_classes:
            extended_classes.append(k)
    class_id = np.array(
        [extended_classes.index(class_name) for class_name in class_names]
    )

    if with_masks:
        annotation = Detections(
            xyxy=xyxy, mask=np.array(masks).astype(bool), class_id=class_id
        )
    else:
        annotation = Detections(xyxy=xyxy, class_id=class_id)
    return annotation, extended_classes


def parse_polygon_points(polygon: Element) -> List[List[int]]:
    polygon_points = []
    coords = polygon.findall(".//*")
    for i in range(0, len(coords), 2):
        x = int(coords[i].text)
        y = int(coords[i + 1].text)
        polygon_points.append([x, y])
    return polygon_points