chrise96 / image-to-coco-json-converter

Convert segmentation RGB mask images to COCO JSON format
199 stars 57 forks source link

'MultiPolygon' object has no attribute 'exterior' #6

Closed getcontrol closed 3 years ago

getcontrol commented 3 years ago

Get this error when trying to convert these types of masks to json . I assume its because of the weird polygons:

mask_sample

Similar issue reported here https://github.com/akTwelve/cocosynth/issues/9 and maybe fixed here https://github.com/akTwelve/cocosynth/blob/3909837290ab3511ff03ffe57ae870c929bd40a0/python/coco_json_utils.py

chrise96 commented 3 years ago

Some objects in this image are indeed multipolygons because they include holes. Could you define the class ids that may be multipolygons in the "multipolygons_id" variable in the main notebook? And set also the black background, the outlier, to a multipolygon.

getcontrol commented 3 years ago

I figured it out: My masks weren't one uniform RGB color but instead a range of light grey & dark grey colors. I have it working on a dataset where masks RGB values are uniform

alancarlosml commented 3 years ago

Hi, I am getting the same error.

The error shows up in the following type of image:

cju2mh8t6p07008350e01tx2a_0_3446

I've already tried to binarized it, but the error persists.

Could you please help me?

enrj97 commented 3 years ago

Hi everyone! Same error here for the following mask.

48 20042200102788_15 615692138671875

For me, the following code in src.create_annotation.create_sub_mask_annotation solves the problem:

if (poly.geom_type == 'MultiPolygon'):
            # if MultiPolygon, take the smallest convex Polygon containing all the points in the object
            poly = poly.convex_hull

if (poly.geom_type == 'Polygon'): # Ignore if still not a Polygon (could be a line or point)
            polygons.append(poly)
            segmentation = np.array(poly.exterior.coords).ravel().tolist()
            segmentations.append(segmentation)
alancarlosml commented 3 years ago

@enrj97 I've tried your code but I'm getting this error:

Traceback (most recent call last):
  File "/home/alan/mask2json/main.py", line 82, in <module>
    coco_format["images"], coco_format["annotations"], annotation_cnt = images_annotations_info(mask_path)
  File "/home/alan/mask2json/main.py", line 46, in images_annotations_info
    polygons, segmentations = create_sub_mask_annotation(sub_mask)
TypeError: cannot unpack non-iterable NoneType object
enrj97 commented 3 years ago

@alancarlosml check this and try to cut off the small polygons.

https://github.com/akTwelve/cocosynth/blob/3909837290ab3511ff03ffe57ae870c929bd40a0/python/coco_json_utils.py

sivamani-k commented 1 year ago

Hi everyone! Same error here for the following mask.

48 20042200102788_15 615692138671875

For me, the following code in src.create_annotation.create_sub_mask_annotation solves the problem:

if (poly.geom_type == 'MultiPolygon'):
            # if MultiPolygon, take the smallest convex Polygon containing all the points in the object
            poly = poly.convex_hull

if (poly.geom_type == 'Polygon'): # Ignore if still not a Polygon (could be a line or point)
            polygons.append(poly)
            segmentation = np.array(poly.exterior.coords).ravel().tolist()
            segmentations.append(segmentation)

Hi everyone! Same error here for the following mask.

48 20042200102788_15 615692138671875

For me, the following code in src.create_annotation.create_sub_mask_annotation solves the problem:

if (poly.geom_type == 'MultiPolygon'):
            # if MultiPolygon, take the smallest convex Polygon containing all the points in the object
            poly = poly.convex_hull

if (poly.geom_type == 'Polygon'): # Ignore if still not a Polygon (could be a line or point)
            polygons.append(poly)
            segmentation = np.array(poly.exterior.coords).ravel().tolist()
            segmentations.append(segmentation)

This solution works in my case. Thank you so much for your solution.