class_docstr: Implements a post processor for DBNet adapted from the implementation of `xuannianz
  <https://github.com/xuannianz/DifferentiableBinarization>`_.
  
  Args:
  ----
      unclip ratio: ratio used to unshrink polygons
      min_size_box: minimal length (pix) to keep a box
      max_candidates: maximum boxes to consider in a single page
      box_thresh: minimal objectness score to consider a box
      bin_thresh: threshold used to binzarized p_map at inference time
class_docstr_tok: ["Implements","a","post","processor","for","DBNet","adapted","from","the","implementation","of","`","xuannianz","<","https",":","\/\/github.com\/xuannianz\/DifferentiableBinarization",">","`","_",".","Args",":","--","--","unclip","ratio",":","ratio","used","to","unshrink","polygons","min_size_box",":","minimal","length","(","pix",")","to","keep","a","box","max_candidates",":","maximum","boxes","to","consider","in","a","single","page","box_thresh",":","minimal","objectness","score","to","consider","a","box","bin_thresh",":","threshold","used","to","binzarized","p_map","at","inference","time"]
docstr: Expand a polygon (points) by a factor unclip_ratio, and returns a polygon
  
  Args:
  ----
      points: The first parameter.
  
  Returns:
  -------
      a box in absolute coordinates (xmin, ymin, xmax, ymax) or (4, 2) array (quadrangle)
docstr_tok: ["Expand","a","polygon","(","points",")","by","a","factor","unclip_ratio",",","and","returns","a","polygon","Args",":","--","--","points",":","The","first","parameter",".","Returns",":","--","--","--","-","a","box","in","absolute","coordinates","(","xmin",",","ymin",",","xmax",",","ymax",")","or","(","4",",","2",")","array","(","quadrangle",")"]
code: def polygon_to_box(
      self,
      points: np.ndarray,
  ) -> np.ndarray:
      """Expand a polygon (points) by a factor unclip_ratio, and returns a polygon
  
      Args:
      ----
          points: The first parameter.
  
      Returns:
      -------
          a box in absolute coordinates (xmin, ymin, xmax, ymax) or (4, 2) array (quadrangle)
      """
      if not self.assume_straight_pages:
          # Compute the rectangle polygon enclosing the raw polygon
          rect = cv2.minAreaRect(points)
          points = cv2.boxPoints(rect)
          # Add 1 pixel to correct cv2 approx
          area = (rect[1][0] + 1) * (1 + rect[1][1])
          length = 2 * (rect[1][0] + rect[1][1]) + 2
      else:
          poly = Polygon(points)
          area = poly.area
          length = poly.length
      distance = (
          area * self.unclip_ratio / length
      )  # compute distance to expand polygon
      offset = pyclipper.PyclipperOffset()
      offset.AddPath(
          points, pyclipper.JT_ROUND, pyclipper.ET_CLOSEDPOLYGON
      )
      _points = offset.Execute(distance)
      # Take biggest stack of points
      idx = 0
      if len(_points) > 1:
          max_size = 0
          for _idx, p in enumerate(_points):
              if len(p) > max_size:
                  idx = _idx
                  max_size = len(p)
          # We ensure that _points can be correctly casted to a ndarray
          _points = [_points[idx]]
      expanded_points: np.ndarray = np.asarray(
          _points
      )  # expand polygon
      if len(expanded_points) < 1:
          return None  # type: ignore[return-value]
      return (
          cv2.boundingRect(expanded_points)  # type: ignore[return-value]
          if self.assume_straight_pages
          else np.roll(
              cv2.boxPoints(cv2.minAreaRect(expanded_points)),
              -1,
              axis=0,
          )
      )
  
file_path: doctr/doctr/models/detection/differentiable_binarization/base.py
imports: from typing import Dict, List, Tuple, Union
  import cv2
  import numpy
  import pyclipper
  from shapely.geometry import Polygon
  from ..core import DetectionPostProcessor
label_desc: Use image node_id 2 for calling the DBPostProcessor obj's underlying member method code with example usage: obj.polygon_to_box(points) and returns: unknown, None