class_docstr: Randomly select a region and crop images to a target size and make sure
  to contain text region. This transform may break up text instances, and for
  broken text instances, we will crop it's bbox and polygon coordinates. This
  transform is recommend to be used in segmentation-based network.
  
  Required Keys:
  
  - img
  - gt_polygons
  - gt_bboxes
  - gt_bboxes_labels
  - gt_ignored
  
  Modified Keys:
  
  - img
  - img_shape
  - gt_polygons
  - gt_bboxes
  - gt_bboxes_labels
  - gt_ignored
  
  Args:
      target_size (tuple(int, int) or int): Target size for the cropped
          image. If it's a tuple, then target width and target height will be
          ``target_size[0]`` and ``target_size[1]``, respectively. If it's an
          integer, them both target width and target height will be
          ``target_size``.
      positive_sample_ratio (float): The probability of sampling regions
          that go through text regions. Defaults to 5. / 8.
docstr: Samples the top-left coordinate of a crop region, ensuring that the
  cropped region contains at least one polygon.
  
  Args:
      gt_polygons (list(ndarray)) : Polygons.
      img_size (tuple(int, int)) : Image size in the format of
          (height, width).
  
  Returns:
      tuple(int, int): Top-left coordinate of the cropped region.
docstr_tok: ["Samples","the","top-left","coordinate","of","a","crop","region",",","ensuring","that","the","cropped","region","contains","at","least","one","polygon",".","Args",":","gt_polygons","(","list","(","ndarray",")",")",":","Polygons",".","img_size","(","tuple","(","int",",","int",")",")",":","Image","size","in","the","format","of","(","height",",","width",")",".","Returns",":","tuple","(","int",",","int",")",":","Top-left","coordinate","of","the","cropped","region","."]
code: def _sample_offset(
      self, gt_polygons: Sequence[np.ndarray], img_size: Tuple[int, int]
  ) -> Tuple[int, int]:
      """Samples the top-left coordinate of a crop region, ensuring that the
      cropped region contains at least one polygon.
  
      Args:
          gt_polygons (list(ndarray)) : Polygons.
          img_size (tuple(int, int)) : Image size in the format of
              (height, width).
  
      Returns:
          tuple(int, int): Top-left coordinate of the cropped region.
      """
      h, w = img_size
      t_w, t_h = self.target_size
  
      # target size is bigger than origin size
      t_h = t_h if t_h < h else h
      t_w = t_w if t_w < w else w
      if (
          gt_polygons is not None
          and len(gt_polygons) > 0
          and self._get_postive_prob() < self.positive_sample_ratio
      ):
          # make sure to crop the positive region
  
          # the minimum top left to crop positive region (h,w)
          tl = np.array([h + 1, w + 1], dtype=np.int32)
          for gt_polygon in gt_polygons:
              temp_point = np.min(gt_polygon.reshape(2, -1), axis=1)
              if temp_point[0] <= tl[0]:
                  tl[0] = temp_point[0]
              if temp_point[1] <= tl[1]:
                  tl[1] = temp_point[1]
          tl = tl - (t_h, t_w)
          tl[tl < 0] = 0
          # the maximum bottum right to crop positive region
          br = np.array([0, 0], dtype=np.int32)
          for gt_polygon in gt_polygons:
              temp_point = np.max(gt_polygon.reshape(2, -1), axis=1)
              if temp_point[0] > br[0]:
                  br[0] = temp_point[0]
              if temp_point[1] > br[1]:
                  br[1] = temp_point[1]
          br = br - (t_h, t_w)
          br[br < 0] = 0
  
          # if br is too big so that crop the outside region of img
          br[0] = min(br[0], h - t_h)
          br[1] = min(br[1], w - t_w)
          #
          h = self._sample_num(tl[0], br[0]) if tl[0] < br[0] else 0
          w = self._sample_num(tl[1], br[1]) if tl[1] < br[1] else 0
      else:
          # make sure not to crop outside of img
  
          h = self._sample_num(0, h - t_h) if h - t_h > 0 else 0
          w = self._sample_num(0, w - t_w) if w - t_w > 0 else 0
  
      return (h, w)
  
imports: import math
  import random
  from typing import Dict, List, Sequence, Tuple, Union
  import cv2
  import mmcv
  import numpy
  from mmcv.transforms import RandomFlip
  from mmcv.transforms.base import BaseTransform
  from mmcv.transforms.utils import avoid_cache_randomness, cache_randomness
  from shapely.geometry import Polygon
  from mmocr.registry import TRANSFORMS
  from mmocr.utils import crop_polygon, poly2bbox, poly_intersection
label_desc: Use image node_id 4 for calling the TextDetRandomCrop obj's underlying member method code with example usage: obj._sample_offset(gt_polygons, img_size) and returns: h,  w