diff --git a/sahi/utils/shapely.py b/sahi/utils/shapely.py index 6a9007fa..61083f87 100644 --- a/sahi/utils/shapely.py +++ b/sahi/utils/shapely.py @@ -20,9 +20,6 @@ def get_shapely_box(x: int, y: int, width: int, height: int) -> Polygon: return shapely_box - - - def get_shapely_multipolygon(coco_segmentation: List[List]) -> MultiPolygon: """ Accepts coco style polygon coords and converts it to valid shapely multipolygon object @@ -32,7 +29,7 @@ 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 + 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. """ @@ -100,7 +97,9 @@ def from_coco_bbox(cls, bbox: List[int], slice_bbox: List[int] = None): slice_bbox (List[int]): [x_min, y_min, x_max, y_max] Is used to calculate sliced coco coordinates. """ - shapely_polygon = get_shapely_box(x=bbox[0], y=bbox[1], width=bbox[2], height=bbox[3]) + shapely_polygon = get_shapely_box( + x=bbox[0], y=bbox[1], width=bbox[2], height=bbox[3] + ) shapely_multipolygon = MultiPolygon([shapely_polygon]) return cls(multipolygon=shapely_multipolygon, slice_bbox=slice_bbox) @@ -184,7 +183,9 @@ def to_coco_segmentation(self): if coco_polygon[:2] == coco_polygon[-2:]: del coco_polygon[-2:] # append coco_polygon to coco_segmentation - coco_polygon = [point for point in coco_polygon] if coco_polygon else coco_polygon + coco_polygon = ( + [point for point in coco_polygon] if coco_polygon else coco_polygon + ) coco_segmentation.append(coco_polygon) return coco_segmentation @@ -207,7 +208,10 @@ def to_opencv_contours(self): miny = self.slice_bbox[1] x_coords = [x_coord - minx for x_coord in x_coords] y_coords = [y_coord - miny for y_coord in y_coords] - opencv_contour = [[[int(x_coords[ind]), int(y_coords[ind])]] for ind in range(len(x_coords))] + opencv_contour = [ + [[int(x_coords[ind]), int(y_coords[ind])]] + for ind in range(len(x_coords)) + ] else: opencv_contour: List = [] # append opencv_contour to opencv_contours @@ -321,6 +325,8 @@ def get_intersection(self, polygon: Polygon): else: intersection_multipolygon = MultiPolygon([]) # create shapely annotation from intersection multipolygon - intersection_shapely_annotation = ShapelyAnnotation(intersection_multipolygon, slice_bbox) + intersection_shapely_annotation = ShapelyAnnotation( + intersection_multipolygon, slice_bbox + ) return intersection_shapely_annotation diff --git a/tests/test_shapelyutils.py b/tests/test_shapelyutils.py index 72627119..11ab53f9 100644 --- a/tests/test_shapelyutils.py +++ b/tests/test_shapelyutils.py @@ -3,12 +3,7 @@ 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):