Lei Mao bio photo

Lei Mao

Machine Learning, Artificial Intelligence, Computer Science.

Twitter Facebook LinkedIn GitHub   G. Scholar E-Mail RSS

Introduction

COCO is a common dataset for object detection and segmentation. It provided a COCO API that allows the user to read and extract annotations conveniently.


In this blog post, I would like to explore the COCO dataset using the COCO Python API.

COCO Exploration

Install Dependencies

The following dependencies are necessary for COCO dataset exploration and visualization.

$ pip install matplotlib==3.3.4 numpy==1.19.5 pillow==8.2.0 requests==2.25.1
$ pip install -U 'git+https://github.com/leimao/cocoapi.git#subdirectory=PythonAPI'

Download Annotation Files

Instead of downloading the entire dataset, we will just download the small annotations files for illustration.

$ wget -c http://images.cocodataset.org/annotations/annotations_trainval2017.zip
$ unzip -o annotations_trainval2017.zip 
Archive:  annotations_trainval2017.zip
  inflating: annotations/instances_train2017.json  
  inflating: annotations/instances_val2017.json  
  inflating: annotations/captions_train2017.json  
  inflating: annotations/captions_val2017.json  
  inflating: annotations/person_keypoints_train2017.json  
  inflating: annotations/person_keypoints_val2017.json 

COCO Annotation Exploration

The basic usages of COCO API, including getting all category IDs, category names, getting images IDs by category, getting images by image ID, getting image annotations by image ID, loading, annotating, and saving images, have been created.

# explore_coco.py 
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from PIL import Image
import requests
from pycocotools.coco import COCO


def main():

    coco_annotation_file_path = "annotations/instances_val2017.json"

    coco_annotation = COCO(annotation_file=coco_annotation_file_path)

    # Category IDs.
    cat_ids = coco_annotation.getCatIds()
    print(f"Number of Unique Categories: {len(cat_ids)}")
    print("Category IDs:")
    print(cat_ids)  # The IDs are not necessarily consecutive.

    # All categories.
    cats = coco_annotation.loadCats(cat_ids)
    cat_names = [cat["name"] for cat in cats]
    print("Categories Names:")
    print(cat_names)

    # Category ID -> Category Name.
    query_id = cat_ids[0]
    query_annotation = coco_annotation.loadCats([query_id])[0]
    query_name = query_annotation["name"]
    query_supercategory = query_annotation["supercategory"]
    print("Category ID -> Category Name:")
    print(
        f"Category ID: {query_id}, Category Name: {query_name}, Supercategory: {query_supercategory}"
    )

    # Category Name -> Category ID.
    query_name = cat_names[2]
    query_id = coco_annotation.getCatIds(catNms=[query_name])[0]
    print("Category Name -> ID:")
    print(f"Category Name: {query_name}, Category ID: {query_id}")

    # Get the ID of all the images containing the object of the category.
    img_ids = coco_annotation.getImgIds(catIds=[query_id])
    print(f"Number of Images Containing {query_name}: {len(img_ids)}")

    # Pick one image.
    img_id = img_ids[2]
    img_info = coco_annotation.loadImgs([img_id])[0]
    img_file_name = img_info["file_name"]
    img_url = img_info["coco_url"]
    print(
        f"Image ID: {img_id}, File Name: {img_file_name}, Image URL: {img_url}"
    )

    # Get all the annotations for the specified image.
    ann_ids = coco_annotation.getAnnIds(imgIds=[img_id], iscrowd=None)
    anns = coco_annotation.loadAnns(ann_ids)
    print(f"Annotations for Image ID {img_id}:")
    print(anns)

    # Use URL to load image.
    im = Image.open(requests.get(img_url, stream=True).raw)

    # Save image and its labeled version.
    plt.axis("off")
    plt.imshow(np.asarray(im))
    plt.savefig(f"{img_id}.jpg", bbox_inches="tight", pad_inches=0)
    # Plot segmentation and bounding box.
    coco_annotation.showAnns(anns, draw_bbox=True)
    plt.savefig(f"{img_id}_annotated.jpg", bbox_inches="tight", pad_inches=0)

    return


if __name__ == "__main__":

    main()

Running the following program will print out the following information and save the sample images to hard drive.

