torchocr.ops#

torchocr.ops.crop_quads(images, quads, batch_idx, height, max_width, fill=0.0, vertical_ratio=1.5)[source]#

Rectify text quadrilaterals into fixed-height, left-aligned crops.

Each quad is mapped onto a height x w_k rectangle by the homography sending its corners (in reading order: top-left, top-right, bottom-right, bottom-left) to the rectangle’s corners, and sampled bilinearly with torch.nn.functional.grid_sample(). w_k keeps the quad’s aspect ratio (clamped to max_width); columns past w_k are set to fill. Quads whose height is at least vertical_ratio times their width are treated as vertical text and rotated 90 degrees counter-clockwise, as PaddleOCR does.

Everything runs on the input device, is batched across all quads of a page, and is differentiable with respect to images. Because per-pixel normalization commutes with bilinear sampling, normalizing the whole page once and then cropping equals cropping then normalizing each crop.

Parameters:
  • images (Tensor) – (B, C, H, W) float pages.

  • quads (Tensor) – (K, 4, 2) vertices in continuous pixel coordinates (pixel j spans [j, j + 1)), in reading order – use order_quad_vertices() for detector output.

  • batch_idx (Tensor) – (K,) page index of each quad.

  • height (int) – Output height (e.g. 32 for CRNN).

  • max_width (int) – Output width; every crop is padded to it.

  • fill (float) – Value of the padding columns.

  • vertical_ratio (float) – Height / width ratio from which a quad is rotated.

Returns:

(K, C, height, max_width) crops and the (K,) number of valid (non-padding) columns of each.

Return type:

(crops, widths)

torchocr.ops.order_quad_vertices(quads)[source]#

Reorder quad vertices to reading order: top-left, top-right, bottom-right, bottom-left.

Accepts any starting vertex and either winding (e.g. the output of cv2.boxPoints()). Vertices are first sorted clockwise around the centroid; the start is the upper of the two leftmost vertices, which is the reading-order top-left for text rotated by less than 45 degrees.

Parameters:

quads (Tensor) – (..., 4, 2) vertices in (x, y) image coordinates.

Returns:

(..., 4, 2) reordered vertices.

Return type:

Tensor

torchocr.ops.polygon_area(polys)[source]#

Unsigned area of simple polygons via the shoelace formula.

Parameters:

polys (Tensor) – (..., P, 2) vertices in either winding order.

Returns:

(...) areas.

Return type:

Tensor

torchocr.ops.quad_to_box(quads)[source]#

Axis-aligned (x1, y1, x2, y2) hull of (..., 4, 2) quads.

Parameters:

quads (Tensor)

Return type:

Tensor

torchocr.ops.reading_order(quads, min_overlap=0.5)[source]#

Permutation that sorts text regions in reading order: lines top to bottom, words left to right.

Regions are taken by increasing vertical centre; each joins the line whose most recently added region overlaps it vertically by at least min_overlap of the smaller height, else starts a new line. Chaining through the latest region follows slightly slanted lines. Multi-column layouts are read across columns, line by line.

Parameters:
  • quads (Tensor) – (K, P, 2) region vertices (any P) in image coordinates.

  • min_overlap (float) – Vertical overlap, as a fraction of the smaller region height, needed to put two regions on the same line.

Returns:

quads[reading_order(quads)] is sorted.

Return type:

(K,) long indices