Skip to content

Commit dfda89c

Browse files
committed
removal of rasterio
1 parent 8567e0c commit dfda89c

File tree

1 file changed

+24
-9
lines changed
  • labelbox/data/annotation_types/geometry

1 file changed

+24
-9
lines changed

labelbox/data/annotation_types/geometry/mask.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
from typing import Callable, Optional, Tuple, Union
1+
from typing import Callable, Optional, Tuple, Union, List
22

33
import numpy as np
44
from pydantic.class_validators import validator
5-
from rasterio.features import shapes
6-
from shapely.geometry import MultiPolygon, shape
5+
from shapely.geometry import MultiPolygon, Polygon
76
import cv2
87

98
from ..data import MaskData
@@ -39,12 +38,23 @@ class Mask(Geometry):
3938
@property
4039
def geometry(self):
4140
mask = self.draw(color=1)
42-
polygons = (
43-
shape(shp)
44-
for shp, val in shapes(mask, mask=None)
45-
# ignore if shape is area of smaller than 1 pixel
46-
if val >= 1)
47-
return MultiPolygon(polygons).__geo_interface__
41+
contours, hierarchy = cv2.findContours(image=mask,
42+
mode=cv2.RETR_TREE,
43+
method=cv2.CHAIN_APPROX_NONE)
44+
45+
holes = []
46+
external_contours = []
47+
for i in range(len(contours)):
48+
if hierarchy[0, i, 3] != -1:
49+
#determined to be a hole based on contour hierarchy
50+
holes.append(contours[i])
51+
else:
52+
external_contours.append(contours[i])
53+
54+
external_polygons = self._extract_polygons_from_contours(
55+
external_contours)
56+
holes = self._extract_polygons_from_contours(holes)
57+
return external_polygons.difference(holes).__geo_interface__
4858

4959
def draw(self,
5060
height: Optional[int] = None,
@@ -86,6 +96,11 @@ def draw(self,
8696
canvas[mask.astype(np.bool)] = color
8797
return canvas
8898

99+
def _extract_polygons_from_contours(self, contours: List) -> MultiPolygon:
100+
contours = map(np.squeeze, contours)
101+
polygons = map(Polygon, contours)
102+
return MultiPolygon(polygons)
103+
89104
def create_url(self, signer: Callable[[bytes], str]) -> str:
90105
"""
91106
Update the segmentation mask to have a url.

0 commit comments

Comments
 (0)