torchocr.datasets#

Datasets return (uint8 image, TextDetectionTarget) pairs (detection) or (image, text) pairs (word crops). None of them downloads anything: point them at the extracted archives.

class torchocr.datasets.TextDetectionTarget(polygons, texts, ignore)[source]#

Bases: object

Ground-truth text regions for one image.

Parameters:
polygons#

(N, P, 2) float vertices in pixel (x, y) order. ICDAR-2015 regions are quadrilaterals (P = 4).

Type:

torch.Tensor

texts#

N transcriptions, aligned with polygons.

Type:

list[str]

ignore#

(N,) bool mask of don’t-care regions (### in ICDAR files). Evaluators exclude them from both recall and precision.

Type:

torch.Tensor

class torchocr.datasets.ICDAR2015(root, split='test', transforms=None)[source]#

Bases: Dataset[tuple[Tensor, TextDetectionTarget]]

ICDAR 2015 Incidental Scene Text, Task 4.1 (text localization).

1000 training and 500 test images of street-level scenes captured with wearable cameras; text regions are word-level quadrilaterals.

The dataset requires registration, so it cannot be downloaded automatically. Get the four archives from https://rrc.cvc.uab.es/?ch=4&com=downloads and extract each into a folder of the same name under root:

root/
    ch4_training_images/                          img_1.jpg ...
    ch4_training_localization_transcription_gt/   gt_img_1.txt ...
    ch4_test_images/                              img_1.jpg ...
    Challenge4_Test_Task1_GT/                     gt_img_1.txt ...
Parameters:
Returns:

(image, target) where image is a (3, H, W) uint8 RGB tensor and target a TextDetectionTarget with (N, 4, 2) polygons in original-image pixels.

load_target(index)[source]#

The annotations of sample index, without decoding its image.

Parameters:

index (int)

Return type:

TextDetectionTarget

class torchocr.datasets.TotalText(root, split='test', transforms=None)[source]#

Bases: Dataset[tuple[Tensor, TextDetectionTarget]]

Total-Text (Ch’ng & Chan, ICDAR 2017): 1255 train / 300 test images of horizontal, multi-oriented and curved scene text, annotated with word-level polygons of 3 to 19 vertices.

Expected layout (the released images plus the txt_format ground truth):

root/
    Images/Train/img11.jpg ...          Images/Test/img1.jpg ...
    txt_format/Train/poly_gt_img11.txt  txt_format/Test/poly_gt_img1.txt

Polygons are padded to the largest vertex count of their image by repeating their last vertex, which changes neither area, IoU nor rasterization, so they fit one (N, P, 2) tensor. Words transcribed # (illegible) or with an empty transcription, and polygons with fewer than 3 vertices, are marked ignore.

Images are read without applying EXIF orientation: the annotations follow the stored pixels (checked on the six EXIF-rotated images).

Parameters:
Returns:

(3, H, W) uint8 RGB and a TextDetectionTarget with (N, P, 2) polygons.

Return type:

(image, target)

load_target(index)[source]#

The annotations of sample index, without decoding its image.

Parameters:

index (int)

Return type:

TextDetectionTarget

class torchocr.datasets.FUNSD(root, split='test', transforms=None)[source]#

Bases: Dataset[tuple[Tensor, TextDetectionTarget]]

FUNSD (Jaume et al., ICDAR-OST 2019): 149 train / 50 test noisy scanned business forms with word-level boxes and transcriptions.

A document benchmark, unlike ICDAR-2015 and Total-Text: use it to check how a scene-text model transfers to scans. Words with an empty transcription (check boxes, rules) are marked ignore.

Download and unzip https://guillaumejaume.github.io/FUNSD/dataset.zip (no registration); root is the extracted dataset folder:

root/training_data/{images,annotations}/  root/testing_data/{images,annotations}/
Parameters:
Returns:

(3, H, W) uint8 RGB (scans are grayscale, replicated to three channels) and a TextDetectionTarget of axis-aligned (N, 4, 2) word quads.

Return type:

(image, target)

load_target(index)[source]#

The annotations of sample index, without decoding its image.

Parameters:

index (int)

Return type:

TextDetectionTarget

class torchocr.datasets.LMDBWords(root, label_filter=None, transform=None)[source]#

Bases: Dataset[tuple[Tensor, str]]

Cropped word images in the LMDB layout of clovaai’s deep-text-recognition-benchmark.

This is how the standard recognition corpora circulate (MJSynth, SynthText, IIIT5K, SVT, ICDAR 2013/2015 crops, CUTE80, MLT word crops): num-samples holds the count and samples i = 1..N are stored under image-%09d (encoded image bytes) and label-%09d (UTF-8 text).

Requires the optional lmdb package (pip install torchocr[lmdb]).

Parameters:
  • root (str | Path) – Directory containing data.mdb.

  • label_filter (Callable[[str], bool] | None) – Optional predicate on the label; only matching samples are kept. Evaluated once on all labels at construction (images are not decoded), e.g. to drop words outside a model’s charset.

  • transform (Callable[[Tensor], Tensor] | None) – Optional callable applied to the uint8 image.

Returns:

a (3, H, W) uint8 RGB crop and its label.

Return type:

(image, text)