下面的代碼改寫自 COCO 官方 API,改寫後的代碼 cocoz.py 被我放置在 Xinering/cocoapi。個人主要改進有:html
defaultdict
爲 dict.get()
,解決 Windows 的編碼問題。images
與標註數據 annotations
操做。具體的 API 使用說明見以下內容:python
COCOZ 簡介git
爲了可使用 cocoz
,你須要下載 Xinering/cocoapi。以後將其放在你須要運行的項目或程序根目錄,亦或者使用以下命令添加環境變量(暫時的):github
import sys sys.path.append('D:\API\cocoapi\PythonAPI') # 你下載的 cocoapi 所在路徑 from pycocotools.cocoz import AnnZ, ImageZ, COCOZ # 載入 cocoz
下面咱們就能夠利用這個 API 的 cocoz.AnnZ
、cocoz.ImageZ
和 cocoz.COCOZ
類來操做 COCO 圖片和標註了。下面我以 Windows 系統爲例說明,Linux 是相似的。json
root = r'E:\Data\coco' # COCO 數據集所在根目錄 annType = 'annotations_trainval2017' # COCO 標註數據類型 annZ = AnnZ(root, annType)
咱們來查看一下,該標註數據所包含的標註類型:api
annZ.names
['annotations/instances_train2017.json', 'annotations/instances_val2017.json', 'annotations/captions_train2017.json', 'annotations/captions_val2017.json', 'annotations/person_keypoints_train2017.json', 'annotations/person_keypoints_val2017.json']
以 dict
的形式載入 'annotations/instances_train2017.json'
的具體信息:網絡
annFile = 'annotations/instances_val2017.json' dataset = annZ.json2dict(annFile)
Loading json in memory ... used time: 0.890035 s
dataset.keys()
dict_keys(['info', 'licenses', 'images', 'annotations', 'categories'])
dataset['images'][0] # 記錄了一張圖片的一些標註信息
{'license': 4, 'file_name': '000000397133.jpg', 'coco_url': 'http://images.cocodataset.org/val2017/000000397133.jpg', 'height': 427, 'width': 640, 'date_captured': '2013-11-14 17:02:52', 'flickr_url': 'http://farm7.staticflickr.com/6116/6255196340_da26cf2c9e_z.jpg', 'id': 397133}
%pylab inline import skimage.io as sio coco_url = dataset['images'][0]['coco_url'] # use url to load image I = sio.imread(coco_url) plt.axis('off') plt.imshow(I) plt.show()
Populating the interactive namespace from numpy and matplotlib
爲了不解壓數據集,我使用了 zipfile
模塊:app
imgType = 'val2017' imgZ = ImageZ(root, imgType) I = imgZ.buffer2array(imgZ.names[0]) plt.axis('off') plt.imshow(I) plt.show()
root = r'E:\Data\coco' # COCO 數據集所在根目錄 annType = 'annotations_trainval2017' # COCO 標註數據類型 annFile = 'annotations/instances_val2017.json' annZ = AnnZ(root, annType) coco = COCOZ(annZ, annFile)
Loading json in memory ... used time: 1.02004 s Loading json in memory ... creating index... index created! used time: 0.431003 s
若是你須要預覽你載入的 COCO 數據集,可使用 print()
來實現:dom
print(coco)
description: COCO 2017 Dataset url: http://cocodataset.org version: 1.0 year: 2017 contributor: COCO Consortium date_created: 2017/09/01
coco.keys()
dict_keys(['dataset', 'anns', 'imgToAnns', 'catToImgs', 'imgs', 'cats'])
cats = coco.loadCats(coco.getCatIds()) nms = set([cat['name'] for cat in cats]) # 獲取 cat 的 name 信息 print('COCO categories: \n{}\n'.format(' '.join(nms))) # ============================================================ snms = set([cat['supercategory'] for cat in cats]) # 獲取 cat 的 name 信息 print('COCO supercategories: \n{}'.format(' '.join(snms)))
COCO categories: kite potted plant handbag clock umbrella sports ball bird frisbee toilet toaster spoon car snowboard banana fire hydrant skis chair tv skateboard wine glass tie cell phone cake zebra baseball glove stop sign airplane bed surfboard cup knife apple broccoli bicycle train carrot remote cat bear teddy bear person bench horse dog couch orange hair drier backpack giraffe sandwich book donut sink oven refrigerator boat mouse laptop toothbrush keyboard truck motorcycle bottle pizza traffic light cow microwave scissors bus baseball bat elephant fork bowl tennis racket suitcase vase sheep parking meter dining table hot dog COCO supercategories: accessory furniture sports vehicle appliance electronic animal indoor outdoor person kitchen food
獲取包含給定類別的全部圖片electron
# get all images containing given categories, select one at random catIds = coco.getCatIds(catNms=['cat', 'dog', 'snowboar']) # 獲取 Cat 的 Ids imgIds = coco.getImgIds(catIds=catIds ) # img = coco.loadImgs(imgIds)
隨機選擇一張圖片的信息:
img = coco.loadImgs(imgIds[np.random.randint(0,len(imgIds))])[0] img
{'license': 4, 'file_name': '000000318238.jpg', 'coco_url': 'http://images.cocodataset.org/val2017/000000318238.jpg', 'height': 640, 'width': 478, 'date_captured': '2013-11-21 00:01:06', 'flickr_url': 'http://farm8.staticflickr.com/7402/9964003514_84ce7550c9_z.jpg', 'id': 318238}
從網絡獲取圖片:
coco_url = img['coco_url'] I = sio.imread(coco_url) plt.axis('off') plt.imshow(I) plt.show()
從本地獲取圖片:
這裏有一個梗:cv2
的圖片默認模式是 BGR 而不是 RGB,因此,將 I
直接使用 plt
會改變原圖的顏色空間,爲此咱們可使用 cv2.COLOR_BGR2RGB
.
imgType = 'val2017' imgZ = ImageZ(root, imgType) I = imgZ.buffer2array(img['file_name']) 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)
# initialize COCO api for person keypoints annotations root = r'E:\Data\coco' # COCO 數據集所在根目錄 annType = 'annotations_trainval2017' # COCO 標註數據類型 annFile = 'annotations/person_keypoints_val2017.json' annZ = AnnZ(root, annType) coco_kps = COCOZ(annZ, annFile)
Loading json in memory ... used time: 0.882997 s Loading json in memory ... creating index... index created! used time: 0.368036 s
先選擇一張帶有 person
的圖片:
catIds = coco.getCatIds(catNms=['person']) # 獲取 Cat 的 Ids imgIds = coco.getImgIds(catIds=catIds) img = coco.loadImgs(imgIds)[77]
# use url to load image I = sio.imread(img['coco_url']) plt.axis('off') plt.imshow(I) plt.show()
# load and display keypoints annotations plt.imshow(I); plt.axis('off') ax = plt.gca() annIds = coco_kps.getAnnIds(imgIds=img['id'], catIds=catIds, iscrowd=None) anns = coco_kps.loadAnns(annIds) coco_kps.showAnns(anns)
# initialize COCO api for person keypoints annotations root = r'E:\Data\coco' # COCO 數據集所在根目錄 annType = 'annotations_trainval2017' # COCO 標註數據類型 annFile = 'annotations/captions_val2017.json' annZ = AnnZ(root, annType) coco_caps = COCOZ(annZ, annFile)
Loading json in memory ... used time: 0.435748 s Loading json in memory ... creating index... index created! used time: 0.0139964 s
# load and display caption annotations annIds = coco_caps.getAnnIds(imgIds=img['id']); anns = coco_caps.loadAnns(annIds) coco_caps.showAnns(anns) plt.imshow(I) plt.axis('off') plt.show()
show:
A brown horse standing next to a woman in front of a house. a person standing next to a horse next to a building A woman stands beside a large brown horse. The woman stands next to the large brown horse. A woman hold a brown horse while a woman watches.
若是你須要使用官方 API, 能夠參考 COCO 數據集的使用。
若是你以爲對你有幫助,請幫忙在 Github 上點個 star:datasetsome。該教程的代碼我放在了 GitHub: COCOZ 使用說明書。