doctr.io#
The io module enables users to easily access content from documents and export analysis results to structured formats.
Document structure#
Structural organization of the documents.
Word#
A Word is an uninterrupted sequence of characters.
- class Word(value, confidence, geometry, objectness_score, crop_orientation)[source]#
Implements a word element
- Parameters:
value (str) – the text string of the word
confidence (float) – the confidence associated with the text prediction
geometry (tuple[tuple[float, float], tuple[float, float]] | ndarray) – bounding box of the word in format ((xmin, ymin), (xmax, ymax)) where coordinates are relative to
size (the page's)
objectness_score (float) – the objectness score of the detection
crop_orientation (dict[str, Any]) – the general orientation of the crop in degrees and its confidence
Line#
A Line is a collection of Words aligned spatially and meant to be read together (on a two-column page, on the same horizontal, we will consider that there are two Lines).
- class Line(words, geometry=None, objectness_score=None)[source]#
Implements a line element as a collection of words
- Parameters:
Artefact#
An Artefact is a non-textual element (e.g. QR code, picture, chart, signature, logo, etc.).
Block#
A Block is a collection of Lines (e.g. an address written on several lines) and Artefacts (e.g. a graph with its title underneath).
- class Block(lines=[], artefacts=[], geometry=None, objectness_score=None)[source]#
Implements a block element as a collection of lines and artefacts
- Parameters:
geometry (tuple[tuple[float, float], tuple[float, float]] | ndarray | None) – bounding box of the word in format ((xmin, ymin), (xmax, ymax)) where coordinates are relative to the page’s size. If not specified, it will be resolved by default to the smallest bounding box enclosing all lines and artefacts in it.
Page#
A Page is a collection of Blocks that were on the same physical page.
- class Page(page, blocks, page_idx, dimensions, orientation=None, language=None)[source]#
Implements a page element as a collection of blocks
- Parameters:
page (ndarray) – image encoded as a numpy array in uint8
page_idx (int) – the index of the page in the input raw document
dimensions (tuple[int, int]) – the page size in pixels in format (height, width)
orientation (dict[str, Any] | None) – a dictionary with the value of the rotation angle in degress and confidence of the prediction
language (dict[str, Any] | None) – a dictionary with the language value and confidence of the prediction
Document#
A Document is a collection of Pages.
File reading#
High-performance file reading and conversion to processable structured data.
- read_pdf(file, scale=2, rgb_mode=True, password=None, **kwargs)[source]#
Read a PDF file and convert it into an image in numpy format
>>> from doctr.io import read_pdf >>> doc = read_pdf("path/to/your/doc.pdf")
- Parameters:
- Returns:
the list of pages decoded as numpy ndarray of shape H x W x C
- Return type:
- read_img_as_numpy(file, output_size=None, rgb_output=True)[source]#
Read an image file into numpy format
>>> from doctr.io import read_img_as_numpy >>> page = read_img_as_numpy("path/to/your/doc.jpg")
- Parameters:
- Returns:
the page decoded as numpy ndarray of shape H x W x 3
- Return type:
- decode_img_as_tensor(img_content, dtype=torch.float32)[source]#
Read a byte stream as a PyTorch tensor
- read_html(url, **kwargs)[source]#
Read a PDF file and convert it into an image in numpy format
>>> from doctr.io import read_html >>> doc = read_html("https://www.yoursite.com")
- class DocumentFile[source]#
Read a document from multiple extensions
- classmethod from_pdf(file, **kwargs)[source]#
Read a PDF file
>>> from doctr.io import DocumentFile >>> doc = DocumentFile.from_pdf("path/to/your/doc.pdf")
- classmethod from_url(url, **kwargs)[source]#
Interpret a web page as a PDF document
>>> from doctr.io import DocumentFile >>> doc = DocumentFile.from_url("https://www.yoursite.com")
- classmethod from_images(files, **kwargs)[source]#
Read an image file (or a collection of image files) and convert it into an image in numpy format
>>> from doctr.io import DocumentFile >>> pages = DocumentFile.from_images(["path/to/your/page1.png", "path/to/your/page2.png"])
- Parameters:
- Returns:
the list of pages decoded as numpy ndarray of shape H x W x 3
- Return type: