8. Use cocoapi (instances)
from matplotlib import pyplot as plt
from matplotlib.patches import Polygon
from skimage import io
from pycocotools.coco import COCO
import numpy as np
import os
annFile = "data/coco2017/annotations/instances_val2017.json"
root = "data/coco2017/val2017/"
coco = COCO(annFile)
catIds = coco.getCatIds(catNms=['person', 'dog', 'skateboard'])
imgIds = coco.getImgIds(catIds=catIds)
images = coco.loadImgs(imgIds[0:3])
9. Use cocoapi (instances)
for idx, image in enumerate(images, 1):
plt.subplot(1, 3, idx)
I = io.imread(image['coco_url'])
plt.axis('off')
plt.imshow(I)
plt.show()
10. Use cocoapi (instances)
for idx, image in enumerate(images, 1):
plt.subplot(1, 3, idx)
I = io.imread(image['coco_url'])
plt.axis('off')
plt.imshow(I)
annIds = coco.getAnnIds(imgIds=image['id'], catIds=catIds, iscrowd=None)
anns = coco.loadAnns(annIds)
coco.showAnns(anns)
plt.show()
11. Use cocoapi (instances)
for idx, image in enumerate(images, 1):
# image
ax = plt.subplot(1, 3, idx)
I = io.imread(image['coco_url'])
plt.imshow(I)
plt.axis('off')
# segment
annIds = coco.getAnnIds(imgIds=image['id'], catIds=catIds, iscrowd=None)
anns = coco.loadAnns(annIds)
coco.showAnns(anns)
# bbox
for ann in anns:
bbox_x, bbox_y, bbox_w, bbox_h = ann['bbox']
poly = [[bbox_x, bbox_y], [bbox_x, bbox_y+bbox_h],
[bbox_x+bbox_w, bbox_y+bbox_h], [bbox_x+bbox_w, bbox_y]]
np_poly = np.array(poly).reshape((4,2))
ax.add_patch(Polygon(np_poly, linestyle='--', facecolor='none', edgecolor="red", linewidth=2))
plt.show()
14. Use cocoapi (keypoints)
• visibility == 0 that keypoint not in the image.
• visibility == 1 that keypoint is in the image BUT
not visible namely maybe behind of an object.
• visibility == 2 that keypoint looks clearly. not
hidden.
"annotations": [
{
"segmentation": [
[
125.12,
539.69,...]
],
"num_keypoints": 10,
"area": 47803.27955,
"iscrowd": 0,
"keypoints": [
162,
551,
2,...],
"image_id": 425226,
"bbox": [
73.35,206.02,
300.58,372.5
],
"category_id": 1,
"id": 183126
},
15. Use cocoapi (keypoints)
annFile = "data/coco2017/annotations/person_keypoints_val2017.json"
coco = COCO(annFile)
catIds = coco.getCatIds(catNms=['person']) # the images icludes all categories
imgIds = coco.getImgIds(catIds=catIds)
images = coco.loadImgs(imgIds[10])
image = images[0]
18. Use cocoapi (caption)
"annotations": [
{
"image_id": 179765,
"id": 38,
"caption": "A black Honda motorcycle parked in front of a garage."
},…]
captions_val2017.json
['licenses', 'info', 'images', 'annotations']
20. Use cocoapi (caption)
A person kitesurfing over the waves of the ocean's shore.
a kite surfer is doing a flying trick over some water
A man is flying up in the air and having fun.
A guy is waterboarding in the ocean on a windy day.
A person kite boarding in rough seas near the shoreline.