code: import json
  import os
  import os.path as osp
  #from collections import OrderedDict
  import tempfile
  
  import numpy as np
  
  from mmpose.core.evaluation.top_down_eval import (keypoint_nme,
                                                    keypoint_pck_accuracy)
  from mmpose.datasets.builder import DATASETS
  from mmpose.datasets.datasets.base import Kpt2dSviewRgbImgTopDownDataset
  
  ######
  
  from collections import OrderedDict, defaultdict
  
  import json_tricks as json
  import numpy as np
  from mmcv import Config, deprecated_api_warning
  from xtcocotools.cocoeval import COCOeval
  
  from ....core.post_processing import oks_nms, soft_oks_nms
  from ...builder import DATASETS
  from ..base import Kpt2dSviewRgbImgTopDownDataset
  
  #####
  
  @DATASETS.register_module()
  class TopDownCOCOTinyDataset(Kpt2dSviewRgbImgTopDownDataset):
  
      def __init__(self,
                   ann_file,
                   img_prefix,
                   data_cfg,
                   pipeline,
                   dataset_info=None,
                   test_mode=False):
          super().__init__(
              ann_file, img_prefix, data_cfg, pipeline, dataset_info, coco_style=False, test_mode=test_mode)
  
          # flip_pairs, upper_body_ids and lower_body_ids will be used
          # in some data augmentations like random flip
          self.ann_info['flip_pairs'] = [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10],
                                         [11, 12], [13, 14], [15, 16]]
          self.ann_info['upper_body_ids'] = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
          self.ann_info['lower_body_ids'] = (11, 12, 13, 14, 15, 16)
  
          self.ann_info['joint_weights'] = None
          self.ann_info['use_different_joint_weights'] = False
  
          self.dataset_name = 'coco_tiny'
          self.db = self._get_db()
  
      def _get_db(self):
          with open(self.ann_file) as f:
              anns = json.load(f)
  
          db = []
          for idx, ann in enumerate(anns):
              # get image path
              image_file = osp.join(self.img_prefix, ann['image_file'])
              # get bbox
              bbox = ann['bbox']
              center, scale = self._xywh2cs(*bbox)
              # get keypoints
              keypoints = np.array(
                  ann['keypoints'], dtype=np.float32).reshape(-1, 3)
              num_joints = keypoints.shape[0]
              joints_3d = np.zeros((num_joints, 3), dtype=np.float32)
              joints_3d[:, :2] = keypoints[:, :2]
              joints_3d_visible = np.zeros((num_joints, 3), dtype=np.float32)
              joints_3d_visible[:, :2] = np.minimum(1, keypoints[:, 2:3])
  
              sample = {
                  'image_file': image_file,
                  'center': center,
                  'scale': scale,
                  'bbox': bbox,
                  'rotation': 0,
                  'joints_3d': joints_3d,
                  'joints_3d_visible': joints_3d_visible,
                  'bbox_score': 1,
                  'bbox_id': idx,
              }
              db.append(sample)
  
          return db
  
      def _xywh2cs(self, x, y, w, h):
          """This encodes bbox(x, y, w, h) into (center, scale)
          Args:
              x, y, w, h
          Returns:
              tuple: A tuple containing center and scale.
              - center (np.ndarray[float32](2,)): center of the bbox (x, y).
              - scale (np.ndarray[float32](2,)): scale of the bbox w & h.
          """
          aspect_ratio = self.ann_info['image_size'][0] / self.ann_info[
              'image_size'][1]
          center = np.array([x + w * 0.5, y + h * 0.5], dtype=np.float32)
          if w > aspect_ratio * h:
              h = w * 1.0 / aspect_ratio
          elif w < aspect_ratio * h:
              w = h * aspect_ratio
  
          # pixel std is 200.0
          scale = np.array([w / 200.0, h / 200.0], dtype=np.float32)
          # padding to include proper amount of context
          scale = scale * 1.25
          return center, scale
  
      @deprecated_api_warning(name_dict=dict(outputs='results'))
      def evaluate(self, results, res_folder=None, metric='mAP', **kwargs):
          """Evaluate coco keypoint results. The pose prediction results will be
          saved in ``${res_folder}/result_keypoints.json``.
  
          Note:
              - batch_size: N
              - num_keypoints: K
              - heatmap height: H
              - heatmap width: W
  
          Args:
              results (list[dict]): Testing results containing the following
                  items:
  
                  - preds (np.ndarray[N,K,3]): The first two dimensions are \
                      coordinates, score is the third dimension of the array.
                  - boxes (np.ndarray[N,6]): [center[0], center[1], scale[0], \
                      scale[1],area, score]
                  - image_paths (list[str]): For example, ['data/coco/val2017\
                      /000000393226.jpg']
                  - heatmap (np.ndarray[N, K, H, W]): model output heatmap
                  - bbox_id (list(int)).
              res_folder (str, optional): The folder to save the testing
                  results. If not specified, a temp folder will be created.
                  Default: None.
              metric (str | list[str]): Metric to be performed. Defaults: 'mAP'.
  
          Returns:
              dict: Evaluation results for evaluation metric.
          """
          metrics = metric if isinstance(metric, list) else [metric]
          allowed_metrics = ['mAP']
          for metric in metrics:
              if metric not in allowed_metrics:
                  raise KeyError(f'metric {metric} is not supported')
  
          if res_folder is not None:
              tmp_folder = None
              res_file = osp.join(res_folder, 'result_keypoints.json')
          else:
              tmp_folder = tempfile.TemporaryDirectory()
              res_file = osp.join(tmp_folder.name, 'result_keypoints.json')
  
          kpts = defaultdict(list)
  
          for result in results:
              preds = result['preds']
              boxes = result['boxes']
              image_paths = result['image_paths']
              bbox_ids = result['bbox_ids']
  
              batch_size = len(image_paths)
              for i in range(batch_size):
                  image_id = self.name2id[image_paths[i][len(self.img_prefix):]]
                  kpts[image_id].append({
                      'keypoints': preds[i],
                      'center': boxes[i][0:2],
                      'scale': boxes[i][2:4],
                      'area': boxes[i][4],
                      'score': boxes[i][5],
                      'image_id': image_id,
                      'bbox_id': bbox_ids[i]
                  })
          kpts = self._sort_and_unique_bboxes(kpts)
  
          # rescoring and oks nms
          num_joints = self.ann_info['num_joints']
          vis_thr = self.vis_thr
          oks_thr = self.oks_thr
          valid_kpts = []
          for image_id in kpts.keys():
              img_kpts = kpts[image_id]
              for n_p in img_kpts:
                  box_score = n_p['score']
                  kpt_score = 0
                  valid_num = 0
                  for n_jt in range(0, num_joints):
                      t_s = n_p['keypoints'][n_jt][2]
                      if t_s > vis_thr:
                          kpt_score = kpt_score + t_s
                          valid_num = valid_num + 1
                  if valid_num != 0:
                      kpt_score = kpt_score / valid_num
                  # rescoring
                  n_p['score'] = kpt_score * box_score
  
              if self.use_nms:
                  nms = soft_oks_nms if self.soft_nms else oks_nms
                  keep = nms(img_kpts, oks_thr, sigmas=self.sigmas)
                  valid_kpts.append([img_kpts[_keep] for _keep in keep])
              else:
                  valid_kpts.append(img_kpts)
  
          self._write_coco_keypoint_results(valid_kpts, res_file)
  
          info_str = self._do_python_keypoint_eval(res_file)
          name_value = OrderedDict(info_str)
  
          if tmp_folder is not None:
              tmp_folder.cleanup()
  
          return name_value
  
      def _write_coco_keypoint_results(self, keypoints, res_file):
          """Write results into a json file."""
          data_pack = [{
              'cat_id': self._class_to_coco_ind[cls],
              'cls_ind': cls_ind,
              'cls': cls,
              'ann_type': 'keypoints',
              'keypoints': keypoints
          } for cls_ind, cls in enumerate(self.classes)
                       if not cls == '__background__']
  
          results = self._coco_keypoint_results_one_category_kernel(data_pack[0])
  
          with open(res_file, 'w') as f:
              json.dump(results, f, sort_keys=True, indent=4)
  
      def _coco_keypoint_results_one_category_kernel(self, data_pack):
          """Get coco keypoint results."""
          cat_id = data_pack['cat_id']
          keypoints = data_pack['keypoints']
          cat_results = []
  
          for img_kpts in keypoints:
              if len(img_kpts) == 0:
                  continue
  
              _key_points = np.array(
                  [img_kpt['keypoints'] for img_kpt in img_kpts])
              key_points = _key_points.reshape(-1,
                                               self.ann_info['num_joints'] * 3)
  
              result = [{
                  'image_id': img_kpt['image_id'],
                  'category_id': cat_id,
                  'keypoints': key_point.tolist(),
                  'score': float(img_kpt['score']),
                  'center': img_kpt['center'].tolist(),
                  'scale': img_kpt['scale'].tolist()
              } for img_kpt, key_point in zip(img_kpts, key_points)]
  
              cat_results.extend(result)
  
          return cat_results
  
  
      def _do_python_keypoint_eval(self, res_file):
          """Keypoint evaluation using COCOAPI."""
          coco_det = self.coco.loadRes(res_file)
          coco_eval = COCOeval(self.coco, coco_det, 'keypoints', self.sigmas)
          coco_eval.params.useSegm = None
          coco_eval.evaluate()
          coco_eval.accumulate()
          coco_eval.summarize()
  
          stats_names = [
              'AP', 'AP .5', 'AP .75', 'AP (M)', 'AP (L)', 'AR', 'AR .5',
              'AR .75', 'AR (M)', 'AR (L)'
          ]
  
          info_str = list(zip(stats_names, coco_eval.stats))
  
          return info_str
  
      def _sort_and_unique_bboxes(self, kpts, key='bbox_id'):
          """sort kpts and remove the repeated ones."""
          for img_id, persons in kpts.items():
              num = len(persons)
              kpts[img_id] = sorted(kpts[img_id], key=lambda x: x[key])
              for i in range(num - 1, 0, -1):
                  if kpts[img_id][i][key] == kpts[img_id][i - 1][key]:
                      del kpts[img_id][i]
  
          return kpts
  
  
      def evaluate1111(self, results, res_folder=None, metric='PCK', **kwargs):
  #    def evaluate(self, results, res_folder=None, metric='PCK', **kwargs):
          """Evaluate keypoint detection results. The pose prediction results will
          be saved in `${res_folder}/result_keypoints.json`.
  
          Note:
          batch_size: N
          num_keypoints: K
          heatmap height: H
          heatmap width: W
  
          Args:
          results (list(preds, boxes, image_path, output_heatmap))
              :preds (np.ndarray[N,K,3]): The first two dimensions are
                  coordinates, score is the third dimension of the array.
              :boxes (np.ndarray[N,6]): [center[0], center[1], scale[0]
                  , scale[1],area, score]
              :image_paths (list[str]): For example, ['Test/source/0.jpg']
              :output_heatmap (np.ndarray[N, K, H, W]): model outputs.
  
          res_folder (str, optional): The folder to save the testing
                  results. If not specified, a temp folder will be created.
                  Default: None.
          metric (str | list[str]): Metric to be performed.
              Options: 'PCK', 'NME'.
  
          Returns:
              dict: Evaluation results for evaluation metric.
          """
          metrics = metric if isinstance(metric, list) else [metric]
          allowed_metrics = ['PCK', 'NME']
          for metric in metrics:
              if metric not in allowed_metrics:
                  raise KeyError(f'metric {metric} is not supported')
  
          if res_folder is not None:
              tmp_folder = None
              res_file = osp.join(res_folder, 'result_keypoints.json')
          else:
              tmp_folder = tempfile.TemporaryDirectory()
              res_file = osp.join(tmp_folder.name, 'result_keypoints.json')
  
          kpts = []
          for result in results:
              preds = result['preds']
              boxes = result['boxes']
              image_paths = result['image_paths']
              bbox_ids = result['bbox_ids']
  
              batch_size = len(image_paths)
              for i in range(batch_size):
                  kpts.append({
                      'keypoints': preds[i].tolist(),
                      'center': boxes[i][0:2].tolist(),
                      'scale': boxes[i][2:4].tolist(),
                      'area': float(boxes[i][4]),
                      'score': float(boxes[i][5]),
                      'bbox_id': bbox_ids[i]
                  })
          kpts = self._sort_and_unique_bboxes(kpts)
  
          self._write_keypoint_results(kpts, res_file)
          info_str = self._report_metric(res_file, metrics)
          name_value = OrderedDict(info_str)
  
          if tmp_folder is not None:
              tmp_folder.cleanup()
  
          return name_value
  
      def _report_metric(self, res_file, metrics, pck_thr=0.3):
          """Keypoint evaluation.
  
          Args:
          res_file (str): Json file stored prediction results.
          metrics (str | list[str]): Metric to be performed.
              Options: 'PCK', 'NME'.
          pck_thr (float): PCK threshold, default: 0.3.
  
          Returns:
          dict: Evaluation results for evaluation metric.
          """
          info_str = []
  
          with open(res_file, 'r') as fin:
              preds = json.load(fin)
          assert len(preds) == len(self.db)
  
          outputs = []
          gts = []
          masks = []
  
          for pred, item in zip(preds, self.db):
              outputs.append(np.array(pred['keypoints'])[:, :-1])
              gts.append(np.array(item['joints_3d'])[:, :-1])
              masks.append((np.array(item['joints_3d_visible'])[:, 0]) > 0)
  
          outputs = np.array(outputs)
          gts = np.array(gts)
          masks = np.array(masks)
  
          normalize_factor = self._get_normalize_factor(gts)
  
          if 'PCK' in metrics:
              _, pck, _ = keypoint_pck_accuracy(outputs, gts, masks, pck_thr,
                                                normalize_factor)
              info_str.append(('PCK', pck))
  
          if 'NME' in metrics:
              info_str.append(
                  ('NME', keypoint_nme(outputs, gts, masks, normalize_factor)))
  
          return info_str
  
      @staticmethod
      def _write_keypoint_results(keypoints, res_file):
          """Write results into a json file."""
  
          with open(res_file, 'w') as f:
              json.dump(keypoints, f, sort_keys=True, indent=4)
  
      @staticmethod
      def _sort_and_unique_bboxes(kpts, key='bbox_id'):
          """sort kpts and remove the repeated ones."""
          kpts = sorted(kpts, key=lambda x: x[key])
          num = len(kpts)
          for i in range(num - 1, 0, -1):
              if kpts[i][key] == kpts[i - 1][key]:
                  del kpts[i]
  
          return kpts
  
      @staticmethod
      def _get_normalize_factor(gts):
          """Get inter-ocular distance as the normalize factor, measured as the
          Euclidean distance between the outer corners of the eyes.
  
          Args:
              gts (np.ndarray[N, K, 2]): Groundtruth keypoint location.
  
          Return:
              np.ndarray[N, 2]: normalized factor
          """
  
          interocular = np.linalg.norm(
              gts[:, 0, :] - gts[:, 1, :], axis=1, keepdims=True)
          return np.tile(interocular, [1, 2])
  
apis: numpy.tile numpy.zeros numpy.array numpy.linalg.norm numpy.minimum