How to create mask images from COCO dataset?

0

Issue

So I have been using this code,. I am trying to generate the raw mask of the images from COCO dataset.

dataDir='G:'
dataType='train2014'
annFile='{}/annotations/instances_{}.json'.format(dataDir,dataType)


coco=COCO(annFile)
annFile = '{}/annotations/person_keypoints_{}.json'.format(dataDir,dataType)
coco_kps=COCO(annFile)


catIds = coco.getCatIds(catNms=['person'])
imgIds = coco.getImgIds(catIds=catIds );
imgIds = coco.getImgIds(imgIds = imgIds[0])
img = coco.loadImgs(imgIds[np.random.randint(0,len(imgIds))])[0]
I = io.imread('G:/train2014/'+img['file_name'])

plt.imshow(I); plt.axis('off')
annIds = coco.getAnnIds(imgIds=img['id'], catIds=catIds, iscrowd=None)
anns = coco.loadAnns(annIds)
coco.showAnns(anns)

But what i get is some thing like this

enter image description here

But what I want is something like this

enter image description here

How can I get the raw mask against each image ?

Solution

I’m late to the party, but if this can help someone.
I don’t know if your code worked for your application, however, if you want each pixel of the mask to have the value of the annotation category id, then you can’t just add the masks, as some will overlapp. I used a numpy maximum for that :

cat_ids = coco.getCatIds()
anns_ids = coco.getAnnIds(imgIds=img['id'], catIds=cat_ids, iscrowd=None)
anns = coco.loadAnns(anns_ids)
anns_img = np.zeros((img['height'],img['width']))
for ann in anns:
    anns_img = np.maximum(anns_img,coco.annToMask(ann)*ann['category_id'])

EDIT :
Here is an example of my code on image 47112 of the 2017 dataset :
Coco2017-47112
With the code above
The value of the shade of grey is the id of the category as described in the dataset description.
Note that here the pizza overlaps with the table at the edges of its polygon. If we add the masks, the overlap would be given an id corresponding to the sum of the classes of pizza and table. However, using max, only one of the class is kept. In this case, as the class table has a id greater than the id of class pizza, the overlap is affected the class table even if the pizza is visualy above. I am not sure this could be fixed easily though.

Answered By – Duret-Robert Louis

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More