COCO

class faster_coco_eval.core.COCO(annotation_file=None, use_deepcopy=False, print_function=<bound method Logger.debug of <Logger faster_coco_eval.core.coco (WARNING)>>)

Bases: object

annToMask(ann)

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

Parameters:

ann (dict) – Annotation information.

Returns:

Binary mask of the annotation.

Return type:

np.ndarray

annToRLE(ann)

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

Parameters:

ann (dict) – Annotation information.

Returns:

Run-length encoding of the annotation.

Return type:

dict

property cat_img_map: dict

Return a mapping from category ids to image ids.

Returns:

Mapping from category ids to image ids.

Return type:

dict

createIndex()

Create index for coco annotation data.

Creates internal indices for the COCO dataset to enable fast lookups. Builds mappings between images, annotations and categories.

download(tarDir=None, imgIds=None)

Warn that image downloading is no longer supported.

Parameters:
  • tarDir (Any, optional) – Target directory. Not used.

  • imgIds (list, optional) – Image ids. Not used.

Warns:

DeprecationWarning – The method is deprecated and does not download images.

dump(output_file)

Dump annotations to a json file.

Parameters:

output_file (Union[str, os.PathLike]) – Path to the output json file.

getAnnIds(imgIds=None, catIds=None, areaRng=None, iscrowd=None)

Get ann ids that satisfy given filter conditions.

Parameters:
  • imgIds (List[int], optional) – Get anns for given images. Defaults to None.

  • catIds (List[int], optional) – Get anns for given categories. Defaults to None.

  • areaRng (List[float], optional) – Get anns for given area range (e.g. [0, inf]). Defaults to None.

  • iscrowd (bool, optional) – Get anns for given crowd label (False or True). Defaults to None.

Returns:

Integer array of ann ids that satisfy the criteria.

Return type:

List[int]

getAnns(imgIds=None, catIds=None)

Get annotation objects directly, without a round trip through ids.

loadAnns(getAnnIds(...)) collects these same dicts, maps them to ids, validates every id, then looks each one back up. For evaluation sized workloads that round trip costs more than the lookup it performs, so this returns the objects the index already holds.

Selection matches getAnnIds() for the same arguments, including its image iteration order, so callers observe the same annotation sequence.

Parameters:
  • imgIds (List[int], optional) – Get anns for given images. Defaults to None.

  • catIds (List[int], optional) – Get anns for given categories. Defaults to None.

Returns:

Annotation objects satisfying the criteria.

Return type:

List[dict]

Examples

>>> coco = COCO()
>>> coco.getAnns(imgIds=[], catIds=[])
[]
getCatIds(catNms=None, supNms=None, catIds=None)

Get category ids that satisfy given filter conditions.

Parameters:
  • catNms (List[str], optional) – Get categories for given cat names. Defaults to None.

  • supNms (List[str], optional) – Get categories for given supercategory names. Defaults to None.

  • catIds (List[int], optional) – Get categories for given ids. Defaults to None.

Returns:

Integer array of cat ids.

Return type:

List[int]

getImgIds(imgIds=None, catIds=None)

Get image ids that satisfy given filter conditions.

Parameters:
  • imgIds (List[int], optional) – Get images for given ids. Defaults to None.

  • catIds (List[int], optional) – Get images with all given categories. Defaults to None.

Returns:

Integer array of img ids.

Return type:

List[int]

get_ann_ids(img_ids=None, cat_ids=None, area_rng=None, iscrowd=None)

Get ann ids that satisfy given filter conditions.

Parameters:
  • img_ids (List[int], optional) – Get anns for given imgs. Defaults to None.

  • cat_ids (List[int], optional) – Get anns for given cats. Defaults to None.

  • area_rng (List[float], optional) – Get anns with area less than this. Defaults to None.

  • iscrowd (bool, optional) – Get anns for given crowd label. Defaults to None.

Returns:

Integer array of ann ids.

Return type:

