pycococreator
repository·master·Indexed 20 days ago
https://github.com/waspinator/pycococreatorA toolkit for generating COCO-style dataset annotations, providing specialized functions for creating COCO-compliant annotations in both polygon and uncompressed RLE ("crowd") formats.
What's inside pycococreator
- 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.
Install pycococreator
masterYou can install pycococreator directly from the GitHub repository using pip. Note that the version specified in this command is 0.2.0.
pip install git+git://github.com/waspinator/pycococreator.git@0.2.0Install pycocotools for Python 3
masterIf you encounter issues installing
pycocotoolsfor 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.0Load and inspect COCO annotations
masterTo work with COCO-formatted data, use the
COCOclass frompycocotools.coco. You can load categories and their associated names or supercategories to understand the dataset schema.- Initialize the COCO object with your annotation JSON file.
- Use
getCatIds()to retrieve all category IDs. - Use
loadCats()to get the full category metadata. - Access
nameandsupercategoryfields 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])Visualize image annotations
masterTo visualize the annotations (like bounding boxes or segmentation masks) on top of an image:
- Load the image file from disk using the
file_nameprovided in the image metadata. - Retrieve annotation IDs for a specific image and category set using
getAnnIds(imgIds=..., catIds=..., iscrowd=None). - Load the actual annotation data using
loadAnns(annotation_ids). - 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)- Load the image file from disk using the
Retrieve images by category
masterYou can filter images based on specific category names using the following workflow:
- Get category IDs for specific names using
getCatIds(catNms=['name']). - Get image IDs associated with those categories using
getImgIds(catIds=...). - 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]- Get category IDs for specific names using