如何为自定义数据集过滤COCO数据集类和注释?

时间:2020-02-14 14:02:52

标签: python computer-vision dataset

我能够使用下面的代码通过COCO API过滤图像,我针对所需的所有类多次执行了此代码,这是类别person的示例,我为{{ 1}}等。

我现在想做的是过滤数据集car的注释,并将其保存在json (instances_train2017.json)中。

instances_train2017.json

我尝试使用COCO API,但我没有给我想要的COCO格式,例如intances_train2017.json)

# Load categories with the specified ids, in this case all
cats = coco.loadCats(coco.getCatIds())
nms = [cat['name'] for cat in cats]
print('COCO categories: \n{}\n'.format(' '.join(nms)))

# Get all images containing given categories
catIds = coco.getCatIds(catNms=['person'])
imgIds = coco.getImgIds(catIds=catIds)
images = coco.loadImgs(imgIds)
print("imgIds: ", len(imgIds))
#print("images: ", images)

# download images for specific category
for im in images:
    print("im: ", im)
    img_data = requests.get(im['coco_url']).content
    with open('customCoco/images/train2017/' + im['file_name'], 'wb') as handler:
        handler.write(img_data)

我发现了这篇文章:https://github.com/cocodataset/cocoapi/issues/271,但它以csv格式保存,我不想要,我想要相同的文件,只是过滤了。

0 个答案:

没有答案