Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 694
Tahseen Rasheed Avatar asked Nov 16 '25 19:11

Tahseen Rasheed


1 Answers

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')
    
like image 145
steinum Avatar answered Nov 19 '25 09:11

steinum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!