code: def letterbox_image(
      image: np.ndarray, inp_dim: Tuple[int, int]
  ) -> np.ndarray:
      """
      Resizes images into the dimension expected by the network. This
      function fills extra spaces in the image with grayscale, if the
      image is smaller than the expected dimesion. This implementation
      keeps the aspect ration of the original image.
      """
      img_w, img_h = (
          image.shape[1],
          image.shape[0],
      )  # original image dimension
      net_w, net_h = inp_dim  # the dimension expected by the network.
  
      # calculate the new dimension with same aspect ration as
      # the original image.
      scale_factor = min(net_w / img_w, net_h / img_h)
      new_w = int(round(img_w * scale_factor))
      new_h = int(round(img_h * scale_factor))
  
      resized_image = cv.resize(
          image, (new_w, new_h), interpolation=cv.INTER_CUBIC
      )
      canvas = np.full((net_w, net_h, 3), 128)
      canvas[
          (net_h - new_h) // 2 : (net_h - new_h) // 2 + new_h,
          (net_w - new_w) // 2 : (net_w - new_w) // 2 + new_w,
          :,
      ] = resized_image
      return canvas
  
file_path: ImageAI/imageai/yolov3/utils.py
imports: import math
  from typing import Union, List, Tuple
  import torch
  import numpy
  import cv2
  from torchvision.ops import batched_nms
label_desc: Use image node_id 2 for calling a global function with example usage: letterbox_image(image, inp_dim) and returns: canvas