kwcoco.compat_dataset module

A wrapper around the basic kwcoco dataset with a pycocotools API.

We do not recommend using this API because it has some idiosyncrasies, where names can be missleading and APIs are not always clear / efficient: e.g.

  1. catToImgs returns integer image ids but imgToAnns returns annotation dictionaries.

  2. showAnns takes a dictionary list as an argument instead of an integer list

The cool thing is that this extends the kwcoco API so you can drop this for compatibility with the old API, but you still get access to all of the kwcoco API including dynamic addition / removal of categories / annotations / images.

class kwcoco.compat_dataset.COCO(annotation_file=None, **kw)[source]

Bases: CocoDataset

A wrapper around the basic kwcoco dataset with a pycocotools API.

Example

>>> from kwcoco.compat_dataset import *  # NOQA
>>> import kwcoco
>>> basic = kwcoco.CocoDataset.demo('shapes8')
>>> self = COCO(basic.dataset)
>>> self.info()
>>> print('self.imgToAnns = {!r}'.format(self.imgToAnns[1]))
>>> print('self.catToImgs = {!r}'.format(self.catToImgs))
createIndex()[source]
info()[source]

Print information about the annotation file.

property imgToAnns
property catToImgs

unlike the name implies, this actually goes from category to image ids Name retained for backward compatibility

getAnnIds(imgIds=[], catIds=[], areaRng=[], iscrowd=None)[source]

Get ann ids that satisfy given filter conditions. default skips that filter

Parameters:
  • imgIds (List[int]) – get anns for given imgs

  • catIds (List[int]) – get anns for given cats

  • areaRng (List[float]) – get anns for given area range (e.g. [0 inf])

  • iscrowd (bool | None) – get anns for given crowd label (False or True)

Returns:

integer array of ann ids

Return type:

List[int]

Example

>>> from kwcoco.compat_dataset import *  # NOQA
>>> import kwcoco
>>> self = COCO(kwcoco.CocoDataset.demo('shapes8').dataset)
>>> self.getAnnIds()
>>> self.getAnnIds(imgIds=1)
>>> self.getAnnIds(imgIds=[1])
>>> self.getAnnIds(catIds=[3])
getCatIds(catNms=[], supNms=[], catIds=[])[source]

filtering parameters. default skips that filter.

Parameters:
  • catNms (List[str]) – get cats for given cat names

  • supNms (List[str]) – get cats for given supercategory names

  • catIds (List[int]) – get cats for given cat ids

Returns:

integer array of cat ids

Return type:

List[int]

Example

>>> from kwcoco.compat_dataset import *  # NOQA
>>> import kwcoco
>>> self = COCO(kwcoco.CocoDataset.demo('shapes8').dataset)
>>> self.getCatIds()
>>> self.getCatIds(catNms=['superstar'])
>>> self.getCatIds(supNms=['raster'])
>>> self.getCatIds(catIds=[3])
getImgIds(imgIds=[], catIds=[])[source]

Get img ids that satisfy given filter conditions.

Parameters:
  • imgIds (List[int]) – get imgs for given ids

  • catIds (List[int]) – get imgs with all given cats

Returns:

integer array of img ids

Return type:

List[int]

Example

>>> from kwcoco.compat_dataset import *  # NOQA
>>> import kwcoco
>>> self = COCO(kwcoco.CocoDataset.demo('shapes8').dataset)
>>> self.getImgIds(imgIds=[1, 2])
>>> self.getImgIds(catIds=[3, 6, 7])
>>> self.getImgIds(catIds=[3, 6, 7], imgIds=[1, 2])
loadAnns(ids=[])[source]

Load anns with the specified ids.

Parameters:

ids (List[int]) – integer ids specifying anns

Returns:

loaded ann objects

Return type:

List[dict]

loadCats(ids=[])[source]

Load cats with the specified ids.

Parameters:

ids (List[int]) – integer ids specifying cats

Returns:

loaded cat objects

Return type:

List[dict]

loadImgs(ids=[])[source]

Load anns with the specified ids.

Parameters:

ids (List[int]) – integer ids specifying img

Returns:

loaded img objects

Return type:

List[dict]

showAnns(anns, draw_bbox=False)[source]

Display the specified annotations.

Parameters:

anns (List[Dict]) – annotations to display

loadRes(resFile)[source]

Load result file and return a result api object.

Parameters:

resFile (str) – file name of result file

Returns:

res result api object

Return type:

object

download(tarDir=None, imgIds=[])[source]

Download COCO images from mscoco.org server.

Parameters:
  • tarDir (str | PathLike | None) – COCO results directory name

  • imgIds (list) – images to be downloaded

loadNumpyAnnotations(data)[source]

Convert result data from a numpy array [Nx7] where each row contains {imageID,x1,y1,w,h,score,class}

Parameters:

data (numpy.ndarray)

Returns:

annotations (python nested list)

Return type:

List[Dict]

annToRLE(ann)[source]

Convert annotation which can be polygons, uncompressed RLE to RLE.

Returns:

kwimage.Mask

Note

  • This requires the C-extensions for kwimage to be installed (i.e.

pip install kwimage_ext) due to the need to interface with the bytes RLE format.

Example

>>> from kwcoco.compat_dataset import *  # NOQA
>>> import kwcoco
>>> self = COCO(kwcoco.CocoDataset.demo('shapes8').dataset)
>>> try:
>>>     rle = self.annToRLE(self.anns[1])
>>> except NotImplementedError:
>>>     import pytest
>>>     pytest.skip('missing kwimage c-extensions')
>>> else:
>>>     assert len(rle['counts']) > 2
>>> # xdoctest: +REQUIRES(module:pycocotools)
>>> self.conform(legacy=True)
>>> orig = self._aspycoco().annToRLE(self.anns[1])
annToMask(ann)[source]

Convert annotation which can be polygons, uncompressed RLE, or RLE to binary mask.

Returns:

binary mask (numpy 2D array)

Return type:

ndarray

Note

The mask is returned as a fortran (F-style) array with the same dimensions as the parent image.

_abc_impl = <_abc._abc_data object>