Load a json file in LVIS's annotation format. Args: json_file (str): full path to the LVIS json annotation file. image_root (str): the directory where the images in this json file exists. dataset_name (str): the name of the dataset (e.g., "lvis_v0.5_train").
(json_file, image_root, dataset_name=None)
| 38 | |
| 39 | |
| 40 | def load_lvis_json(json_file, image_root, dataset_name=None): |
| 41 | """ |
| 42 | Load a json file in LVIS's annotation format. |
| 43 | |
| 44 | Args: |
| 45 | json_file (str): full path to the LVIS json annotation file. |
| 46 | image_root (str): the directory where the images in this json file exists. |
| 47 | dataset_name (str): the name of the dataset (e.g., "lvis_v0.5_train"). |
| 48 | If provided, this function will put "thing_classes" into the metadata |
| 49 | associated with this dataset. |
| 50 | |
| 51 | Returns: |
| 52 | list[dict]: a list of dicts in Detectron2 standard format. (See |
| 53 | `Using Custom Datasets </tutorials/datasets.html>`_ ) |
| 54 | |
| 55 | Notes: |
| 56 | 1. This function does not read the image files. |
| 57 | The results do not have the "image" field. |
| 58 | """ |
| 59 | from lvis import LVIS |
| 60 | |
| 61 | json_file = PathManager.get_local_path(json_file) |
| 62 | |
| 63 | timer = Timer() |
| 64 | lvis_api = LVIS(json_file) |
| 65 | if timer.seconds() > 1: |
| 66 | logger.info("Loading {} takes {:.2f} seconds.".format(json_file, timer.seconds())) |
| 67 | |
| 68 | if dataset_name is not None: |
| 69 | meta = get_lvis_instances_meta(dataset_name) |
| 70 | MetadataCatalog.get(dataset_name).set(**meta) |
| 71 | |
| 72 | # sort indices for reproducible results |
| 73 | img_ids = sorted(lvis_api.imgs.keys()) |
| 74 | # imgs is a list of dicts, each looks something like: |
| 75 | # {'license': 4, |
| 76 | # 'url': 'http://farm6.staticflickr.com/5454/9413846304_881d5e5c3b_z.jpg', |
| 77 | # 'file_name': 'COCO_val2014_000000001268.jpg', |
| 78 | # 'height': 427, |
| 79 | # 'width': 640, |
| 80 | # 'date_captured': '2013-11-17 05:57:24', |
| 81 | # 'id': 1268} |
| 82 | imgs = lvis_api.load_imgs(img_ids) |
| 83 | # anns is a list[list[dict]], where each dict is an annotation |
| 84 | # record for an object. The inner list enumerates the objects in an image |
| 85 | # and the outer list enumerates over images. Example of anns[0]: |
| 86 | # [{'segmentation': [[192.81, |
| 87 | # 247.09, |
| 88 | # ... |
| 89 | # 219.03, |
| 90 | # 249.06]], |
| 91 | # 'area': 1035.749, |
| 92 | # 'image_id': 1268, |
| 93 | # 'bbox': [192.81, 224.8, 74.73, 33.43], |
| 94 | # 'category_id': 16, |
| 95 | # 'id': 42986}, |
| 96 | # ...] |
| 97 | anns = [lvis_api.img_ann_map[img_id] for img_id in img_ids] |
no test coverage detected