pycocotools没有文档说明,或者它的文档说明就在源代码的注释里面。注释如下:部分如下
# The following API functions are defined:
# COCO - COCO api class that loads COCO annotation file and prepare data structures.
# 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.
# Help on each functions can be accessed by: "help COCO>function".
from pycocotools.coco import COCO
import matplotlib.pyplot as plt
import cv2
import os
import numpy as np
import random
cocoRoot = "/media/gph/D(Data)/COCO/"
dataType = "val2017"
annFile = os.path.join(cocoRoot, f'annotations/instances_{dataType}.json')
print(f'Annotation file: {annFile}')
Annotation file: /media/gph/D(Data)/COCO/annotations/instances_val2017.json
# # initialize COCO api for instance annotations
coco=COCO(annFile)
loading annotations into memory…
Done (t=0.32s)
creating index…
index created!
# 利用getCatIds函数获取某个类别对应的ID,
# 这个函数可以实现更复杂的功能,请参考官方文档
ids = coco.getCatIds('person')[0]
print(f'"person" 对应的序号: {ids}')
# 利用loadCats获取序号对应的文字类别
# 这个函数可以实现更复杂的功能,请参考官方文档
cats = coco.loadCats(1)
print(f'"1" 对应的类别名称: {cats}')
“person” 对应的序号: 1
“1” 对应的类别名称: [{‘supercategory’: ‘person’, ‘id’: 1, ‘name’: ‘person’}]
# 获取包含person的所有图片
imgIds = coco.getImgIds(catIds=[1])
print(f'包含person的图片共有:{len(imgIds)}张')
包含person的图片共有:2693张
# 获取包含dog的所有图片
id = coco.getCatIds(['dog'])[0]
imgIds = coco.catToImgs[id]
print(f'包含dog的图片共有:{len(imgIds)}张, 分别是:')
print(imgIds)
包含dog的图片共有:218张, 分别是:
[289343, 61471, 472375, 520301, 579321, 494869, 554002, 78823, 419974, 404484, 329219, 68078, 170893, 65485, 498286, 424162, 61108, 67213, 365207, 131273, 279278, 7386, 139099, 554291, 482917, 88951, 60835, 261161, 129756, 267300, 169076, 546829, 209613, 224200, 205834, 432553, 225184, 372819, 452891, 76417, 366884, 291664, 29393, 377575, 129756, 209613, 432553, 372819, 366884, 209613, 372819, 372819, 78565, 171611, 489014, 193162, 361571, 532575, 190140, 79229, 407083, 415990, 219578, 161609, 311190, 241326, 474164, 117525, 189806, 246454, 286422, 235399, 236166, 126110, 140203, 392818, 53529, 288685, 305343, 193162, 246454, 375278, 532530, 251572, 81766, 401991, 49269, 206831, 139872, 412240, 22192, 71226, 505573, 237864, 292330, 534270, 222317, 318908, 347930, 89880, 89880, 170278, 225670, 486479, 30494, 554579, 549220, 555005, 399655, 185250, 309484, 80153, 273642, 446522, 329447, 389933, 145781, 575357, 462728, 273232, 490171, 181969, 263463, 517832, 338624, 138492, 297830, 179392, 509403, 182805, 17029, 236784, 385997, 295478, 149568, 82807, 253386, 511398, 355240, 512836, 107226, 367082, 151962, 338901, 81766, 71226, 289702, 182805, 236784, 512836, 107226, 81766, 107226, 149568, 143961, 355240, 143961, 289702, 401991, 269113, 355905, 562561, 134112, 22892, 463522, 564280, 255664, 90003, 309938, 447200, 221693, 547502, 236592, 364636, 72813, 283412, 367195, 357459, 479155, 331075, 464522, 125405, 560880, 52891, 421455, 64868, 369541, 269113, 562561, 90003, 547502, 125405, 547502, 547502, 269113,
262938, 262938, 262938, 262938, 464522, 269113, 530624, 366611, 324158, 193674, 318238, 159458, 318238, 371749, 371699, 427034, 543581, 471789, 395801, 57760, 427034, 57760, 57760]
imgId = imgIds[10]
imgInfo = coco.loadImgs(imgId)[0]
print(f'图像{imgId}的信息如下:\n{imgInfo}')
imPath = os.path.join(cocoRoot, 'images', dataType, imgInfo['file_name'])
im = cv2.imread(imPath)
plt.axis('off')
plt.imshow(im)
plt.show()
图像329219的信息如下:
{‘license’: 1, ‘file_name’: ‘000000329219.jpg’, ‘coco_url’: ‘http://images.cocodataset.org/val2017/000000329219.jpg’,
‘height’: 427, ‘width’: 640, ‘date_captured’: ‘2013-11-14 19:21:56’,
‘flickr_url’: ‘http://farm9.staticflickr.com/8104/8505307842_465524a6a6_z.jpg’,
‘id’: 329219}
plt.imshow(im); plt.axis('off')
# 获取该图像对应的anns的Id
annIds = coco.getAnnIds(imgIds=imgInfo['id'])
print(f'图像{imgInfo["id"]}包含{len(anns)}个ann对象,分别是:\n{annIds}')
anns = coco.loadAnns(annIds)
coco.showAnns(anns)
图像329219包含21个ann对象,分别是:
[8032, 192816, 693180, 1508387, 1510882, 1518236, 1527016, 1529043, 1882305, 1885153, 1885350, 1885410, 1886212, 1886466, 1887489, 1981518, 2106278, 2183575, 2183858, 2213662, 2213709]
print(f'ann{annIds[3]}对应的mask如下:')
mask = coco.annToMask(anns[3])
plt.imshow(mask); plt.axis('off')
ann1508387对应的mask如下:
(-0.5, 639.5, 426.5, -0.5)