I want to download only person class and binary segmentation from COCO dataset. How can I do it?
Asked
Active
Viewed 710 times
1 Answers
5
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 itif 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')

steinum
- 76
- 3