class_docstr: Implements a document builder
  
  Args:
  ----
      resolve_lines: whether words should be automatically grouped into lines
      resolve_blocks: whether lines should be automatically grouped into blocks
      paragraph_break: relative length of the minimum space separating paragraphs
      export_as_straight_boxes: if True, force straight boxes in the export (fit a rectangle
          box to all rotated boxes). Else, keep the boxes format unchanged, no matter what it is.
class_docstr_tok: ["Implements","a","document","builder","Args",":","--","--","resolve_lines",":","whether","words","should","be","automatically","grouped","into","lines","resolve_blocks",":","whether","lines","should","be","automatically","grouped","into","blocks","paragraph_break",":","relative","length","of","the","minimum","space","separating","paragraphs","export_as_straight_boxes",":","if","True",",","force","straight","boxes","in","the","export","(","fit","a","rectangle","box","to","all","rotated","boxes",")",".","Else",",","keep","the","boxes","format","unchanged",",","no","matter","what","it","is","."]
docstr: Sort bounding boxes from top to bottom, left to right
  
  Args:
  ----
      boxes: bounding boxes of shape (N, 4) or (N, 4, 2) (in case of rotated bbox)
  
  Returns:
  -------
      tuple: indices of ordered boxes of shape (N,), boxes
          If straight boxes are passed tpo the function, boxes are unchanged
          else: boxes returned are straight boxes fitted to the straightened rotated boxes
          so that we fit the lines afterwards to the straigthened page
docstr_tok: ["Sort","bounding","boxes","from","top","to","bottom",",","left","to","right","Args",":","--","--","boxes",":","bounding","boxes","of","shape","(","N",",","4",")","or","(","N",",","4",",","2",")","(","in","case","of","rotated","bbox",")","Returns",":","--","--","--","-","tuple",":","indices","of","ordered","boxes","of","shape","(","N",",",")",",","boxes","If","straight","boxes","are","passed","tpo","the","function",",","boxes","are","unchanged","else",":","boxes","returned","are","straight","boxes","fitted","to","the","straightened","rotated","boxes","so","that","we","fit","the","lines","afterwards","to","the","straigthened","page"]
code: def _sort_boxes(boxes: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
      """Sort bounding boxes from top to bottom, left to right
  
      Args:
      ----
          boxes: bounding boxes of shape (N, 4) or (N, 4, 2) (in case of rotated bbox)
  
      Returns:
      -------
          tuple: indices of ordered boxes of shape (N,), boxes
              If straight boxes are passed tpo the function, boxes are unchanged
              else: boxes returned are straight boxes fitted to the straightened rotated boxes
              so that we fit the lines afterwards to the straigthened page
      """
      if boxes.ndim == 3:
          boxes = rotate_boxes(
              loc_preds=boxes,
              angle=-estimate_page_angle(boxes),
              orig_shape=(1024, 1024),
              min_angle=5.0,
          )
          boxes = np.concatenate((boxes.min(1), boxes.max(1)), -1)
      return (
          boxes[:, 0]
          + 2 * boxes[:, 3] / np.median(boxes[:, 3] - boxes[:, 1])
      ).argsort(), boxes
  
file_path: doctr/doctr/models/builder.py
imports: from typing import Any, Dict, List, Optional, Tuple
  import numpy
  from scipy.cluster.hierarchy import fclusterdata
  from doctr.io.elements import Block, Document, KIEDocument, KIEPage, Line, Page, Prediction, Word
  from doctr.utils.geometry import estimate_page_angle, resolve_enclosing_bbox, resolve_enclosing_rbbox, rotate_boxes
  from doctr.utils.repr import NestedObject
label_desc: Use image node_id 2 for calling the DocumentBuilder obj's underlying member method code with example usage: obj._sort_boxes(boxes) and returns: unknown,  boxes