Exemplo n.º 1
0
    def test_get_shapely_box(self):
        x, y, width, height = 1, 1, 256, 256
        shapely_box = get_shapely_box(x, y, width, height)

        self.assertListEqual(shapely_box.exterior.coords.xy[0].tolist(), [257.0, 257.0, 1.0, 1.0, 257.0])
        self.assertEqual(shapely_box.area, 65536)
        self.assertTupleEqual(shapely_box.bounds, (1, 1, 257, 257))
Exemplo n.º 2
0
    def test_get_empty_intersection(self):
        x, y, width, height = 300, 300, 256, 256
        shapely_box = get_shapely_box(x, y, width, height)

        coco_segmentation = [[1, 1, 325, 125, 250, 200, 5, 200]]
        shapely_annotation = ShapelyAnnotation.from_coco_segmentation(coco_segmentation)

        intersection_shapely_annotation = shapely_annotation.get_intersection(shapely_box)

        self.assertEqual(intersection_shapely_annotation.area, 0)

        self.assertEqual(intersection_shapely_annotation.to_coco_bbox(), [])
Exemplo n.º 3
0
def slice_coco_annotations_by_box(coco_annotation_list,
                                  box) -> (List[CocoAnnotation], int):
    """
    Crops all annotations for a given grid. Returns processed annotation list.

    Args:
        coco_annotations: list of CocoAnnotation
            List of CocoAnnotation objects that belong to given COCO image
        box: list
            Points of slice box [x: int, y: int, width: int, height: int]
    Returns:
        sliced_annotations: list of CocoAnnotation
            Sliced list of CocoAnnotation objects that belong to given COCO image
    """

    sliced_annotations = []
    num_invalid_segmentation = 0
    for coco_annotation in coco_annotation_list:
        # calculate intersection polygon btw slice and annotation
        x, y, width, height = box
        shapely_box = get_shapely_box(x, y, width, height)

        shapely_annotation = coco_annotation._shapely_annotation

        invalid_segmentation = False
        if shapely_annotation.multipolygon.is_valid is True:
            intersection_shapely_annotation = shapely_annotation.get_intersection(
                shapely_box)
        else:
            try:
                shapely_annotation = shapely_annotation.get_buffered_shapely_annotation(
                    distance=3)
                intersection_shapely_annotation = shapely_annotation.get_intersection(
                    shapely_box)
            except:
                invalid_segmentation = True
                num_invalid_segmentation += 1

        # store intersection polygon if intersection area is greater than 0
        if (invalid_segmentation is
                False) and (intersection_shapely_annotation.area > 0):
            new_annotation = CocoAnnotation.from_coco_segmentation(
                segmentation=intersection_shapely_annotation.
                to_coco_segmentation(),
                category_id=coco_annotation.category_id,
                category_name=coco_annotation.category_name,
            )

            sliced_annotations.append(new_annotation)

    return sliced_annotations, num_invalid_segmentation
Exemplo n.º 4
0
    def test_get_intersection(self):
        x, y, width, height = 1, 1, 256, 256
        shapely_box = get_shapely_box(x, y, width, height)

        coco_segmentation = [[1, 1, 325, 125, 250, 200, 5, 200]]
        shapely_annotation = ShapelyAnnotation.from_coco_segmentation(
            coco_segmentation)

        intersection_shapely_annotation = shapely_annotation.get_intersection(
            shapely_box)

        test_list = intersection_shapely_annotation.to_list()[0]
        true_list = [(256, 97), (0, 0), (4, 199), (249, 199), (256, 192),
                     (256, 97)]
        for i in range(len(test_list)):
            for j in range(2):
                self.assertEqual(int(test_list[i][j]), int(true_list[i][j]))

        self.assertEqual(
            intersection_shapely_annotation.to_coco_segmentation(),
            [[
                256.0,
                97.97530864197532,
                0.0,
                0.0,
                4.0,
                199.0,
                249.0,
                199.0,
                256.0,
                192.0,
            ]],
        )

        self.assertEqual(intersection_shapely_annotation.to_coco_bbox(),
                         [0, 0, 256, 199])

        self.assertEqual(intersection_shapely_annotation.to_voc_bbox(),
                         [0, 0, 256, 199])
Exemplo n.º 5
0
    def test_shapely_annotation(self):
        # init shapely_annotation from coco segmentation
        segmentation = [[1, 1, 325, 125, 250, 200, 5, 200]]
        shapely_multipolygon = get_shapely_multipolygon(segmentation)
        shapely_annotation = ShapelyAnnotation.from_coco_segmentation(
            segmentation)

        # test conversion methods
        coco_segmentation = shapely_annotation.to_coco_segmentation()
        self.assertEqual(
            coco_segmentation,
            [[1, 1, 325, 125, 250, 200, 5, 200]],
        )
        opencv_contours = shapely_annotation.to_opencv_contours()
        self.assertEqual(
            opencv_contours,
            [[
                [[1, 1]],
                [[325, 125]],
                [[250, 200]],
                [[5, 200]],
                [[1, 1]],
            ]],
        )
        coco_bbox = shapely_annotation.to_coco_bbox()
        self.assertEqual(
            coco_bbox,
            [1, 1, 324, 199],
        )
        voc_bbox = shapely_annotation.to_voc_bbox()
        self.assertEqual(
            voc_bbox,
            [1, 1, 325, 200],
        )

        # test properties
        self.assertEqual(
            shapely_annotation.area,
            shapely_multipolygon.area,
        )
        self.assertEqual(
            shapely_annotation.multipolygon,
            shapely_multipolygon,
        )

        # init shapely_annotation from coco bbox
        coco_bbox = [1, 1, 100, 100]
        shapely_polygon = get_shapely_box(x=coco_bbox[0],
                                          y=coco_bbox[1],
                                          width=coco_bbox[2],
                                          height=coco_bbox[3])
        shapely_annotation = ShapelyAnnotation.from_coco_bbox(coco_bbox)

        # test conversion methods
        coco_segmentation = shapely_annotation.to_coco_segmentation()
        self.assertEqual(
            coco_segmentation,
            [[101, 1, 101, 101, 1, 101, 1, 1]],
        )
        opencv_contours = shapely_annotation.to_opencv_contours()
        self.assertEqual(
            opencv_contours,
            [[
                [[101, 1]],
                [[101, 101]],
                [[1, 101]],
                [[1, 1]],
                [[101, 1]],
            ]],
        )
        coco_bbox = shapely_annotation.to_coco_bbox()
        self.assertEqual(
            coco_bbox,
            [1, 1, 100, 100],
        )
        voc_bbox = shapely_annotation.to_voc_bbox()
        self.assertEqual(
            voc_bbox,
            [1, 1, 101, 101],
        )

        # test properties
        self.assertEqual(
            shapely_annotation.area,
            MultiPolygon([shapely_polygon]).area,
        )
        self.assertEqual(
            shapely_annotation.multipolygon,
            MultiPolygon([shapely_polygon]),
        )