code: def convert_records_to_coco_style(
      records,
      images: bool = True,
      annotations: bool = True,
      categories: bool = True,
      show_pbar: bool = True,
  ):
      """Converts records from library format to coco format.
      Inspired from: https://github.com/pytorch/vision/blob/master/references/detection/coco_utils.py#L146
      """
      images_ = []
      annotations_dict = defaultdict(list)
  
      for record in pbar(records, show=show_pbar):
          if images:
              image_ = convert_record_to_coco_image(record)
              images_.append(image_)
  
          if annotations:
              annotations_ = convert_record_to_coco_annotations(record)
              for k, v in annotations_.items():
                  annotations_dict[k].extend(v)
  
      if annotations:
          annotations_dict = dict(annotations_dict)
          if not allequal([len(o) for o in annotations_dict.values()]):
              raise RuntimeError("Mismatch lenght of elements")
  
          # convert dict of lists to list of dicts
          annotations_ = []
          for i in range(len(annotations_dict["image_id"])):
              annotation = {
                  k: v[i] for k, v in annotations_dict.items()
              }
              # annotations should be initialized starting at 1 (torchvision issue #1530)
              annotation["id"] = i + 1
              annotations_.append(annotation)
  
      if categories:
          categories_set = set(annotations_dict["category_id"])
          categories_ = [{"id": i} for i in categories_set]
  
      res = {}
      if images_:
          res["images"] = images_
      if annotations:
          res["annotations"] = annotations_
      if categories:
          res["categories"] = categories_
  
      return res
  
file_path: icevision/icevision/data/convert_records_to_coco_style.py
imports: from icevision.imports import *
  from icevision.utils import *
  from icevision.core import *
  from pycocotools.coco import COCO
  from pycocotools.cocoeval import COCOeval
  import json
  import numpy
  import PIL
label_desc: Use image node_id 9 for calling a global function with example usage: convert_records_to_coco_style(records, images, annotations, categories, show_pbar) and returns: res