pycococreator

repository·master·Indexed 20 days ago

https://github.com/waspinator/pycococreator

A toolkit for generating COCO-style dataset annotations, providing specialized functions for creating COCO-compliant annotations in both polygon and uncompressed RLE ("crowd") formats.

Tokens
1.2K
Snippets
5
Records
6
Agent score
24%

What's inside pycococreator

  1. Overview of pycococreator

    master
    pycococreator is a toolkit designed to assist in the creation of COCO (Common Objects in Context) datasets. It provides specialized functions for generating COCO-compliant annotations, specifically supporting both uncompressed RLE ("crowd") formats and polygon formats.
  2. Install pycocotools for Python 3

    master

    If you encounter issues installing pycocotools for Python 3, follow these steps to install the necessary development headers and dependencies first:

    sudo apt-get install python3-dev
    pip install cython
    pip install git+git://github.com/waspinator/coco.git@2.1.0
  3. Load and inspect COCO annotations

    master

    To work with COCO-formatted data, use the COCO class from pycocotools.coco. You can load categories and their associated names or supercategories to understand the dataset schema.

    1. Initialize the COCO object with your annotation JSON file.
    2. Use getCatIds() to retrieve all category IDs.
    3. Use loadCats() to get the full category metadata.
    4. Access name and supercategory fields from the returned category dictionaries.
    from pycocotools.coco import COCO
    
    annotation_file = 'train/annotations/instances_shape_train2018.json'
    example_coco = COCO(annotation_file)
    
    # Get category names
    categories = example_coco.loadCats(example_coco.getCatIds())
    category_names = [category['name'] for category in categories]
    
    # Get supercategory names
    supercategories = set([category['supercategory'] for category in categories])
  4. Visualize image annotations

    master

    To visualize the annotations (like bounding boxes or segmentation masks) on top of an image:

    1. Load the image file from disk using the file_name provided in the image metadata.
    2. Retrieve annotation IDs for a specific image and category set using getAnnIds(imgIds=..., catIds=..., iscrowd=None).
    3. Load the actual annotation data using loadAnns(annotation_ids).
    4. Use showAnns(annotations) to overlay the annotations on a Matplotlib plot.
    import skimage.io as io
    import matplotlib.pyplot as plt
    import pylab
    
    # Setup paths and data
    image_directory = 'train/shapes_train2018/'
    # image_data comes from loadImgs()
    
    # Load and display
    image = io.imread(image_directory + image_data['file_name'])
    plt.imshow(image)
    plt.axis('off')
    
    # Get and show annotations
    annotation_ids = example_coco.getAnnIds(imgIds=image_data['id'], catIds=category_ids, iscrowd=None)
    annotations = example_coco.loadAnns(annotation_ids)
    example_coco.showAnns(annotations)
  5. Retrieve images by category

    master

    You can filter images based on specific category names using the following workflow:

    1. Get category IDs for specific names using getCatIds(catNms=['name']).
    2. Get image IDs associated with those categories using getImgIds(catIds=...).
    3. Load specific image metadata using loadImgs(image_ids).
    # Find images containing a 'square'
    category_ids = example_coco.getCatIds(catNms=['square'])
    image_ids = example_coco.getImgIds(catIds=category_ids)
    
    # Load metadata for a random image from the results
    import numpy as np
    image_data = example_coco.loadImgs(image_ids[np.random.randint(0, len(image_ids))])[0]