In [3]:
%matplotlib inline
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)

In [7]:
dataDir = './data'
dataType = 'train2014'

インスタンスアノテーション


In [8]:
# インスタンスアノテーションデータで初期化
annFile = '%s/annotations/instances_%s.json' % (dataDir, dataType)
coco = COCO(annFile)


loading annotations into memory...
Done (t=12.34s)
creating index...
index created!

In [13]:
categories = coco.loadCats(coco.getCatIds())

In [14]:
categories


Out[14]:
[{'id': 1, 'name': 'person', 'supercategory': 'person'},
 {'id': 2, 'name': 'bicycle', 'supercategory': 'vehicle'},
 {'id': 3, 'name': 'car', 'supercategory': 'vehicle'},
 {'id': 4, 'name': 'motorcycle', 'supercategory': 'vehicle'},
 {'id': 5, 'name': 'airplane', 'supercategory': 'vehicle'},
 {'id': 6, 'name': 'bus', 'supercategory': 'vehicle'},
 {'id': 7, 'name': 'train', 'supercategory': 'vehicle'},
 {'id': 8, 'name': 'truck', 'supercategory': 'vehicle'},
 {'id': 9, 'name': 'boat', 'supercategory': 'vehicle'},
 {'id': 10, 'name': 'traffic light', 'supercategory': 'outdoor'},
 {'id': 11, 'name': 'fire hydrant', 'supercategory': 'outdoor'},
 {'id': 13, 'name': 'stop sign', 'supercategory': 'outdoor'},
 {'id': 14, 'name': 'parking meter', 'supercategory': 'outdoor'},
 {'id': 15, 'name': 'bench', 'supercategory': 'outdoor'},
 {'id': 16, 'name': 'bird', 'supercategory': 'animal'},
 {'id': 17, 'name': 'cat', 'supercategory': 'animal'},
 {'id': 18, 'name': 'dog', 'supercategory': 'animal'},
 {'id': 19, 'name': 'horse', 'supercategory': 'animal'},
 {'id': 20, 'name': 'sheep', 'supercategory': 'animal'},
 {'id': 21, 'name': 'cow', 'supercategory': 'animal'},
 {'id': 22, 'name': 'elephant', 'supercategory': 'animal'},
 {'id': 23, 'name': 'bear', 'supercategory': 'animal'},
 {'id': 24, 'name': 'zebra', 'supercategory': 'animal'},
 {'id': 25, 'name': 'giraffe', 'supercategory': 'animal'},
 {'id': 27, 'name': 'backpack', 'supercategory': 'accessory'},
 {'id': 28, 'name': 'umbrella', 'supercategory': 'accessory'},
 {'id': 31, 'name': 'handbag', 'supercategory': 'accessory'},
 {'id': 32, 'name': 'tie', 'supercategory': 'accessory'},
 {'id': 33, 'name': 'suitcase', 'supercategory': 'accessory'},
 {'id': 34, 'name': 'frisbee', 'supercategory': 'sports'},
 {'id': 35, 'name': 'skis', 'supercategory': 'sports'},
 {'id': 36, 'name': 'snowboard', 'supercategory': 'sports'},
 {'id': 37, 'name': 'sports ball', 'supercategory': 'sports'},
 {'id': 38, 'name': 'kite', 'supercategory': 'sports'},
 {'id': 39, 'name': 'baseball bat', 'supercategory': 'sports'},
 {'id': 40, 'name': 'baseball glove', 'supercategory': 'sports'},
 {'id': 41, 'name': 'skateboard', 'supercategory': 'sports'},
 {'id': 42, 'name': 'surfboard', 'supercategory': 'sports'},
 {'id': 43, 'name': 'tennis racket', 'supercategory': 'sports'},
 {'id': 44, 'name': 'bottle', 'supercategory': 'kitchen'},
 {'id': 46, 'name': 'wine glass', 'supercategory': 'kitchen'},
 {'id': 47, 'name': 'cup', 'supercategory': 'kitchen'},
 {'id': 48, 'name': 'fork', 'supercategory': 'kitchen'},
 {'id': 49, 'name': 'knife', 'supercategory': 'kitchen'},
 {'id': 50, 'name': 'spoon', 'supercategory': 'kitchen'},
 {'id': 51, 'name': 'bowl', 'supercategory': 'kitchen'},
 {'id': 52, 'name': 'banana', 'supercategory': 'food'},
 {'id': 53, 'name': 'apple', 'supercategory': 'food'},
 {'id': 54, 'name': 'sandwich', 'supercategory': 'food'},
 {'id': 55, 'name': 'orange', 'supercategory': 'food'},
 {'id': 56, 'name': 'broccoli', 'supercategory': 'food'},
 {'id': 57, 'name': 'carrot', 'supercategory': 'food'},
 {'id': 58, 'name': 'hot dog', 'supercategory': 'food'},
 {'id': 59, 'name': 'pizza', 'supercategory': 'food'},
 {'id': 60, 'name': 'donut', 'supercategory': 'food'},
 {'id': 61, 'name': 'cake', 'supercategory': 'food'},
 {'id': 62, 'name': 'chair', 'supercategory': 'furniture'},
 {'id': 63, 'name': 'couch', 'supercategory': 'furniture'},
 {'id': 64, 'name': 'potted plant', 'supercategory': 'furniture'},
 {'id': 65, 'name': 'bed', 'supercategory': 'furniture'},
 {'id': 67, 'name': 'dining table', 'supercategory': 'furniture'},
 {'id': 70, 'name': 'toilet', 'supercategory': 'furniture'},
 {'id': 72, 'name': 'tv', 'supercategory': 'electronic'},
 {'id': 73, 'name': 'laptop', 'supercategory': 'electronic'},
 {'id': 74, 'name': 'mouse', 'supercategory': 'electronic'},
 {'id': 75, 'name': 'remote', 'supercategory': 'electronic'},
 {'id': 76, 'name': 'keyboard', 'supercategory': 'electronic'},
 {'id': 77, 'name': 'cell phone', 'supercategory': 'electronic'},
 {'id': 78, 'name': 'microwave', 'supercategory': 'appliance'},
 {'id': 79, 'name': 'oven', 'supercategory': 'appliance'},
 {'id': 80, 'name': 'toaster', 'supercategory': 'appliance'},
 {'id': 81, 'name': 'sink', 'supercategory': 'appliance'},
 {'id': 82, 'name': 'refrigerator', 'supercategory': 'appliance'},
 {'id': 84, 'name': 'book', 'supercategory': 'indoor'},
 {'id': 85, 'name': 'clock', 'supercategory': 'indoor'},
 {'id': 86, 'name': 'vase', 'supercategory': 'indoor'},
 {'id': 87, 'name': 'scissors', 'supercategory': 'indoor'},
 {'id': 88, 'name': 'teddy bear', 'supercategory': 'indoor'},
 {'id': 89, 'name': 'hair drier', 'supercategory': 'indoor'},
 {'id': 90, 'name': 'toothbrush', 'supercategory': 'indoor'}]

