保存coco dataset注释为单一文件,并逐一显示所有图片的mask
大意:
官方的例子只显示 一张图片,我需要逐一显示,并且官方的那个JSON文件太大了,我把注释文件分开存储,每张图片一个注释文件,另行保存在一个叫coco的文件夹中,
#
# windows version cocoapi
# https://github.com/philferriere/cocoapi
#
#
from pycocotools.coco import COCO
import numpy as np
import skimage.io as io
import json
import os
import matplotlib as mpl
mpl.use('TkAgg')
import pylab
import matplotlib.rcsetup as rcsetup
pylab.rcParams['figure.figsize'] = (8.0, 10.0)
#dataDir='..'
#dataType='val2017'
#dataDir='F:/BigData/msCoco2014'
#dataType='val2014'
dataDir='F:/BigData/msCoco2017'
dataType='val2017'
annFile='{}/annotations/instances_{}.json'.format(dataDir,dataType)
# initialize COCO api for instance annotations
coco=COCO(annFile)
# display COCO categories and supercategories
catIds = coco.getCatIds()
cats = coco.loadCats(catIds)
#print the names out
nms=[cat['name'] for cat in cats]
print('COCO categories: \n{}\n'.format(' '.join(nms)))
#print the supercat out
nms = set([cat['supercategory'] for cat in cats])
print('COCO supercategories: \n{}'.format(' '.join(nms)))
# recursively display all images and its masks
imgIds = coco.getImgIds()
for id in imgIds:
annIds = coco.getAnnIds([id], catIds=catIds, iscrowd=None)
anns = coco.loadAnns(annIds)
imgIds = coco.getImgIds(imgIds = [id])
img = coco.loadImgs(imgIds[0])[0]
file_name_ext=img['file_name']
(filename,extension) = os.path.splitext(file_name_ext)
file_path = "coco/" + filename + ".json"
data = {"annotations":anns}
with open(file_path, 'w') as result_file:
json.dump(data, result_file)
I = io.imread('%s/%s/%s'%(dataDir,dataType,img['file_name']))
mpl.pyplot.imshow(I)
mpl.pyplot.axis('off')
coco.showAnns(anns)
顺带再提一下coco数据集中各个参数的解释吧,一般的参数望文即可生义,只需要注意的是iscrowd,这个值为0 即表示polygon,注意,单个的对象(iscrowd=0)可能需要多个polygon来表示,比如某个对象在图像中被挡住了一部分。而iscrowd=1时,segmentation使用的就是RLE格式。
具体样式和解释参数官方文档:https://cocodataset.org/#format-data
如果是自己定义的数据集,采用coco数据格式的话,各个id到底有什么用也是需要注意的地方。
{
"type": "instances",
"images": [
{
"file_name": "0.jpg",
"height": 600,
"width": 800,
"id": 0 ----> image_id
}
],
"categories": [
{
"supercategory": "none", ----> supercategory can be anything
"name": "date",
"id": 0 ----> category_id
},
{
"supercategory": "none",
"name": "hazelnut",
"id": 2
},
{
"supercategory": "none",
"name": "fig",
"id": 1
}
],
"annotations": [
{
"id": 1, ----> annotation id (each annotation has a unique id)
"bbox": [
100,
116,
140,
170
],
"image_id": 0,
"segmentation": [],
"ignore": 0,
"area": 23800,
"iscrowd": 0,
"category_id": 0
},
{
"id": 2,
"bbox": [
321,
320,
142,
102
],
"image_id": 0,
"segmentation": [],
"ignore": 0,
"area": 14484,
"iscrowd": 0,
"category_id": 0
}
]
}