$ python explore_coco.py 
loading annotations into memory...
Done (t=0.96s)
creating index...
index created!
Number of Unique Categories: 80
Category IDs:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 67, 70, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 84, 85, 86, 87, 88, 89, 90]
Categories Names:
['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']
Category ID -> Category Name:
Category ID: 1, Category Name: person, Supercategory: person
Category Name -> ID:
Category Name: car, Category ID: 3
Number of Images Containing car: 535
Image ID: 454661, File Name: 000000454661.jpg, Image URL: http://images.cocodataset.org/val2017/000000454661.jpg
Annotations for Image ID 454661:
[{'segmentation': [[368.36, 356.08, 498.47, 351.5, 633.17, 338.67, 639.58, 231.46, 591.02, 227.79, 549.79, 185.64, 350.03, 187.48, 295.97, 242.46, 280.39, 238.79, 268.48, 245.2, 276.73, 258.03, 247.4, 284.61, 254.73, 359.74]], 'area': 56297.64605000001, 'iscrowd': 0, 'image_id': 454661, 'bbox': [247.4, 185.64, 392.18, 174.1], 'category_id': 3, 'id': 134116}, {'segmentation': [[181.82, 210.29, 204.78, 207.53, 271.81, 207.53, 295.69, 214.88, 299.36, 220.39, 300.28, 222.22, 300.28, 225.9, 297.52, 231.41, 292.93, 236.92, 282.83, 236.92, 268.14, 244.26, 269.97, 249.77, 273.65, 252.53, 264.46, 258.95, 264.46, 264.46, 258.04, 270.89, 250.69, 275.48, 246.1, 282.83, 247.02, 293.85, 247.02, 302.11, 243.34, 314.05, 243.34, 317.72, 242.43, 320.48, 234.16, 323.23, 219.47, 323.23, 209.37, 324.15, 180.9, 316.81, 189.17, 293.85, 182.74, 290.18, 173.55, 270.89, 168.96, 258.04, 152.43, 243.34, 151.52, 243.34, 149.68, 236.92, 170.8, 212.12, 183.66, 211.2]], 'area': 10699.444349999998, 'iscrowd': 0, 'image_id': 454661, 'bbox': [149.68, 207.53, 150.6, 116.62], 'category_id': 3, 'id': 136684}, {'segmentation': [[180.23, 359.46, 177.59, 325.23, 177.59, 300.21, 176.93, 285.72, 165.08, 267.95, 153.89, 254.12, 141.38, 244.9, 145.99, 221.2, 138.09, 209.35, 120.31, 210.67, 103.19, 196.84, 92.0, 192.89, 84.1, 189.6, 74.22, 187.62, 53.81, 184.99, 26.82, 184.99, 13.65, 185.65, 0.0, 186.31, 0.49, 373.29]], 'area': 28391.276699999995, 'iscrowd': 0, 'image_id': 454661, 'bbox': [0.0, 184.99, 180.23, 188.3], 'category_id': 3, 'id': 137943}, {'segmentation': [[310.68, 128.26, 348.66, 121.93, 414.5, 128.26, 417.67, 185.87, 337.9, 191.56, 310.05, 223.22, 287.89, 204.86, 249.91, 198.53, 253.07, 166.24, 272.06, 163.71, 273.96, 141.55, 309.41, 144.08]], 'area': 10512.914100000002, 'iscrowd': 0, 'image_id': 454661, 'bbox': [249.91, 121.93, 167.76, 101.29], 'category_id': 6, 'id': 163954}, {'segmentation': [[275.39, 38.22, 315.69, 44.94, 309.93, 149.53, 273.47, 146.65]], 'area': 4106.2858, 'iscrowd': 0, 'image_id': 454661, 'bbox': [273.47, 38.22, 42.22, 111.31], 'category_id': 10, 'id': 405641}, {'segmentation': [[172.48, 118.32, 192.82, 117.7, 190.94, 186.22, 183.74, 186.22, 165.91, 186.22, 165.91, 180.59, 166.22, 136.47, 167.16, 123.02, 168.41, 119.89, 169.66, 117.07, 174.36, 116.76]], 'area': 1746.8993499999997, 'iscrowd': 0, 'image_id': 454661, 'bbox': [165.91, 116.76, 26.91, 69.46], 'category_id': 10, 'id': 408541}, {'segmentation': [[236.69, 62.77, 268.21, 63.94, 267.63, 163.2, 234.93, 162.03, 236.1, 62.19]], 'area': 3218.3995999999984, 'iscrowd': 0, 'image_id': 454661, 'bbox': [234.93, 62.19, 33.28, 101.01], 'category_id': 10, 'id': 410901}, {'segmentation': [[60.83, 162.89, 78.67, 162.16, 77.69, 188.06, 60.59, 184.15, 60.34, 164.6]], 'area': 417.91600000000017, 'iscrowd': 0, 'image_id': 454661, 'bbox': [60.34, 162.16, 18.33, 25.9], 'category_id': 10, 'id': 1381083}, {'segmentation': [[41.03, 117.84, 55.99, 117.59, 57.01, 149.28, 42.05, 147.76]], 'area': 460.1950999999999, 'iscrowd': 0, 'image_id': 454661, 'bbox': [41.03, 117.59, 15.98, 31.69], 'category_id': 10, 'id': 1384347}, {'segmentation': [[51.44, 168.71, 59.26, 169.01, 59.11, 185.09, 50.69, 184.79]], 'area': 130.7046, 'iscrowd': 0, 'image_id': 454661, 'bbox': [50.69, 168.71, 8.57, 16.38], 'category_id': 10, 'id': 2057288}]
COCO Image and Annotations

References