In [23]:
names = [cat['name'] for cat in categories]

In [24]:
# COCOのカテゴリリスト
print('COCO categories: \n', ' '.join(names))


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

In [25]:
names = set([cat['supercategory'] for cat in categories])

In [27]:
# COCOのスーパーカテゴリリスト
print('COCO supercategories: \n', ' '.join(names))


COCO supercategories: 
 food appliance kitchen electronic vehicle furniture accessory sports indoor animal outdoor person

In [111]:
# 特定のカテゴリに属する画像をランダムに取得する
catIds = coco.getCatIds(catNms=['person', 'dog', 'skateboard'])

In [112]:
imgIds = coco.getImgIds(catIds=catIds)

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

In [114]:
img


Out[114]:
{'coco_url': 'http://mscoco.org/images/568187',
 'date_captured': '2013-11-18 10:52:54',
 'file_name': 'COCO_train2014_000000568187.jpg',
 'flickr_url': 'http://farm5.staticflickr.com/4027/4377663947_4632f4f029_z.jpg',
 'height': 375,
 'id': 568187,
 'license': 5,
 'width': 500}

In [115]:
I = io.imread('data/train2014/' + img['file_name'])

In [116]:
plt.imshow(I)


Out[116]:
<matplotlib.image.AxesImage at 0x18ab45208>

In [117]:
plt.imshow(I)
annIds = coco.getAnnIds(imgIds=img['id'], catIds=catIds, iscrowd=None)
anns = coco.loadAnns(annIds)
coco.showAnns(anns)



In [118]:
anns


