Tahseen Rasheed
Tahseen Rasheed

Reputation: 115

How can i get person class and segmentation from MSCOCO dataset?

I want to download only person class and binary segmentation from COCO dataset. How can I do it?

Upvotes: 3

Views: 1254

Answers (1)

steinum
steinum

Reputation: 86

use pycocotools .

  • import library
    from pycocotools.coco import COCO
    
  • load json file of coco annotation
    coco = COCO('/home/office/cocoDataset/annotations/instances_train2017.json')
    
  • get category IDs of coco dataset
    category_ids = coco.getCatIds(catNms=['person'])
    
  • get annotations of a single image
    annotations = coco.getAnnIds(imgIds=image_id, catIds=category_ids, iscrowd=False)
    
  • here each person has different annotation, and i'th person's annotation is annotation[i] hence merge all the annotations and save it
    if annotations:
      mask = coco.annToMask(annotations[0])
      for i in range(len(annotations)):
        mask |= coco.annToMask(annotations[i])
      mask = mask * 255
      im = Image.fromarray(mask)
      im.save('~/mask_name.png')
    

Upvotes: 5

Related Questions