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_krectangle 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 withtorch.nn.functional.grid_sample().w_kkeeps the quad’s aspect ratio (clamped tomax_width); columns pastw_kare set tofill. Quads whose height is at leastvertical_ratiotimes 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 (pixeljspans[j, j + 1)), in reading order – useorder_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.
- torchocr.ops.polygon_area(polys)[source]#
Unsigned area of simple polygons via the shoelace formula.
- 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_overlapof 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.