1. 程式人生 > >Ubuntu 16.04系統Microsoft Common Objects in Context(COCO)資料集在Python環境中的使用

Ubuntu 16.04系統Microsoft Common Objects in Context(COCO)資料集在Python環境中的使用

Microsoft Common Objects in Context(簡寫COCO)資料集是微軟團隊提供的一個可以用來進行影象識別,分割,註解等開發工作的資料集。

該資料集主要有的特點如下:(1)Object segmentation(2)Recognition in Context(3)Multiple objects per image(4)More than 300,000 images(5)More than 2 Million instances(6)80 object categories(7)5 captions per image(8)Keypoints on 100,000 people

為了更好的介紹這個資料集,微軟在ECCV Workshops裡發表這篇文章:Microsoft COCO: Common Objects in Context。從這篇文章中,我們瞭解了這個資料集以物體識別為目標,主要從複雜的日常場景中擷取,影象中的目標通過精確的標定。影象包括91類目標,328,000張圖片和2,500,000個標籤。

下面,我們以2014年的資料集合,來簡單介紹一下這個資料集在Python環境中的使用。

建議使用aria2去下載,執行命令如下:

$ sudo apt-get install aria2

$ cd ~

$ mkdir coco

$ cd coco

$ aria2c -c http://msvocds.blob.core.windows.net/coco2014/train2014.zip

$ aria2c -c http://msvocds.blob.core.windows.net/annotations-1-0-3/instances_train-val2014.zip

下載完成後,解壓縮到當前目錄

$ sudo apt-get install unzip

$ unzip instances_train-val2014.zip

$ unzip train2014.zip

接下來,去下載原始碼COCO API的原始碼

$ sudo apt-get install git

$ git clone https://github.com/pdollar/coco.git

$ cd coco/PythonAPI

$ make

$ sudo make install

$ sudo python setup.py install

$ cd ../..

我們寫如下的測試例子:

testCoCo.py

from pycocotools.coco import COCO

import numpy as np

import skimage.io as io

import matplotlib.pyplot as plt

import pylab

pylab.rcParams['figure.figsize'] = (10.0, 8.0)


dataDir='.'

dataType='train2014'

annFile='%s/annotations/instances_%s.json'%(dataDir,dataType)

# initialize COCO api for instance annotations

coco=COCO(annFile)

# display COCO categories and supercategories

cats = coco.loadCats(coco.getCatIds())

nms=[cat['name'] for cat in cats]

print 'COCO categories: \n\n', ' '.join(nms)


nms = set([cat['supercategory'] for cat in cats])

print 'COCO supercategories: \n', ' '.join(nms)


# get all images containing given categories, select one at random

catIds = coco.getCatIds(catNms=['person','dog','skateboard']);

imgIds = coco.getImgIds(catIds=catIds );

img = coco.loadImgs(imgIds[np.random.randint(0,len(imgIds))])[0]


# load and display image

I = io.imread('%s/%s/%s'%(dataDir,dataType,img['file_name']))

plt.figure(); plt.axis('off')

plt.imshow(I)

plt.show()


# load and display instance annotations

plt.imshow(I); plt.axis('off')

annIds = coco.getAnnIds(imgIds=img['id'], catIds=catIds, iscrowd=None)

anns = coco.loadAnns(annIds)

coco.showAnns(anns)

plt.show()

執行指令碼的程式碼如下:

$ cd ~/coco

$ python testCoco.py

Reference: