code: def from_directory_and_annotation(
      cls, images_directory: str, annotation: str
  ):
      samples = []
      with open(annotation, "r") as f:
          data = json.load(f)
  
      category_ids, class_names = zip(
          *[
              (category["id"], category["name"])
              for category in data["categories"]
          ]
      )
  
      annotations = cls.convert_to_dict(data["annotations"])
      category_id_to_index = {
          category_id: index
          for index, category_id in enumerate(category_ids)
      }
  
      for image in data["images"]:
          image_id = image["id"]
          image_path = os.path.join(
              images_directory, image["file_name"]
          )
          image_width = image["width"]
          image_height = image["height"]
  
          labels = []
          bboxes = []
          is_difficult = []
  
          if image_id in annotations:
              for annotations in annotations[image_id]:
                  class_index = category_id_to_index[
                      annotations["category_id"]
                  ]
                  x, y, w, h = annotations["bbox"]
                  bbox_xyxy = [x, y, x + w, y + h]
  
                  labels.append(class_index)
                  bboxes.append(bbox_xyxy)
                  is_difficult.append(annotations["iscrowd"])
  
          sample = DetectionSample(
              image_id=image_id,
              image_path=image_path,
              image_width=image_width,
              image_height=image_height,
              labels=np.array(labels, dtype=int).reshape(-1),
              bboxes=np.array(bboxes, dtype=np.float32).reshape(-1, 4),
              is_difficult=np.array(is_difficult, dtype=bool).reshape(
                  -1
              ),
          )
          samples.append(sample)
  
      return cls(samples, class_names)
  
file_path: pytorch-toolbelt/pytorch_toolbelt/datasets/providers/coco_detection.py
imports: import dataclasses
  import json
  import os.path
  from collections import defaultdict
  from typing import List
  import numpy
  from torch.utils.data import Dataset
label_desc: Use image node_id 5 for calling the COCODetectionDatasetReader obj's underlying member method code with example usage: obj.from_directory_and_annotation(cls, images_directory, annotation) and returns: cls