Out[118]:
[{'area': 1712.2799,
  'bbox': [0.0, 124.22, 41.69, 67.66],
  'category_id': 18,
  'id': 3206,
  'image_id': 568187,
  'iscrowd': 0,
  'segmentation': [[1.62,
    133.75,
    1.95,
    129.48,
    6.55,
    126.85,
    10.16,
    124.22,
    12.79,
    124.22,
    17.39,
    127.18,
    21.0,
    129.15,
    25.27,
    131.12,
    26.25,
    134.4,
    26.25,
    136.05,
    30.19,
    138.34,
    33.15,
    143.27,
    33.15,
    149.51,
    32.82,
    157.07,
    32.82,
    160.68,
    36.11,
    165.6,
    36.43,
    176.77,
    38.08,
    182.35,
    41.69,
    187.94,
    38.4,
    188.27,
    36.11,
    187.61,
    35.45,
    186.95,
    34.79,
    183.67,
    34.79,
    182.03,
    33.48,
    179.07,
    31.18,
    176.44,
    25.92,
    182.03,
    21.0,
    184.98,
    12.46,
    184.98,
    12.13,
    191.88,
    8.85,
    191.88,
    6.55,
    191.22,
    6.22,
    190.56,
    6.22,
    190.24,
    5.56,
    189.25,
    4.25,
    188.59,
    3.92,
    187.28,
    3.92,
    184.65,
    5.56,
    183.67,
    6.88,
    182.35,
    2.93,
    181.7,
    0.0,
    184.98,
    1.29,
    162.32,
    4.25,
    160.68,
    7.86,
    160.02,
    8.52,
    159.36,
    6.88,
    156.74,
    6.88,
    155.75,
    6.88,
    154.11,
    6.88,
    152.8,
    6.22,
    150.83,
    7.95,
    146.75,
    8.33,
    145.43,
    8.52,
    144.48,
    7.57,
    143.73,
    6.44,
    143.82,
    6.35,
    143.35,
    5.69,
    142.6,
    6.25,
    140.99,
    4.27,
    140.71,
    3.23,
    140.52,
    3.04,
    140.05,
    2.76,
    139.39,
    2.57,
    138.64,
    2.1,
    136.94,
    2.95,
    135.71,
    2.67,
    135.05,
    1.63,
    135.05,
    1.16,
    134.58,
    1.16,
    134.11,
    1.35,
    132.88]]},
 {'area': 673.6726999999996,
  'bbox': [463.69, 129.71, 36.31, 27.74],
  'category_id': 18,
  'id': 15064,
  'image_id': 568187,
  'iscrowd': 0,
  'segmentation': [[464.34,
    152.89,
    467.38,
    152.03,
    468.03,
    150.08,
    467.16,
    148.13,
    467.16,
    145.74,
    467.59,
    144.01,
    467.16,
    141.19,
    467.16,
    134.91,
    469.33,
    133.61,
    474.31,
    129.71,
    483.41,
    130.14,
    493.6,
    131.44,
    500.0,
    134.04,
    499.88,
    146.83,
    493.81,
    146.39,
    488.18,
    147.91,
    485.79,
    150.73,
    480.59,
    152.68,
    478.21,
    153.98,
    478.21,
    156.15,
    478.64,
    156.36,
    475.61,
    157.45,
    471.28,
    157.01,
    469.54,
    157.01,
    468.68,
    156.8,
    468.24,
    155.93,
    467.16,
    155.28,
    464.34,
    155.06,
    463.69,
    153.54]]},
 {'area': 5789.745899999998,
  'bbox': [14.33, 65.73, 113.88, 135.67],
  'category_id': 1,
  'id': 212191,
  'image_id': 568187,
  'iscrowd': 0,
  'segmentation': [[68.26,
    201.4,
    60.67,
    192.98,
    57.3,
    151.69,
    40.45,
    144.1,
    30.34,
    133.99,
    14.33,
    127.25,
    15.17,
    112.92,
    25.28,
    95.22,
    41.29,
    86.8,
    41.29,
    86.8,
    42.13,
    75.0,
    49.72,
    65.73,
    61.52,
    67.42,
    60.67,
    90.17,
    73.31,
    95.22,
    85.11,
    106.18,
    94.51,
    121.14,
    89.45,
    126.19,
    103.78,
    130.41,
    113.89,
    164.96,
    114.73,
    172.54,
    128.21,
    184.34,
    120.63,
    189.4,
    105.46,
    186.03,
    102.93,
    172.54,
    91.13,
    145.58,
    75.12,
    146.42,
    74.28,
    178.44,
    80.18,
    196.98]]},
 {'area': 3353.92785,
  'bbox': [440.33, 74.37, 59.67, 85.72],
  'category_id': 1,
  'id': 223967,
  'image_id': 568187,
  'iscrowd': 0,
  'segmentation': [[444.37,
    158.23,
    444.37,
    152.33,
    448.71,
    148.91,
    444.68,
    135.55,
    440.64,
    120.33,
    440.33,
    114.74,
    442.5,
    106.05,
    452.13,
    97.66,
    457.72,
    93.0,
    460.52,
    90.83,
    461.14,
    88.96,
    462.38,
    81.2,
    465.18,
    76.85,
    473.87,
    74.37,
    476.98,
    77.47,
    479.15,
    82.13,
    481.64,
    89.59,
    482.88,
    93.31,
    483.5,
    95.8,
    488.78,
    95.8,
    487.85,
    97.97,
    489.71,
    99.52,
    491.58,
    111.95,
    493.75,
    121.27,
    495.3,
    126.24,
    495.92,
    130.89,
    500.0,
    134.62,
    500.0,
    147.67,
    494.99,
    146.73,
    491.58,
    145.8,
    492.51,
    148.29,
    487.85,
    151.39,
    476.67,
    155.12,
    471.39,
    156.98,
    464.86,
    157.29,
    457.72,
    156.05,
    451.2,
    157.61,
    447.78,
    159.47,
    443.74,
    160.09]]},
 {'area': 2392.431250000001,
  'bbox': [465.17, 7.58, 34.83, 112.93],
  'category_id': 1,
  'id': 227436,
  'image_id': 568187,
  'iscrowd': 0,
  'segmentation': [[492.98,
    10.96,
    487.92,
    7.58,
    482.02,
    12.64,
    476.97,
    27.81,
    475.28,
    27.81,
    468.54,
    36.24,
    465.17,
    48.88,
    465.17,
    57.3,
    465.17,
    76.69,
    471.07,
    76.69,
    474.44,
    78.37,
    487.08,
    97.75,
    491.29,
    96.07,
    500.0,
    120.51,
    499.72,
    32.87,
    494.66,
    16.85]]},
 {'area': 267.6581999999997,
  'bbox': [442.17, 153.94, 45.4, 12.07],
  'category_id': 41,
  'id': 639525,
  'image_id': 568187,
  'iscrowd': 0,
  'segmentation': [[463.43,
    154.2,
    468.15,
    156.56,
    472.87,
    158.4,
    474.97,
    157.09,
    477.07,
    155.25,
    478.91,
    154.2,
    479.17,
    153.94,
    487.57,
    156.56,
    485.99,
    159.97,
    479.17,
    158.92,
    476.29,
    159.45,
    469.2,
    162.33,
    467.36,
    166.01,
    463.43,
    164.69,
    461.07,
    164.17,
    458.7,
    163.38,
    454.77,
    164.17,
    451.62,
    164.17,
    448.73,
    162.6,
    450.31,
    159.18,
    449.0,
    158.66,
    442.17,
    156.04,
    446.63,
    155.51,
    450.57,
    154.99,
    453.46,
    154.72]]}]

