MCPcopy Create free account
hub / github.com/PeizeSun/SparseR-CNN / load_coco_json

Function load_coco_json

detectron2/data/datasets/coco.py:30–206  ·  view source on GitHub ↗

Load a json file with COCO's instances annotation format. Currently supports instance detection, instance segmentation, and person keypoints annotations. Args: json_file (str): full path to the json file in COCO instances annotation format. image_root (str or path-l

(json_file, image_root, dataset_name=None, extra_annotation_keys=None)

Source from the content-addressed store, hash-verified

28
29
30def load_coco_json(json_file, image_root, dataset_name=None, extra_annotation_keys=None):
31 """
32 Load a json file with COCO's instances annotation format.
33 Currently supports instance detection, instance segmentation,
34 and person keypoints annotations.
35
36 Args:
37 json_file (str): full path to the json file in COCO instances annotation format.
38 image_root (str or path-like): the directory where the images in this json file exists.
39 dataset_name (str): the name of the dataset (e.g., coco_2017_train).
40 If provided, this function will also put "thing_classes" into
41 the metadata associated with this dataset.
42 extra_annotation_keys (list[str]): list of per-annotation keys that should also be
43 loaded into the dataset dict (besides "iscrowd", "bbox", "keypoints",
44 "category_id", "segmentation"). The values for these keys will be returned as-is.
45 For example, the densepose annotations are loaded in this way.
46
47 Returns:
48 list[dict]: a list of dicts in Detectron2 standard dataset dicts format. (See
49 `Using Custom Datasets </tutorials/datasets.html>`_ )
50
51 Notes:
52 1. This function does not read the image files.
53 The results do not have the "image" field.
54 """
55 from pycocotools.coco import COCO
56
57 timer = Timer()
58 json_file = PathManager.get_local_path(json_file)
59 with contextlib.redirect_stdout(io.StringIO()):
60 coco_api = COCO(json_file)
61 if timer.seconds() > 1:
62 logger.info("Loading {} takes {:.2f} seconds.".format(json_file, timer.seconds()))
63
64 id_map = None
65 if dataset_name is not None:
66 meta = MetadataCatalog.get(dataset_name)
67 cat_ids = sorted(coco_api.getCatIds())
68 cats = coco_api.loadCats(cat_ids)
69 # The categories in a custom json file may not be sorted.
70 thing_classes = [c["name"] for c in sorted(cats, key=lambda x: x["id"])]
71 meta.thing_classes = thing_classes
72
73 # In COCO, certain category ids are artificially removed,
74 # and by convention they are always ignored.
75 # We deal with COCO's id issue and translate
76 # the category ids to contiguous ids in [0, 80).
77
78 # It works by looking at the "categories" field in the json, therefore
79 # if users' own json also have incontiguous ids, we'll
80 # apply this mapping as well but print a warning.
81 if not (min(cat_ids) == 1 and max(cat_ids) == len(cat_ids)):
82 if "coco" not in dataset_name:
83 logger.warning(
84 """
85Category ids in annotations are not in [1, #categories]! We'll apply a mapping for you.
86"""
87 )

Callers 4

testMethod · 0.90
register_coco_instancesFunction · 0.85
coco.pyFile · 0.85

Calls 1

getMethod · 0.45

Tested by 1

testMethod · 0.72