In [2]:
%matplotlib inline
from pycocotools.coco import COCO
import numpy as np
import matplotlib.pyplot as plt
import skimage.io as io
In [4]:
annFile = './data/coco/annotations/person_keypoints_train2014.json'
coco = COCO(annFile)
In [5]:
# 人のカテゴリのみ抽出
catIds = coco.getCatIds(catNms=['person'])
catIds
Out[5]:
In [6]:
imgIds = coco.getImgIds(catIds=catIds)
print(len(imgIds))
In [7]:
img_id = 0
imgInfo = coco.loadImgs(imgIds[img_id])[0]
imgInfo
Out[7]:
In [9]:
I = io.imread('data/coco/train2014/' + imgInfo['file_name'])
In [10]:
plt.imshow(I)
annIds = coco.getAnnIds(imgIds=imgInfo['id'], iscrowd=None)
anns = coco.loadAnns(annIds)
coco.showAnns(anns)
In [23]:
def show_person_keypoints(img_id):
catIds = coco.getCatIds(catNms=['person'])
imgIds = coco.getImgIds(catIds=catIds)
assert img_id < len(imgIds)
imgInfo = coco.loadImgs(imgIds[img_id])[0]
img = io.imread('data/coco/train2014/' + imgInfo['file_name'])
annIds = coco.getAnnIds(imgIds=imgInfo['id'], iscrowd=None)
anns = coco.loadAnns(annIds)
# annsは複数人数の情報が入ったリスト
plt.figure()
plt.imshow(img)
coco.showAnns(anns)
In [24]:
show_person_keypoints(0)
In [99]:
def show_person_keypoints(img_id):
catIds = coco.getCatIds(catNms=['person'])
imgIds = coco.getImgIds(catIds=catIds)
assert img_id < len(imgIds)
imgInfo = coco.loadImgs(imgIds[img_id])[0]
img = io.imread('data/coco/train2014/' + imgInfo['file_name'])
height, width, _ = img.shape
annIds = coco.getAnnIds(imgIds=imgInfo['id'], iscrowd=None)
anns = coco.loadAnns(annIds)
noses = []
for i in range(len(anns)): # person loop
keypoints = anns[i]['keypoints']
nose_points = keypoints[0:3]
# visible = 2 => labled and visible
if nose_points[2] == 2:
noses.append(nose_points)
if noses == []:
return
plt.figure()
plt.imshow(img)
plt.axis('off')
plt.xlim((0, width))
plt.ylim((height, 0))
for p in noses:
x1, y1, visible = p
plt.plot([x1], [y1], marker='o', markersize=3, color='red')
In [101]:
show_person_keypoints(4)
In [ ]:
In [ ]: