docstr: 将预测的结果转换成COCOeval指定的格式，针对目标检测任务
docstr_tok: ["\u5c06\u9884\u6d4b\u7684\u7ed3\u679c\u8f6c\u6362\u6210COCOeval\u6307\u5b9a\u7684\u683c\u5f0f\uff0c\u9488\u5bf9\u76ee\u6807\u68c0\u6d4b\u4efb\u52a1"]
code: def prepare_for_coco_detection(self, targets, outputs):
      """将预测的结果转换成COCOeval指定的格式，针对目标检测任务"""
      # 遍历每张图像的预测结果
      for target, output in zip(targets, outputs):
          if len(output) == 0:
              continue
  
          img_id = int(target["image_id"])
          if img_id in self.img_ids:
              # 防止出现重复的数据
              continue
          self.img_ids.append(img_id)
          per_image_boxes = output["boxes"]
          # 对于coco_eval, 需要的每个box的数据格式为[x_min, y_min, w, h]
          # 而我们预测的box格式是[x_min, y_min, x_max, y_max]，所以需要转下格式
          per_image_boxes[:, 2:] -= per_image_boxes[:, :2]
          per_image_classes = output["labels"].tolist()
          per_image_scores = output["scores"].tolist()
  
          res_list = []
          # 遍历每个目标的信息
          for object_score, object_class, object_box in zip(
              per_image_scores, per_image_classes, per_image_boxes
          ):
              object_score = float(object_score)
              class_idx = int(object_class)
              if self.classes_mapping is not None:
                  class_idx = int(self.classes_mapping[str(class_idx)])
              # We recommend rounding coordinates to the nearest tenth of a pixel
              # to reduce resulting JSON file size.
              object_box = [round(b, 2) for b in object_box.tolist()]
  
              res = {
                  "image_id": img_id,
                  "category_id": class_idx,
                  "bbox": object_box,
                  "score": round(object_score, 3),
              }
              res_list.append(res)
          self.results.append(res_list)
  
file_path: deep-learning-for-image-processing/pytorch_object_detection/mask_rcnn/train_utils/coco_eval.py
imports: import json
  import copy
  import numpy
  from pycocotools.coco import COCO
  from pycocotools.cocoeval import COCOeval
  import pycocotools.mask
  from .distributed_utils import all_gather, is_main_process
label_desc: Use image node_id 2 for calling the EvalCOCOMetric obj's underlying member method code with example usage: obj.prepare_for_coco_detection(targets, outputs) without return types