http://cocodataset.org/#download 官网地址
本文的目的是获取所有图像的分割结果并保存的工作。
Mask API 中介绍
COCO为每个目标实例都提供了分割Msak,instance_train201X.json表示的是整个数据集的结构,下面这部分主要介绍他有哪些数据以及其数据类型
{
"info" : info,
"images" : [image],
"annotations" : [annotation],
"licenses" : [license],
}
info{
"year" : int,
"version" : str,
"description" : str,
"contributor" : str,
"url" : str,
"date_created" : datetime,
}
image{
"id" : int,
"width" : int,
"height" : int,
"file_name" : str,
"license" : int,
"flickr_url" : str,
"coco_url" : str,
"date_captured" : datetime,
}
license{
"id" : int,
"name" : str,
"url" : str,
}
刚介绍完他的数据类型,现介绍下具体到json文件中的每一部分的具体形式
解析下instance_train2014.json文件,最后我们讲一下如何获取并展示分割结果。
{
"info": #第一个info信息
{ #数据集信息
"description": "COCO 2014 Dataset",
"url": "http://cocodataset.org",
"version": "1.0",
"year": 2014,
"contributor": "COCO Consortium",
"date_created": "2017/09/01"
},
"images": #第二个图片信息,数组包含了多张图像
[ { #每张图像的具体信息
"license": 5,
"file_name": "COCO_train2014_000000057870.jpg",
"coco_url": "http://images.cocodataset.org/train2014/COCO_train2014_000000057870.jpg",
"height": 480,
"width": 640,
"date_captured": "2013-11-14 16:28:13",
"flickr_url": "http://farm4.staticflickr.com/3153/2970773875_164f0c0b83_z.jpg",
"id": 57870
},
......
...... #此处省略很多图片
{
"license": 4,
"file_name": "COCO_train2014_000000475546.jpg",
"http://images.cocodataset.org/train2014/COCO_train2014_000000475546.jpg",
"height": 375,
"width":500,;、
"date_captured": "2013-11-25 21:20:23",
"flickr_url": "http://farm1.staticflickr.com/167/423175046_6cd9d0205a_z.jpg",
"id": 475546
}], #图像描述结束,下面开始介绍licenses
"licenses":
[ {
"url": "http://creativecommons.org/licenses/by-nc-sa/2.0/",
"id": 1,
"name": "Attribution-NonCommercial-ShareAlike License"
},
.....#此处省略七个license
.....
{
"url": "http://creativecommons.org/licenses/by-nc-nd/2.0/",
"id": 8,
"name": "Attribution-NonCommercial-NoDerivs License"
}],
"annotations":
[ {
#如果你想了解这个annotations中segment里面是什么,首先它是通过压缩处理后的分割区域的一个表示,TFRecord
"segmentation":[[312.29,562.89,402.25,511.49,400.96,425.38,398.39,372.69,
388.11,332.85,318.71,325.14,295.58,305.86,269.88,314.86,
258.31,337.99,217.19,321.29,182.49,343.13,141.37,348.27,
132.37,358.55,159.36,377.83,116.95,421.53,167.07,499.92,
232.61,560.32,300.72,571.89]],
"area": 54652.9556,
"iscrowd": 0,
"image_id": 480023,
"bbox": [116.95,305.86,285.3,266.03],
"category_id": 58,"id": 86
},
.....#此处省略很多图像的分割标签
.....
"segmentation":[[312.29,562.89,402.25,511.49,400.96,425.38,398.39,372.69,
388.11,332.85,318.71,325.14,295.58,305.86,269.88,314.86,
258.31,337.99,217.19,321.29,182.49,343.13,141.37,348.27,
132.37,358.55,159.36,377.83,116.95,421.53,167.07,499.92,
232.61,560.32,300.72,571.89]],
"area": 54652.9556,
"iscrowd": 0,
"image_id": 480023,
"bbox": [116.95,305.86,285.3,266.03],
"category_id": 58,
"id": 86
},
"categories":#类别信息
[ {
"supercategory": "person",
"id": 1,
"name": "person"
},
.......#此处省略很多图像的类标签
.......
{
"supercategory": "vehicle",
"id": 2,
"name": "bicycle"
},
{
"supercategory": "kitchen",#大类
"id": 50,
"name": "spoon"
}
首先,下载COCOAPI中的pythonAPI到coco文件夹中
git clone https://github.com/cocodataset/cocoapi.git
然后,cd到pythonApi下,执行make,可能会出现下面情况
cd coco/PythonAPI
make
#错误提示pycocotools/_mask.c:没有那个文件或目录
pip install cython #解决方式
make#再次执行make,如果你没有出现上面的错误,可以跳过
#接下来,验证cocoApi是否安装成功
python
>>>import pycocotools
#不报错就成功了一半了
接下来,在coco文件夹,下载好image和anotation。
这里介绍我们的分割结果图获取方法
官方给的使用示例在下载目录下的pycocoDemo.ipynb文件下https://github.com/dengdan/coco/blob/master/PythonAPI/pycocoDemo.ipynb
那么我们首先了解一下,之前为什么要引入pycocotools,因为这个问件下包含了对coco数据的json文件的解析工具,他定义了coco.py这个文件,中包含一下几个接口。
# decodeMask - Decode binary mask M encoded via run-length encoding.
# encodeMask - Encode binary mask M using run-length encoding.
# getAnnIds - Get ann ids that satisfy given filter conditions.
# getCatIds - Get cat ids that satisfy given filter conditions.
# getImgIds - Get img ids that satisfy given filter conditions.
# loadAnns - Load anns with the specified ids.
# loadCats - Load cats with the specified ids.
# loadImgs - Load imgs with the specified ids.
# annToMask - Convert segmentation in an annotation to binary mask.
# showAnns - Display the specified annotations.
# loadRes - Load algorithm results and create API for accessing them.
# download - Download COCO images from mscoco.org server.
# Throughout the API "ann"=annotation, "cat"=category, and "img"=image.
------ 首先,我们获取COCO数据集中共有多少类,需要在pythonAPI下新建一个python文件,命名为segcoco.py用于获取分割图,先执行下面这段话,获取下COCO中共有多少类别
import numpy as np
import skimage as io
import matplotlib as mpl
mpl.use('Agg')
#这里为了防止linux没有GUI报错
import matplotlib.pyplot as plt
import pylab
import urllib
import numpy as np
from io import BytesIO
import requests as req
from PIL import Image
pylab.rcParams['figure.figsize'] = (8.0, 10.0)
dataDir='..'
dataType='val2014' #这里改为train2017的话,类别是相同的
annFile='{}/annotations/instances_{}.json'.format(dataDir,dataType)
coco=COCO(annFile)
cats = coco.loadCats(coco.getCatIds())
#这里loadCats就是coco提供的接口,获取类别
nms=[cat['name'] for cat in cats]
print('COCO categories: \n{}\n'.format(' '.join(nms)))
nms = set([cat['supercategory'] for cat in cats])
#cat['supercategory'],从这里可以看出来,cat是一个包含多个属性的数组\说是字典更好
print('COCO supercategories: \n{}'.format(' '.join(nms)))
显示
loading annotations into memory...
Done (t=5.08s)
creating index...
index created!
COCO categories:
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
COCO supercategories:
outdoor food indoor appliance sports person animal vehicle furniture accessory electronic kitchen
接下来,获取分割图,按照github的示例,我们在之前代码的基础上,添加一下代码
imgIds = coco.getImgIds(imgIds=[324158])
img = coco.loadImgs(imgIds[np.random.randint(0, len(imgIds))])[0]
print(img)
print(img['flickr_url'])
response = req.get(img['flickr_url'])
#这里跟github中不一样,通过request来获取的图像url来得到图像的,因为发现如果使用coco_url会下载不了图片,可能跟外网有关
image = Image.open(BytesIO(response.content))
plt.imshow(image)
#在这里,如果在linux服务器上,由于没有GUI,会导致错误,所以, 在前面导包的时候加了import matplotlib as mpl
mpl.use('Agg')
plt.axis('off')
annIds = coco.getAnnIds(imgIds=img['id'])
anns = coco.loadAnns(annIds)
#print(anns)
ax=coco.showAnns(anns)
#如果这里提示了TKL的错误
注意,在linux服务器上,由于没有GUI,除非你有Xmanager,否则会导致错误TKL...,所以, 在coco.py前面导包的时候加了
import matplotlib as mpl
mpl.use('Agg')
如果在没有GUI的情况下,是在想看的话,那你就在coco.py的showAnns(ans)这个方法后面,加上plt.save('起个名.jpg'),再次执行,就能够看到这个图的分割结果了。
网友评论