text: # ------------------------------------------------------------------------
  # Deformable DETR
  # Copyright (c) 2020 SenseTime. All Rights Reserved.
  # Licensed under the Apache License, Version 2.0 [see LICENSE for details]
  # ------------------------------------------------------------------------
  # Modified from DETR (https://github.com/facebookresearch/detr)
  # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
  # ------------------------------------------------------------------------
  
  """
  COCO evaluator that works in distributed mode.
  
  Mostly copy-paste from https://github.com/pytorch/vision/blob/edfd5a7/references/detection/coco_eval.py
  The difference is that there is less copy-pasting from pycocotools
  in the end of the file, as python3 can suppress prints with contextlib
  """
  import os
  import contextlib
  import copy
  import numpy as np
  import torch
  from pycocotools.cocoeval import COCOeval
  from pycocotools.coco import COCO
  from .Rotatedcoco import RotatedCOCO
  import pycocotools.mask as mask_util
  from .coco_eval import CocoEvaluator, evaluate
  from util.box_ops import polygon_box_iou
  from util.misc import all_gather
  
  class RotatedCocoEvaluator(CocoEvaluator):
      """
      For rotated object detection evaluation
      """
      def update(self, predictions):
          img_ids = list(np.unique(list(predictions.keys())))
          self.img_ids.extend(img_ids)
  
          for iou_type in self.iou_types:
              results = self.prepare(predictions, iou_type)
  
              # suppress pycocotools prints
              with open(os.devnull, 'w') as devnull:
                  with contextlib.redirect_stdout(devnull):
                      coco_dt = RotatedCOCO.loadRes(self.coco_gt, results) if results else COCO()
              coco_eval = self.coco_eval[iou_type]
  
              coco_eval.cocoDt = coco_dt
              coco_eval.params.imgIds = list(img_ids)
              img_ids, eval_imgs = evaluate(coco_eval)
  
              self.eval_imgs[iou_type].append(eval_imgs)
  
      def prepare_for_coco_detection(self, predictions):
          coco_results = []
          for original_id, prediction in predictions.items():
              if len(prediction) == 0:
                  continue
  
              boxes = prediction["boxes"]
              boxes = boxes.tolist() # polys
              scores = prediction["scores"].tolist()
              labels = prediction["labels"].tolist()
  
              coco_results.extend(
                  [
                      {
                          "image_id": original_id,
                          "category_id": labels[k],
                          "bbox": box,
                          "score": scores[k],
                      }
                      for k, box in enumerate(boxes)
                  ]
              )
          return coco_results
  