code: def export_batch_inferences_as_coco_annotations(
      preds,
      img_files,
      transforms,
      class_map,
      output_filepath="inference_results_as_coco_annotations.json",
      info=None,
      licenses=None,
  ):
      """
      For converting object detection predictions to COCO annotation format.
      Useful for e.g. leveraging partly-trained models to help annotate
      unlabeled data and make round trips back into annotation programs.
  
      Parameters
      ----------
      preds : List[Prediction]
          The result of predict_from_dl()
      img_files : fastcore.foundation.L (i.e. 'Paths')
          References to the original image filepaths in array-like form.
      transforms : Albumentations Adapter
          Transforms that were applied to the original images (to be reversed)
      class_map : icevision.core.class_map.ClassMap
          The map of classes your model is familiar with
      output_filepath : str, optional
          The filepath (including filename) where you want the json results
          to be serialized, by default
          "new_pseudo_labels_for_further_training.json"
      info: dict, optional
          Option to manually create the info dict containing annotation metadata
          including year, version, description, contributor, url, and date created
              For example:
                  "info": {
                      "year": "2022",
                      "version": "1",
                      "description": "Exported from IceVision",
                      "contributor": "Awesome contributor",
                      "url": "https://lazyannotator.fun",
                      "date_created": "2022-08-05T20:13:09+00:00"
                  }
      licenses: List[dict], optional
          Option to manually create the license metadata for the annotations, e.g.
          licenses = [
              {
                  "name": "Creative Commons Attribution 4.0",
                  "id": 0,
                  "url": "https://creativecommons.org/licenses/by/4.0/legalcode",
              }
          ]
  
      Returns
      -------
      None
          This just spits out a serialized .json file and returns nothing.
      """
      object_category_list = [
          {"id": v, "name": k, "supercategory": ""}
          for k, v in class_map._class2id.items()
      ]
  
      if info is None:
          # Then automatically generate COCO annotation metadata:
          info = {
              "contributor": "",
              "date_created": "",
              "description": "",
              "url": "",
              "version": "",
              "year": "",
          }
  
      if licenses is None:
          licenses = [
              {
                  "name": "",
                  "id": 0,
                  "url": "",
              }
          ]
  
      addl_info = {
          "licenses": licenses,
          "info": info,
          "categories": object_category_list,
      }
  
      # Each entry needs a filepath
      [pred.add_component(FilepathRecordComponent()) for pred in preds]
      [preds[_].set_filepath(img_files[_]) for _ in range(len(preds))]
  
      # process_bbox_predictions happens inplace, thus no new variable
      for p in preds:
          process_bbox_predictions(
              p,
              PIL.Image.open(Path(p.pred.filepath)),
              transforms.tfms_list,
          )
  
      coco_style_preds = convert_preds_to_coco_style(preds)
      imgs_array = [PIL.Image.open(Path(fname)) for fname in img_files]
  
      sizes = [
          {"x": img._size[0], "y": img._size[1]} for img in imgs_array
      ]
  
      for idx, image in enumerate(coco_style_preds["images"]):
          coco_style_preds["images"][idx]["width"] = sizes[idx]["x"]
          coco_style_preds["images"][idx]["height"] = sizes[idx]["y"]
  
      finalized_pseudo_labels = {**addl_info, **coco_style_preds}
  
      # Serialize
      with open(output_filepath, "w") as jfile:
          json.dump(finalized_pseudo_labels, jfile, cls=NpEncoder)
  
          # Print confirmation message
          print(f"New COCO annotation file saved to {output_filepath}")
  
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 1 for calling a global function with example usage: export_batch_inferences_as_coco_annotations(preds, img_files, transforms, class_map, output_filepath, info, licenses) without return types