code: def object_detection_df_to_coco(
      data: pd.DataFrame, save_path: Optional[str] = None
  ):
      """
      If the user already has dataframe format data and wants to convert to coco format .json files, this function
      completes the task
      Parameters
      ----------
      data
          pd.DataFrame format of object detection data
      save_path
          str path to save the output
      Returns
      -------
          Dict
      """
      output_json_dict = {
          "images": [],
          "type": "instances",
          "annotations": [],
          "categories": [],
      }
      bbox_count = 0
      unique_classes = {}
      for idx in range(data.shape[0]):
          row = data.iloc[idx]
          image_path = row["image"]
          rois = row["rois"]
          # label = row["label"]
          image_id = idx
  
          image_info = _get_image_info(image_path)
          if image_info:
              image_entry = {
                  "file_name": image_path,
                  "height": image_info["height"],
                  "width": image_info["width"],
                  "id": image_id,
              }
              output_json_dict["images"].append(image_entry)
          else:
              continue
  
          for roi in rois:
              xmin, ymin, xmax, ymax, class_label = roi
              x, y, w, h = bbox_xyxy_to_xywh([xmin, ymin, xmax, ymax])
  
              ann = {
                  "area": w * h,
                  "iscrowd": 0,
                  "bbox": [x, y, w, h],
                  "category_id": class_label,
                  "ignore": 0,
                  "segmentation": [],  # This script is not for segmentation
                  "image_id": image_id,
                  "id": bbox_count,
              }
              bbox_count += 1
  
              output_json_dict["annotations"].append(ann)
  
              if class_label not in unique_classes:
                  unique_classes[class_label] = len(unique_classes)
  
      for class_name, id in unique_classes.items():
          output_json_dict["categories"].append(
              {"supercategory": "none", "id": id, "name": class_name}
          )
  
      if save_path and save_path.endswith(".json"):
          with open(save_path, "w") as fp:
              json.dump(output_json_dict, fp)
  
      return output_json_dict
  
imports: import json
  import logging
  import os
  import warnings
  from pathlib import Path
  from typing import Dict, Iterable, List, Optional, Union
  import defusedxml.ElementTree
  import numpy
  import pandas
  import PIL
  from torchmetrics.detection.mean_ap import MeanAveragePrecision
  from ..constants import BBOX, LABEL, MAP, MAP_50, MAP_75, MAP_LARGE, MAP_MEDIUM, MAP_SMALL, MAR_1, MAR_10, MAR_100, MAR_LARGE, MAR_MEDIUM, MAR_SMALL, MEAN_AVERAGE_PRECISION
  from .download import download, is_url
label_desc: Use image node_id 3 for calling a global function with example usage: object_detection_df_to_coco(data, save_path) and returns: output_json_dict