Skip to content

Commit

Permalink
formatting again...
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiev committed Jul 18, 2024
1 parent 9b0205b commit 508b9f2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
3 changes: 2 additions & 1 deletion sahi/utils/shapely.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ def filter_polygons(geometry):
"""
Filters out and returns only Polygon or MultiPolygon components of a geometry.
If geometry is a Polygon, it converts it into a MultiPolygon.
If it's a GeometryCollection, it filters to create a MultiPolygon from any Polygons in the collection.
If it's a GeometryCollection, it filters
to create a MultiPolygon from any Polygons in the collection.
Returns an empty MultiPolygon if no Polygon or MultiPolygon components are found.
"""
if isinstance(geometry, Polygon):
Expand Down
25 changes: 18 additions & 7 deletions tests/test_shapelyutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@

import unittest

from sahi.utils.shapely import MultiPolygon, ShapelyAnnotation, get_shapely_box, get_shapely_multipolygon
from sahi.utils.shapely import (
MultiPolygon,
ShapelyAnnotation,
get_shapely_box,
get_shapely_multipolygon,
)


class TestShapelyUtils(unittest.TestCase):
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.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))

Expand All @@ -26,7 +33,6 @@ def test_get_shapely_multipolygon(self):
self.assertEqual(shapely_multipolygon.area, 41177.5)
self.assertTupleEqual(shapely_multipolygon.bounds, (1, 1, 325, 200))


def test_get_shapely_multipolygon_naughty(self):
# self-intersection case
coco_segmentation = [
Expand All @@ -35,7 +41,6 @@ def test_get_shapely_multipolygon_naughty(self):
shapely_multipolygon = get_shapely_multipolygon(coco_segmentation)
self.assertTrue(shapely_multipolygon.is_valid)


def test_shapely_annotation(self):
# init shapely_annotation from coco segmentation
segmentation = [[1, 1, 325, 125.2, 250, 200, 5, 200]]
Expand Down Expand Up @@ -84,7 +89,9 @@ def test_shapely_annotation(self):

# 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_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
Expand Down Expand Up @@ -134,7 +141,9 @@ def test_get_intersection(self):
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)
intersection_shapely_annotation = shapely_annotation.get_intersection(
shapely_box
)

test_list = intersection_shapely_annotation.to_list()[0]
true_list = [(0, 0), (4, 199), (249, 199), (256, 192), (256, 97), (0, 0)]
Expand Down Expand Up @@ -171,7 +180,9 @@ def test_get_empty_intersection(self):
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)
intersection_shapely_annotation = shapely_annotation.get_intersection(
shapely_box
)

self.assertEqual(intersection_shapely_annotation.area, 0)

Expand Down

0 comments on commit 508b9f2

Please sign in to comment.