cocodataset / cocoapi

COCO API - Dataset @ http://cocodataset.org/
Other
6.07k stars 3.75k forks source link

How to convert Polygon to binary mask dynamically. #229

Open gr8Adakron opened 5 years ago

gr8Adakron commented 5 years ago

This is my input JSON RLE:

segment_data_set =  {
    "info": {
        "year": 2018,
        "version": "N.A.",
        "description": "fashion-pixel-annotation",
        "contributor": "zenithzimmer@gmail.com",
        "url": "labelbox.com",
        "date_created": "2018-11-13T08:29:02.231985+00:00"
    },
    "images": [{
        "id": "cjofg72cyzqla0924wfnnu9o3",
        "file_name": "https://firebasestorage.googleapis.com/v0/b/labelbox-193903.appspot.com/o/cjo9kxq031qji0b99ouf4s3jx%2Fe7d6d40c-e4db-42a0-96b5-a554b58e17e7%2FdzlMl983ml7qbko8Ft3u.jpg?alt=media&token=d4c4925a-91ea-49c4-bd52-ddf773652154",
        "license": "N.A.",
        "flickr_url": "https://firebasestorage.googleapis.com/v0/b/labelbox-193903.appspot.com/o/cjo9kxq031qji0b99ouf4s3jx%2Fe7d6d40c-e4db-42a0-96b5-a554b58e17e7%2FdzlMl983ml7qbko8Ft3u.jpg?alt=media&token=d4c4925a-91ea-49c4-bd52-ddf773652154",
        "coco_url": "https://firebasestorage.googleapis.com/v0/b/labelbox-193903.appspot.com/o/cjo9kxq031qji0b99ouf4s3jx%2Fe7d6d40c-e4db-42a0-96b5-a554b58e17e7%2FdzlMl983ml7qbko8Ft3u.jpg?alt=media&token=d4c4925a-91ea-49c4-bd52-ddf773652154",
        "date_captured": "N.A.",
        "width": 500,
        "height": 500
    }],
    "annotations": [{
        "id": 1,
        "image_id": "cjofg72cyzqla0924wfnnu9o3",
        "category_id": 1,
        "segmentation": [
            [183.0, 89.0, 159.0, 119.0, 151.0, 150.0, 145.0, 178.0, 153.0, 207.0, 154.0, 237.0, 144.0, 267.0, 143.0, 304.0, 138.0, 336.0, 134.0, 396.0, 143.0, 437.0, 181.0, 463.0, 210.0, 461.0, 268.0, 473.0, 306.0, 467.0, 347.0, 453.0, 369.0, 422.0, 360.0, 373.0, 351.0, 209.0, 350.0, 133.0, 320.0, 92.0, 256.0, 190.0, 254.0, 196.0, 183.0, 89.0]
        ],
        "area": 69744.0,
        "bbox": [134.0, 89.0, 235.0, 384.0],
        "iscrowd": 0
    }, {
        "id": 2,
        "image_id": "cjofg72cyzqla0924wfnnu9o3",
        "category_id": 1,
        "segmentation": [
            [340.0, 111.0, 346.0, 85.0, 348.0, 56.0, 341.0, 48.0, 327.0, 63.0, 318.0, 81.0, 322.0, 88.0, 327.0, 72.0, 336.0, 54.0, 341.0, 60.0, 341.0, 73.0, 340.0, 84.0, 336.0, 99.0, 340.0, 111.0]
        ],
        "area": 505.5,
        "bbox": [318.0, 48.0, 30.0, 63.0],
        "iscrowd": 0
    }],
    "licenses": [],
    "categories": [{
        "supercategory": "upper-body-fashion",
        "id": 1,
        "name": "upper-body-fashion"
    }]
    }

I have used the demo python notebook to convert the RLE JSON into the proper image.

This is the code using which I am able to convert the RLE JSON to an annotated image:

# load and display instance annotations
plt.imshow(I); plt.axis('on')
annIds = coco.getAnnIds(imgIds=[img['id']], catIds=catIds, iscrowd=None)
anns = coco.loadAnns(annIds)
coco.showAnns(anns)

This is the resulted annotated images:

download

But I want to use this RLE JSON to create a Binary Masked image from this which should only 2-pixel of mask and unmask.

Any help? how to do that? I can see this method in the API, but dont know how to use it as it keep giving me error.

coco.annToMask(anns)

Error:


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-26-149ebe9d557c> in <module>()
----> 1 coco.annToMask(anns)

~/cocoapi/PythonAPI/pycocotools/coco.py in annToMask(self, ann)
    429         Convert annotation which can be polygons, uncompressed RLE, or RLE to binary mask.
    430         :return: binary mask (numpy 2D array)
--> 431         """
    432         rle = self.annToRLE(ann)
    433         m = maskUtils.decode(rle)

~/cocoapi/PythonAPI/pycocotools/coco.py in annToRLE(self, ann)
    408         Convert annotation which can be polygons, uncompressed RLE to RLE.
    409         :return: binary mask (numpy 2D array)
--> 410         """
    411         t = self.imgs[ann['image_id']]
    412         h, w = t['height'], t['width']

TypeError: list indices must be integers or slices, not str
gr8Adakron commented 5 years ago

@rodrigob @pdollar @rbgirshick @rodrigob any help would be appreciated.

jsbroks commented 5 years ago
  1. Image Ids need to be integers
  2. annToMask accepts a single annotation, not an array
annids = coco.getAnnIds()
anns = coco.loadAnns(annids)
mask = coco.annToMask(anns[0])
lolitsgab commented 5 years ago

If you don't mind me asking, what dataset are you using @gr8Adakron ?

daniel-redder commented 2 years ago

I have a similar question. I have gotten past this particular problem by casting my image ids to integers. The annToRLE function runs, but then I get the same error on mask.decode which is a .pyc so I cannot really manually debug it... image notably the RLE file passed to decode only has two inputs a "size" list [1000,1000] and a binary string "counts"