キーポイントアノテーション


In [119]:
annFile = '%s/annotations/person_keypoints_%s.json' % (dataDir, dataType)
coco_kps = COCO(annFile)


loading annotations into memory...
Done (t=7.03s)
creating index...
index created!

In [120]:
plt.imshow(I)
annIds = coco_kps.getAnnIds(imgIds=img['id'], catIds=catIds, iscrowd=None)
anns = coco_kps.loadAnns(annIds)
coco_kps.showAnns(anns)



In [122]:
anns[0]['num_keypoints']


Out[122]:
16

In [125]:
# (x1, y1, v1)
anns[0]['keypoints']


Out[125]:
[53,
 85,
 2,
 56,
 83,
 2,
 50,
 82,
 2,
 0,
 0,
 0,
 43,
 78,
 2,
 70,
 97,
 2,
 30,
 98,
 2,
 88,
 121,
 2,
 18,
 120,
 2,
 83,
 119,
 2,
 30,
 126,
 2,
 76,
 133,
 2,
 50,
 134,
 2,
 95,
 136,
 2,
 68,
 137,
 2,
 110,
 175,
 2,
 68,
 180,
 2]

In [124]:
anns


Out[124]:
[{'area': 5789.7459,
  'bbox': [14.33, 65.73, 113.88, 135.67],
  'category_id': 1,
  'id': 212191,
  'image_id': 568187,
  'iscrowd': 0,
  'keypoints': [53,
   85,
   2,
   56,
   83,
   2,
   50,
   82,
   2,
   0,
   0,
   0,
   43,
   78,
   2,
   70,
   97,
   2,
   30,
   98,
   2,
   88,
   121,
   2,
   18,
   120,
   2,
   83,
   119,
   2,
   30,
   126,
   2,
   76,
   133,
   2,
   50,
   134,
   2,
   95,
   136,
   2,
   68,
   137,
   2,
   110,
   175,
   2,
   68,
   180,
   2],
  'num_keypoints': 16,
  'segmentation': [[68.26,
    201.4,
    60.67,
    192.98,
    57.3,
    151.69,
    40.45,
    144.1,
    30.34,
    133.99,
    14.33,
    127.25,
    15.17,
    112.92,
    25.28,
    95.22,
    41.29,
    86.8,
    41.29,
    86.8,
    42.13,
    75,
    49.72,
    65.73,
    61.52,
    67.42,
    60.67,
    90.17,
    73.31,
    95.22,
    85.11,
    106.18,
    94.51,
    121.14,
    89.45,
    126.19,
    103.78,
    130.41,
    113.89,
    164.96,
    114.73,
    172.54,
    128.21,
    184.34,
    120.63,
    189.4,
    105.46,
    186.03,
    102.93,
    172.54,
    91.13,
    145.58,
    75.12,
    146.42,
    74.28,
    178.44,
    80.18,
    196.98]]},
 {'area': 3353.92785,
  'bbox': [440.33, 74.37, 59.67, 85.72],
  'category_id': 1,
  'id': 223967,
  'image_id': 568187,
  'iscrowd': 0,
  'keypoints': [470,
   93,
   2,
   473,
   90,
   2,
   468,
   90,
   2,
   477,
   91,
   2,
   465,
   91,
   2,
   486,
   103,
   2,
   456,
   100,
   2,
   489,
   124,
   2,
   444,
   112,
   2,
   490,
   144,
   2,
   453,
   113,
   2,
   474,
   131,
   2,
   462,
   125,
   2,
   466,
   151,
   2,
   447,
   121,
   2,
   488,
   150,
   2,
   452,
   151,
   2],
  'num_keypoints': 17,
  'segmentation': [[444.37,
    158.23,
    444.37,
    152.33,
    448.71,
    148.91,
    444.68,
    135.55,
    440.64,
    120.33,
    440.33,
    114.74,
    442.5,
    106.05,
    452.13,
    97.66,
    457.72,
    93,
    460.52,
    90.83,
    461.14,
    88.96,
    462.38,
    81.2,
    465.18,
    76.85,
    473.87,
    74.37,
    476.98,
    77.47,
    479.15,
    82.13,
    481.64,
    89.59,
    482.88,
    93.31,
    483.5,
    95.8,
    488.78,
    95.8,
    487.85,
    97.97,
    489.71,
    99.52,
    491.58,
    111.95,
    493.75,
    121.27,
    495.3,
    126.24,
    495.92,
    130.89,
    500,
    134.62,
    500,
    147.67,
    494.99,
    146.73,
    491.58,
    145.8,
    492.51,
    148.29,
    487.85,
    151.39,
    476.67,
    155.12,
    471.39,
    156.98,
    464.86,
    157.29,
    457.72,
    156.05,
    451.2,
    157.61,
    447.78,
    159.47,
    443.74,
    160.09]]},
 {'area': 2392.43125,
  'bbox': [465.17, 7.58, 34.83, 112.93],
  'category_id': 1,
  'id': 227436,
  'image_id': 568187,
  'iscrowd': 0,
  'keypoints': [488,
   16,
   2,
   489,
   14,
   2,
   486,
   14,
   2,
   0,
   0,
   0,
   480,
   15,
   1,
   497,
   31,
   2,
   472,
   33,
   2,
   0,
   0,
   0,
   468,
   52,
   2,
   0,
   0,
   0,
   469,
   71,
   2,
   493,
   73,
   2,
   477,
   74,
   2,
   498,
   98,
   2,
   476,
   100,
   1,
   0,
   0,
   0,
   476,
   126,
   1],
  'num_keypoints': 13,
  'segmentation': [[492.98,
    10.96,
    487.92,
    7.58,
    482.02,
    12.64,
    476.97,
    27.81,
    475.28,
    27.81,
    468.54,
    36.24,
    465.17,
    48.88,
    465.17,
    57.3,
    465.17,
    76.69,
    471.07,
    76.69,
    474.44,
    78.37,
    487.08,
    97.75,
    491.29,
    96.07,
    500,
    120.51,
    499.72,
    32.87,
    494.66,
    16.85]]}]

In [141]:
plt.imshow(I)
plt.plot([68], [180], marker='o', markersize=3, color='red')


Out[141]:
[<matplotlib.lines.Line2D at 0x1ceba6e80>]

In [ ]: