Skip to content

Commit

Permalink
format 3
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiev committed Jul 18, 2024
1 parent 508b9f2 commit 9e7a8a9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
22 changes: 14 additions & 8 deletions sahi/utils/shapely.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
"""
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
7 changes: 1 addition & 6 deletions tests/test_shapelyutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 9e7a8a9

Please sign in to comment.