code: def __init__(
      self,
      root,
      dataset="train",
      years="2017",
      transforms=None,
      det_json_path=None,
      fixed_size=(256, 192),
  ):
      super().__init__()
      assert dataset in [
          "train",
          "val",
      ], 'dataset must be in ["train", "val"]'
      anno_file = f"person_keypoints_{dataset}{years}.json"
      assert os.path.exists(root), "file '{}' does not exist.".format(
          root
      )
      self.img_root = os.path.join(root, f"{dataset}{years}")
      assert os.path.exists(
          self.img_root
      ), "path '{}' does not exist.".format(self.img_root)
      self.anno_path = os.path.join(root, "annotations", anno_file)
      assert os.path.exists(
          self.anno_path
      ), "file '{}' does not exist.".format(self.anno_path)
  
      self.fixed_size = fixed_size
      self.mode = dataset
      self.transforms = transforms
      self.coco = COCO(self.anno_path)
      img_ids = list(sorted(self.coco.imgs.keys()))
  
      if det_json_path is not None:
          det = self.coco.loadRes(det_json_path)
      else:
          det = self.coco
  
      self.valid_person_list = []
      obj_idx = 0
      for img_id in img_ids:
          img_info = self.coco.loadImgs(img_id)[0]
          ann_ids = det.getAnnIds(imgIds=img_id)
          anns = det.loadAnns(ann_ids)
          for ann in anns:
              # only save person class
              if ann["category_id"] != 1:
                  print(
                      f'warning: find not support id: {ann["category_id"]}, only support id: 1 (person)'
                  )
                  continue
  
              # COCO_val2017_detections_AP_H_56_person.json文件中只有det信息，没有keypoint信息，跳过检查
              if det_json_path is None:
                  # skip objs without keypoints annotation
                  if "keypoints" not in ann:
                      continue
                  if max(ann["keypoints"]) == 0:
                      continue
  
              xmin, ymin, w, h = ann["bbox"]
              # Use only valid bounding boxes
              if w > 0 and h > 0:
                  info = {
                      "box": [xmin, ymin, w, h],
                      "image_path": os.path.join(
                          self.img_root, img_info["file_name"]
                      ),
                      "image_id": img_id,
                      "image_width": img_info["width"],
                      "image_height": img_info["height"],
                      "obj_origin_hw": [h, w],
                      "obj_index": obj_idx,
                      "score": ann["score"] if "score" in ann else 1.0,
                  }
  
                  # COCO_val2017_detections_AP_H_56_person.json文件中只有det信息，没有keypoint信息，跳过
                  if det_json_path is None:
                      keypoints = np.array(ann["keypoints"]).reshape(
                          [-1, 3]
                      )
                      visible = keypoints[:, 2]
                      keypoints = keypoints[:, :2]
                      info["keypoints"] = keypoints
                      info["visible"] = visible
  
                  self.valid_person_list.append(info)
                  obj_idx += 1
  
file_path: deep-learning-for-image-processing/pytorch_keypoint/HRNet/my_dataset_coco.py
imports: import os
  import copy
  import torch
  import numpy
  import cv2
  import torch.utils.data
  from pycocotools.coco import COCO
label_desc: Use image node_id 1 to create a new CocoKeypoint object from inherited base classes: data with example: obj = CocoKeypoint(root, dataset, years, transforms, det_json_path, fixed_size)