text: import cv2
  import numpy as np
  
  def gaussian2D(shape, sigma=1):
      m, n = [(ss - 1.) / 2. for ss in shape]
      y, x = np.ogrid[-m:m+1,-n:n+1]
  
      h = np.exp(-(x * x + y * y) / (2 * sigma * sigma))
      h[h < np.finfo(h.dtype).eps * h.max()] = 0
      return h
  
  def draw_gaussian(heatmap, center, radius, k=1):
      diameter = 2 * radius + 1
      gaussian = gaussian2D((diameter, diameter), sigma=diameter / 6)
  
      x, y = center
  
      height, width = heatmap.shape[0:2]
      
      left, right = min(x, radius), min(width - x, radius + 1)
      top, bottom = min(y, radius), min(height - y, radius + 1)
  
      masked_heatmap  = heatmap[y - top:y + bottom, x - left:x + right]
      masked_gaussian = gaussian[radius - top:radius + bottom, radius - left:radius + right]
      np.maximum(masked_heatmap, masked_gaussian * k, out=masked_heatmap)
  
  def gaussian_radius(det_size, min_overlap):
      height, width = det_size
  
      a1  = 1
      b1  = (height + width)
      c1  = width * height * (1 - min_overlap) / (1 + min_overlap)
      sq1 = np.sqrt(b1 ** 2 - 4 * a1 * c1)
      r1  = (b1 - sq1) / (2 * a1)
  
      a2  = 4
      b2  = 2 * (height + width)
      c2  = (1 - min_overlap) * width * height
      sq2 = np.sqrt(b2 ** 2 - 4 * a2 * c2)
      r2  = (b2 - sq2) / (2 * a2)
  
      a3  = 4 * min_overlap
      b3  = -2 * min_overlap * (height + width)
      c3  = (min_overlap - 1) * width * height
      sq3 = np.sqrt(b3 ** 2 - 4 * a3 * c3)
      r3  = (b3 + sq3) / (2 * a3)
      return min(r1, r2, r3)
  
  def _get_border(border, size):
      i = 1
      while size - border // i <= border // i:
          i *= 2
      return border // i
  
  def random_crop(image, detections, random_scales, view_size, border=64):
      view_height, view_width   = view_size
      image_height, image_width = image.shape[0:2]
  
      scale  = np.random.choice(random_scales)
      height = int(view_height * scale)
      width  = int(view_width  * scale)
  
      cropped_image = np.zeros((height, width, 3), dtype=image.dtype)
  
      w_border = _get_border(border, image_width)
      h_border = _get_border(border, image_height)
  
      ctx = np.random.randint(low=w_border, high=image_width - w_border)
      cty = np.random.randint(low=h_border, high=image_height - h_border)
  
      x0, x1 = max(ctx - width // 2, 0),  min(ctx + width // 2, image_width)
      y0, y1 = max(cty - height // 2, 0), min(cty + height // 2, image_height)
  
      left_w, right_w = ctx - x0, x1 - ctx
      top_h, bottom_h = cty - y0, y1 - cty
  
      # crop image
      cropped_ctx, cropped_cty = width // 2, height // 2
      x_slice = slice(cropped_ctx - left_w, cropped_ctx + right_w)
      y_slice = slice(cropped_cty - top_h, cropped_cty + bottom_h)
      cropped_image[y_slice, x_slice, :] = image[y0:y1, x0:x1, :]
  
      # crop detections
      cropped_detections = detections.copy()
      cropped_detections[:, 0:4:2] -= x0
      cropped_detections[:, 1:4:2] -= y0
      cropped_detections[:, 0:4:2] += cropped_ctx - left_w
      cropped_detections[:, 1:4:2] += cropped_cty - top_h
  
      return cropped_image, cropped_detections
  
  def random_crop_cls(image, detections, offset_image, random_scales, view_size, border=64):
      view_height, view_width   = view_size
      image_height, image_width = image.shape[0:2]
  
      scale  = np.random.choice(random_scales)
      offset_image /= scale
      height = int(view_height * scale)
      width  = int(view_width  * scale)
  
      cropped_image = np.zeros((height, width, 3), dtype=image.dtype)
  
      w_border = _get_border(border, image_width)
      h_border = _get_border(border, image_height)
  
      ctx = np.random.randint(low=w_border, high=image_width - w_border)
      cty = np.random.randint(low=h_border, high=image_height - h_border)
  
      x0, x1 = max(ctx - width // 2, 0),  min(ctx + width // 2, image_width)
      y0, y1 = max(cty - height // 2, 0), min(cty + height // 2, image_height)
  
      left_w, right_w = ctx - x0, x1 - ctx
      top_h, bottom_h = cty - y0, y1 - cty
  
      # crop image
      cropped_ctx, cropped_cty = width // 2, height // 2
      x_slice = slice(cropped_ctx - left_w, cropped_ctx + right_w)
      y_slice = slice(cropped_cty - top_h, cropped_cty + bottom_h)
      cropped_image[y_slice, x_slice, :] = image[y0:y1, x0:x1, :]
  
      # crop detections
      cropped_detections = detections.copy()
      cropped_detections[:, 0:4:2] -= x0
      cropped_detections[:, 1:4:2] -= y0
      cropped_detections[:, 0:4:2] += cropped_ctx - left_w
      cropped_detections[:, 1:4:2] += cropped_cty - top_h
  
      return cropped_image, cropped_detections, offset_image
  
  def random_crop_pie(image, detections, random_scales, view_size, border=64):
      view_height, view_width   = view_size
      image_height, image_width = image.shape[0:2]
  
      scale  = np.random.choice(random_scales)
      height = int(view_height * scale)
      width  = int(view_width  * scale)
  
      cropped_image = np.zeros((height, width, 3), dtype=image.dtype)
  
      w_border = _get_border(border, image_width)
      h_border = _get_border(border, image_height)
  
      ctx = np.random.randint(low=w_border, high=image_width - w_border)
      cty = np.random.randint(low=h_border, high=image_height - h_border)
  
      x0, x1 = max(ctx - width // 2, 0),  min(ctx + width // 2, image_width)
      y0, y1 = max(cty - height // 2, 0), min(cty + height // 2, image_height)
  
      left_w, right_w = ctx - x0, x1 - ctx
      top_h, bottom_h = cty - y0, y1 - cty
  
      # crop image
      cropped_ctx, cropped_cty = width // 2, height // 2
      x_slice = slice(cropped_ctx - left_w, cropped_ctx + right_w)
      y_slice = slice(cropped_cty - top_h, cropped_cty + bottom_h)
      cropped_image[y_slice, x_slice, :] = image[y0:y1, x0:x1, :]
  
      # crop detections
      cropped_detections = detections.copy()
      cropped_detections[:, 0:6:2] -= x0
      cropped_detections[:, 1:6:2] -= y0
      cropped_detections[:, 0:6:2] += cropped_ctx - left_w
      cropped_detections[:, 1:6:2] += cropped_cty - top_h
  
      return cropped_image, cropped_detections
  
  def random_crop_line(image, detections, random_scales, view_size, border=64):
      view_height, view_width   = view_size
      image_height, image_width = image.shape[0:2]
  
      scale  = np.random.choice(random_scales)
      height = int(view_height * scale)
      width  = int(view_width  * scale)
  
      cropped_image = np.zeros((height, width, 3), dtype=image.dtype)
  
      w_border = _get_border(border, image_width)
      h_border = _get_border(border, image_height)
  
      ctx = np.random.randint(low=w_border, high=image_width - w_border)
      cty = np.random.randint(low=h_border, high=image_height - h_border)
  
      x0, x1 = max(ctx - width // 2, 0),  min(ctx + width // 2, image_width)
      y0, y1 = max(cty - height // 2, 0), min(cty + height // 2, image_height)
  
      left_w, right_w = ctx - x0, x1 - ctx
      top_h, bottom_h = cty - y0, y1 - cty
  
      # crop image
      cropped_ctx, cropped_cty = width // 2, height // 2
      x_slice = slice(cropped_ctx - left_w, cropped_ctx + right_w)
      y_slice = slice(cropped_cty - top_h, cropped_cty + bottom_h)
      cropped_image[y_slice, x_slice, :] = image[y0:y1, x0:x1, :]
  
      # crop detections
      cropped_detections = detections.copy()
      cropped_detections[:, 0:-1:2] -= x0
      cropped_detections[:, 1:-1:2] -= y0
      cropped_detections[:, 0:-1:2] += cropped_ctx - left_w
      cropped_detections[:, 1:-1:2] += cropped_cty - top_h
  
      return cropped_image, cropped_detections, scale
  
  def random_crop_xy(image, image_xy, detections, random_scales, view_size, border=64):
      view_height, view_width   = view_size
      image_height, image_width = image.shape[0:2]
  
      scale  = np.random.choice(random_scales)
      height = int(view_height * scale)
      width  = int(view_width  * scale)
  
      cropped_image = np.zeros((height, width, 3), dtype=image.dtype)
      cropped_xy = np.zeros((height, width, 2), dtype=image_xy.dtype)
      w_border = _get_border(border, image_width)
      h_border = _get_border(border, image_height)
  
      ctx = np.random.randint(low=w_border, high=image_width - w_border)
      cty = np.random.randint(low=h_border, high=image_height - h_border)
  
      x0, x1 = max(ctx - width // 2, 0),  min(ctx + width // 2, image_width)
      y0, y1 = max(cty - height // 2, 0), min(cty + height // 2, image_height)
  
      left_w, right_w = ctx - x0, x1 - ctx
      top_h, bottom_h = cty - y0, y1 - cty
  
      # crop image
      cropped_ctx, cropped_cty = width // 2, height // 2
      x_slice = slice(cropped_ctx - left_w, cropped_ctx + right_w)
      y_slice = slice(cropped_cty - top_h, cropped_cty + bottom_h)
      cropped_image[y_slice, x_slice, :] = image[y0:y1, x0:x1, :]
      cropped_xy[y_slice, x_slice, :] = image_xy[y0:y1, x0:x1, :]
      # crop detections
      cropped_detections = detections.copy()
      cropped_detections[:, 0:4:2] -= x0
      cropped_detections[:, 1:4:2] -= y0
      cropped_detections[:, 0:4:2] += cropped_ctx - left_w
      cropped_detections[:, 1:4:2] += cropped_cty - top_h
  
      return cropped_image, cropped_detections, cropped_xy
  