http://cocodataset.org/#download 官網(wǎng)地址
本文的目的是獲取所有圖像的分割結果并保存的工作。
Mask API 中介紹
COCO為每個目標實例都提供了分割Msak走贪,instance_train201X.json表示的是整個數(shù)據(jù)集的結構,下面這部分主要介紹他有哪些數(shù)據(jù)以及其數(shù)據(jù)類型
{
"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,
}
剛介紹完他的數(shù)據(jù)類型佛猛,現(xiàn)介紹下具體到json文件中的每一部分的具體形式
解析下instance_train2014.json文件,最后我們講一下如何獲取并展示分割結果坠狡。
{
"info": #第一個info信息
{ #數(shù)據(jù)集信息
"description": "COCO 2014 Dataset",
"url": "http://cocodataset.org",
"version": "1.0",
"year": 2014,
"contributor": "COCO Consortium",
"date_created": "2017/09/01"
},
"images": #第二個圖片信息继找,數(shù)組包含了多張圖像
[ { #每張圖像的具體信息
"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里面是什么,首先它是通過壓縮處理后的分割區(qū)域的一個表示边臼,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下柠并,執(zhí)行make究飞,可能會出現(xiàn)下面情況
cd coco/PythonAPI
make
#錯誤提示pycocotools/_mask.c:沒有那個文件或目錄
pip install cython #解決方式
make#再次執(zhí)行make,如果你沒有出現(xiàn)上面的錯誤堂鲤,可以跳過
#接下來亿傅,驗證cocoApi是否安裝成功
python
>>>import pycocotools
#不報錯就成功了一半了
接下來,在coco文件夾瘟栖,下載好image和anotation葵擎。
這里介紹我們的分割結果圖獲取方法
官方給的使用示例在下載目錄下的pycocoDemo.ipynb文件下https://github.com/dengdan/coco/blob/master/PythonAPI/pycocoDemo.ipynb
那么我們首先了解一下,之前為什么要引入pycocotools半哟,因為這個問件下包含了對coco數(shù)據(jù)的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數(shù)據(jù)集中共有多少類,需要在pythonAPI下新建一個python文件戒良,命名為segcoco.py用于獲取分割圖,先執(zhí)行下面這段話体捏,獲取下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是一個包含多個屬性的數(shù)組\說是字典更好
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來得到圖像的,因為發(fā)現(xiàn)如果使用coco_url會下載不了圖片惰瓜,可能跟外網(wǎng)有關
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'),再次執(zhí)行柴淘,就能夠看到這個圖的分割結果了迫淹。