Pycocotools provides the official APIs for the Microsoft COCO (Common Objects in Context) dataset. It facilitates loading, parsing, and visualizing COCO annotations, as well as evaluating object detection, segmentation, and keypoint detection results. The current version is 2.0.11, and it is actively maintained with bug fixes and packaging improvements over the original COCO API.
pip install pycocotoolsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize the COCO API, load an annotation file, and retrieve basic information such as image and category IDs, and annotations for a specific image. It requires a downloaded COCO annotation JSON file.
Install 'Desktop development with C++' workload from Visual Studio Build Tools. For Python 3.9+, ensure compatibility with the installed C++ compiler version.
Replace `E.params.useSegm = True/False` with `E.params.iouType = 'segm'` or `E.params.iouType = 'bbox'` respectively.
Ensure `pip install cython numpy` is run before `pip install pycocotools`. For persistent issues, verify your C/C++ compiler setup (e.g., GCC on Linux, Visual C++ Build Tools on Windows).
Install 'Build Tools for Visual Studio' from Microsoft's website, ensuring to select the 'Desktop development with C++' workload. Alternatively, for Windows, `pip install pycocotools-windows` often resolves this by providing pre-built wheels.
Ensure C++ build tools are correctly installed and configured. If on Windows, `pip install pycocotools-windows` is recommended. For manual installation from source, ensure Cython is installed (`pip install cython`), then navigate to the `PythonAPI` directory of the cloned `cocoapi` repository and run `python setup.py build_ext --inplace` followed by `python setup.py install`.
Install the package using `pip install pycocotools`. If you are on Windows and encountering persistent issues, try `pip install pycocotools-windows`. Ensure you are installing it into the correct Python environment you intend to use.
Explicitly import the `mask` submodule from `pycocotools` using `import pycocotools.mask` or `from pycocotools import mask as coco_mask`.