List[int]

get_cat_ids(cat_names=None, sup_names=None, cat_ids=None)

Get cat ids that satisfy given filter conditions.

Parameters:
  • cat_names (List[str], optional) – Get cats for given names. Defaults to None.

  • sup_names (List[str], optional) – Get cats for given supercategory names. Defaults to None.

  • cat_ids (List[int], optional) – Get cats for given ids. Defaults to None.

Returns:

Integer array of cat ids.

Return type:

List[int]

get_img_ids(img_ids=None, cat_ids=None)

Get img ids that satisfy given filter conditions.

Parameters:
  • img_ids (List[int], optional) – Get imgs for given ids. Defaults to None.

  • cat_ids (List[int], optional) – Get imgs with all given cats. Defaults to None.

Returns:

Integer array of img ids.

Return type:

List[int]

property img_ann_map: dict

Return a mapping from image ids to annotation ids.

Returns:

Mapping from image ids to annotation ids.

Return type:

dict

info()

Print information about the annotation file.

Prints the info section of the annotation file using the print function.

loadAnns(ids=None)

Load annotations with the specified ids.

Parameters:

ids (Union[List[int], int], optional) – Integer ids specifying annotations. Defaults to None.

Returns:

Loaded annotation objects.

Return type:

List[dict]

loadCats(ids=None)

Load categories with the specified ids.

Parameters:

ids (Union[List[int], int], optional) – Integer ids specifying categories. Defaults to None.

Returns:

Loaded category objects.

Return type:

List[dict]

loadImgs(ids=None)

Load images with the specified ids.

Parameters:

ids (Union[List[int], int], optional) – Integer ids specifying images. Defaults to None.

Returns:

Loaded image objects.

Return type:

List[dict]

loadNumpyAnnotations(data)

Convert result data from array to anns.

Parameters:

data (np.ndarray) – 2d array where each row contains [imageID, x1, y1, w, h, score, class]

Returns:

Converted annotations as a list of dicts.

Return type:

List[dict]

loadRes(resFile, min_score=0.0)

Load result file and return a result api object.

Parameters:
  • resFile (Union[str, os.PathLike, pathlib.PosixPath, pathlib.WindowsPath, dict, list, np.ndarray]) – File name of result file or numpy array.

  • min_score (float, optional) – Minimum score to consider a result. Defaults to 0.0.

Returns:

Result api object.

Return type:

COCO

load_anns(ids)

Load anns with the specified ids.

Parameters:

ids (List[int]) – Integer ids specifying anns.

Returns:

Loaded annotation objects.

Return type:

List[dict]

load_cats(ids)

Load cats with the specified ids.

Parameters:

ids (List[int]) – Integer ids specifying cats.

Returns:

Loaded category objects.

Return type:

List[dict]

load_imgs(ids)

Load imgs with the specified ids.

Parameters:

ids (List[int]) – Integer ids specifying imgs.

Returns:

Loaded image objects.

Return type:

List[dict]

static load_json(json_file, use_deepcopy=False)

Load a json file.

Parameters:
  • json_file (Union[str, os.PathLike, pathlib.PosixPath, pathlib.WindowsPath, dict, list]) – Path to the json file or data dict/list.

  • use_deepcopy (Optional[bool], optional) – If True, use deep copy. Defaults to False.

Returns:

Loaded json data.

Return type:

dict

property print_function: Callable

Get the function used for printing/logging messages.

Returns:

Print/log function.

Return type:

Callable

showAnns(anns, draw_bbox=False)

Display the specified annotations.

Parameters:
  • anns (List[dict]) – Annotations to display.

  • draw_bbox (Optional[bool], optional) – Whether to display bbox. Defaults to False.

to_dict(separate_fn=False)

Convert to a standard python dictionary.

Parameters:

separate_fn (bool, optional) – Whether to separate the fn category. Defaults to False.

Returns:

Standard python dictionary containing the COCO data.

Return type:

dict