COCO2017数据集api说明

什么是COCO?
COCO数据集是Microsoft制作收集用于Detection + Segmentation + Localization + Captioning的数据集,本人收集了其2017年的版本,一共有25G左右的图片和600M左右的标签文件。 
COCO数据集共有小类80个,分别为

[‘person’, ‘bicycle’, ‘car’, ‘motorcycle’, ‘airplane’, ‘bus’, ‘train’, ‘truck’, ‘boat’, ‘traffic light’, ‘fire hydrant’, ‘stop sign’, ‘parking meter’, ‘bench’, ‘bird’, ‘cat’, ‘dog’, ‘horse’, ‘sheep’, ‘cow’, ‘elephant’, ‘bear’, ‘zebra’, ‘giraffe’, ‘backpack’, ‘umbrella’, ‘handbag’, ‘tie’, ‘suitcase’, ‘frisbee’, ‘skis’, ‘snowboard’, ‘sports ball’, ‘kite’, ‘baseball bat’, ‘baseball glove’, ‘skateboard’, ‘surfboard’, ‘tennis racket’, ‘bottle’, ‘wine glass’, ‘cup’, ‘fork’, ‘knife’, ‘spoon’, ‘bowl’, ‘banana’, ‘apple’, ‘sandwich’, ‘orange’, ‘broccoli’, ‘carrot’, ‘hot dog’, ‘pizza’, ‘donut’, ‘cake’, ‘chair’, ‘couch’, ‘potted plant’, ‘bed’, ‘dining table’, ‘toilet’, ‘tv’, ‘laptop’, ‘mouse’, ‘remote’, ‘keyboard’, ‘cell phone’, ‘microwave’, ‘oven’, ‘toaster’, ‘sink’, ‘refrigerator’, ‘book’, ‘clock’, ‘vase’, ‘scissors’, ‘teddy bear’, ‘hair drier’, ‘toothbrush’]

大类12个,分别为

[‘appliance’, ‘food’, ‘indoor’, ‘accessory’, ‘electronic’, ‘furniture’, ‘vehicle’, ‘sports’, ‘animal’, ‘kitchen’, ‘person’, ‘outdoor’]

安装COCO api
COCO api来自于github, 从github上clone即可, https://github.com/pdollar/coco 
clone下来后在命令行中把路径切换到该路径,输入

python setup.py install
1

我认为自述文件中有一个误导性的错字:

对于Python,在coco / PythonAPI下运行“make”...

它应该是:

对于Python,在coco / cocoapi / PythonAPI下运行“make”...

我安装了“pip install Cython”,但我还是得到了

clang:错误:没有这样的文件或目录:'pycocotools / _mask.c'clang 
:错误:没有输入文件
错误:命令'cc'失败,退出状态为1

当我跑步时。

我还缺少一个中间步骤吗?
我已经在PythonApi目录中了。

编辑:解决了。我有。Cython for Python3,为Python 2安装它就可以了。

对我来说使用python3 
pip install cython 
然后'make'在coco / cocoapi / PythonAPI 
后跟sudo python setup.py install解决了这个问题

我有同样的问题,但为python3安装了cython。我在Makefile中更改了两行来安装非默认的python intrepreter:

all:
    # install pycocotools locally
        python3.6 setup.py build_ext --inplace
        rm -rf build

install:
        # install pycocotools to the Python site-packages
        python3.6 setup.py build_ext install
        rm -rf build


即可,如果遇到错误,参考这两篇博主写的博客即可 
https://blog.csdn.net/gxiaoyaya/article/details/78363391 
https://blog.csdn.net/qq_32768743/article/details/80202429 
安装完成后,打开pycharm就会在左侧外部库中看到pycocotools的库

COCO api介绍
使用COCO api,需要首先import COCO的类,然后根据任务实例化

from pycocotools.coco import COCO
coco = COCO(annfile)
1
2
不同任务使用不同的annfile,为下载的数据集中的json文件,如

E:/Dataset/COCO2017/annotations/instances_train2017.json

COCO类中我们主要使用以下几种方法:

getCatIds(catNms=[], supNms=[], catIds=[]) 
通过输入类别的名字、大类的名字或是种类的id,来筛选得到图片所属类别的id 
比如,我们想知道dog类的id是多少

catIds = coco.getCatIds(catNms=['dog'])
1
当然catNms可以包含多个类别名字,如

catIds = coco.getCatIds(catNms=['dog', ‘person', 'bicycle'])
1
getImgIds(imgIds=[], catIds=[]) 
通过图片的id或是所属种类的id得到图片的id 
上一步得到了catIds包含了dog、person、bicycle三个类别的id,我们就可以查询到那些包含有这些类别的图片的id

imgIds = coco.getImgIds(catIds=catIds)
1
最终得到一个长度为112的list,包含了图片的id信息

loadImgs(ids=[]) 
得到图片的id信息后,就可以用loadImgs得到图片的信息了 
在这里我们随机选取之前list中的一张图片

img = coco.loadImgs(imgIds[np.random.randint(0, len(imgIds))])[0]
1
最终得到的img并不是一张numpy格式或是PIL格式的图片,而是一个字典,包含了我们找到的这个id所代表的图片的信息 
 
有了图片的file_name,就可以把这张图片通过matplotlib画出来 
 
可以看到通过查询得到的图片确实包含了dog、person与bicycle

getAnnIds(imgIds=[], catIds=[], areaRng=[], iscrowd=None) 
通过输入图片的id、类别的id、实例的面积、是否是人群来得到图片的注释id 
我们想要在之前的图片中画出对之前给定的三个种类进行实例分割的结果,就需要找到这张图片的注释信息的id

annIds = coco.getAnnIds(imgIds=img['id'], catIds=catIds, iscrowd=None)
1
这里我们得到了一个list

[6971, 192383, 218386, 240733]

loadAnns(ids=[]) 
通过注释的id,得到注释的信息

anns = coco.loadAnns(annIds)
1
得到一个list,包含了4个注释的形状位置信息

showAnns(anns) 
使用标注的信息画出来分割的结果

coco.showAnns(anns)
1


如果我们重新定义一个coco的类,使用的是keypoints的json文件,就可以画出keypoints的图

annFile = '%s/annotations/person_keypoints_%s.json' % (dataDir, dataType)
coco_kps = COCO(annFile)
plt.imshow(I)
plt.axis('off')
annIds = coco_kps.getAnnIds(imgIds=img['id'], catIds=catIds, iscrowd=None)
anns = coco_kps.loadAnns(annIds)
coco_kps.showAnns(anns)
1
2
3
4
5
6
7

--------------------- 
作者:心态已炸_沉迷学习 
来源:CSDN 
原文:https://blog.csdn.net/zym19941119/article/details/80241663 
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自blog.csdn.net/c2a2o2/article/details/85317728
今日推荐