Skip to content
Legereocr

Legereocr

github.com • December 3, 2026

Installation · Quick start · See it in action · Key numbers · Documentation · Español

Legere — Latin: to read .

Legere — Latin: to read .

A general-purpose library that makes any scanned document readable, on CPU: PP-OCRv5/v6 OCR on ONNX Runtime or OpenVINO (no PaddlePaddle; the optional Tesela engine is faster), image enhancement, structured output for LLMs, searchable PDF, Word and Excel.

For developers and teams that need to read scanned documents (contracts, invoices, forms, photos) without a GPU or a cloud service: one pip install , accurate text with positions, and ready-made outputs for LLMs, searchable PDF, Word and Excel. It runs on CPU, handles degraded scans better than PaddleOCR itself and works offline once the models are downloaded.

Minimal example (after the installation steps; run it from the folder that contains modelos/ ). It downloads a real sample, a synthetic phone photo of an invoice, and reads it:

PowerShell (Windows): curl.exe -L -O

Against PaddleOCR 3.7 (same images, same metrics; every row comes from docs/COMPARATIVA-PADDLEOCR.md , the full function-by-function comparison, in Spanish):

Speed is a tie; the gain is accuracy on degraded scans and photos, and Legere loses the last row by default. One machine and one set of benchmarks: details and caveats in Key numbers .

The detailed documentation ( docs/ ) is written in Spanish. The entry-point files (this README, SECURITY.md , CONTRIBUTING.md , CHANGELOG.md , LICENSES.md , NOTICE ) are in English.

The detailed documentation ( docs/ ) is written in Spanish. The entry-point files (this README, SECURITY.md , CONTRIBUTING.md , CHANGELOG.md , LICENSES.md , NOTICE ) are in English.

Real LegereOCR output on ejemplos/factura_foto.jpg , a synthetic phone photo of an invoice: every name and number in it is fictitious, and ejemplos/generar_foto_factura.py rebuilds it. It is one sample, not a benchmark; the measured numbers are in Key numbers .

To reproduce it (the time was measured on a Ryzen 9 9900X):

PowerShell (Windows):

See all outputs → One example per output: searchable, presentable and transcribed PDF, Word, Excel, Markdown, translation, SVG, compare, locate, batch split and anonymization.

PaddleOCR ships the best open-source OCR models, but as a product it leaves a lot on the table. On degraded scans it loses badly: CER 0.31-0.42 on the heavy variant of the 172-image ground truth (see the note under the table), against 0.11 for Legere. Its default path (PaddlePaddle) is 20-40 times slower than its own ONNX Runtime path. And with the server detector and its defaults, 30 of 172 large pages come out empty ( docs/COMPARATIVA-PADDLEOCR.md ).

LegereOCR does not compete with Baidu's models: it uses them better. It does not train new networks; it builds a better inference engine and document pipeline around them:

It is not tied to a document type, language or project. lang= accepts 113 codes (Paddle's ~106 languages plus aliases): 88 are measured and 62 reach level A (CER <= 0.03 on a clean page and <= 0.10 on a degraded one). Anything domain-specific (lexicons, language models, field validators) lives in optional domain packages ; the core behaves the same for invoices, books, receipts or forms.

Four products on a shared core:

OCR : image/PDF → text and JSON with positions. A PDF that already has a text layer uses that layer and its geometry, with no OCR pass.

Image enhancement : dark, skewed or dirty scans → clean images (usable without OCR).

Structured output : layout-preserving text for LLMs, semantic Markdown, JSON of blocks and fields (label → value), tables as records (Markdown, CSV or JSON), Excel, editable Word with native tables, hOCR 1.2 and ALTO v4. Opt-in: bar and pie charts → table.

Searchable PDF : a scan where nothing can be searched → a PDF you can , select and copy from. Three modes: faithful (the untouched scan + an invisible text layer), presentable (corrected image, optional MRC-lite and PDF/A-2B) and transcribed (rebuilt from the recognized text, like a born-digital file).

Same images, ground truth and metrics on both sides ( paddlepaddle 3.3.1 + paddleocr 3.7.0 + paddlex 3.7.2 , engine onnxruntime ; paired 95% CI). Paddle is the reference, not the goal. Methods and ceilings are in docs/COMPARATIVA-PADDLEOCR.md (Spanish).

Full public benchmarks with their official evaluators (not a head-to-head with Paddle):

Which rows you can reproduce. The 172-image ground truth (the reading, heavy and clean rows) is built from synthetic pages and from 112 pages rendered from a private collection of real documents (65% of the bank), which is not published: only aggregate results are. The "13 hard cases" row comes from crops of private documents and reports only pass/fail counts. The XFUND, OmniDocBench, olmOCR, photo and cropped-table (FinTabNet, PubTabNet) rows use public datasets; the 180° orientation, extreme-photometry (132-image set), table-merging, Word/Excel, synthetic-table, stamp and checkbox rows use synthetic data; all of these can be rerun with the scripts in benchmark/ . Timings are the median of three interleaved rounds of 20 pages of the ground-truth bank (a mix of synthetic pages and pages from the private collection), and RAM the maximum of those rounds. They come from one machine (Ryzen 9 9900X, 8 cores, 96 GB, Windows, with other workloads running: indicative times; compare only figures from the same run).

A rerun on a private collection of real documents (32 documents, 117 pages, not published) gave 30/32 read correctly (the other 2 are encrypted PDFs) and 0 pages with errors.

Where Legere loses , stated plainly: chart-to-table quality on ChartQA (it is opt-in and much faster per chart); the hard photometry benchmark by default (0.1623 against 0.0928; with the opt-in rescue, 0.0964, they tie); and formula reading against specialized systems. It has no document VLM like PaddleOCR-VL. On clean forms (XFUND) the advantage shrinks to character errors.

Requires Python 3.10 to 3.14 ; development and measurements use 3.12 (onnxruntime 1.31 needs 3.11+: on 3.10 pip installs the latest version it can find). On Python 3.15 pip install legereocr fails with from versions: none , because onnxruntime does not publish cp315 wheels yet: use 3.14 or earlier until it does.

Tested on Windows and Linux x86-64; macOS is untested. On Apple Silicon you need macOS 14 or newer (on macOS 12 and 13 pip only finds old onnxruntime wheels, 1.19 or older, never tried with Legere; on Python 3.13 none). Intel Macs: Python 3.10 to 3.13.

Linux and macOS (bash or zsh); the later legere and python commands assume this virtual environment is active:

Windows (PowerShell):

With extras: pip install "legereocr[rapido,word,excel]" .

Windows: use a short path. Create the venv in a short folder (for example C:\legere\.venv ) or enable long paths . The onnx dependency ships deeply nested test data, and in a long path pip install fails with WinError 206: The filename or extension is too long .

Windows: use a short path. Create the venv in a short folder (for example C:\legere\.venv ) or enable long paths . The onnx dependency ships deeply nested test data, and in a long path pip install fails with WinError 206: The filename or extension is too long .

The models are not part of the package: they are downloaded from their sources with sha256 verification (this includes det_v6_tiny , the default detector):

PowerShell: .venv\Scripts\python.exe -m legereocr.motores.descarga

They are stored in modelos/ in the current folder , and Legere looks for them there (or in the folder named by LEGERE_MODELOS ). Run your scripts from the same folder, or set LEGERE_MODELOS to an absolute path; otherwise you get ModelosFaltantesError . To try it on the synthetic scanned PDFs of the repository (they are not in the wheel):

PowerShell (Windows):

Optional extras ( pip install "legereocr[extra]" ):

pip install "legereocr[beam]" only works on Python 3.10 to 3.12: pyctcdecode (0.5.0, the latest) requires numpy<2 , so pip downgrades numpy to 1.26 and OpenCV to 4.11. On 3.13 or newer the extra installs nothing, and if you ask for an LM ( domain= , lm_path= , LEGERE_LM ) Legere warns and carries on with the greedy decoder. Beam is opt-in and only matters with a domain LM.

legere-mcp only reads inside the folders you authorize and exits at startup if none is given (for safety). Client config: {"command": "uvx", "args": ["--from", "legereocr", "legere-mcp", "--raiz", "/path/to/documents"]} ; alternatively set LEGERE_MCP_RAICES (folders separated by ; on Windows and : on Linux and macOS).

English API. Every public name of the main API has an English name ( Legere(profile="balanced") , doc.pages , line.text , legere pdf --output , X-Legere-Keys: en …) and the Spanish names stay valid forever. The two are listed side by side in docs/API-EN.md . The chart reader's own type is still Spanish only: chart.tipo , chart.valores , chart.series , chart.categorias , chart.confianza and chart.tabla() (there is no chart.kind ).

Both files are the samples downloaded in Installation . To read a bar or pie chart, pass the image of the chart on its own (the whole image is the chart): chart = ocr.read_chart("my_chart.png") . Then chart.tipo is its type and chart.tabla() is [[category, series, value]] (doubtful values are left out). There is no chart sample in the repository.

The file names below ( document.pdf , scan.pdf , photo.jpg , trans.json ...) stand for your own files. They work the same in bash and in PowerShell, with the virtual environment active (on Windows without activating it, write .venv\Scripts\legere.exe instead of legere ):

The Spanish command names ( legere texto , legere tablas , legere excel …) are aliases of the English ones.

Plain text, JSON with positions, layout text, Markdown, blocks and fields, tables (Markdown/CSV/JSON), Excel, Word, searchable PDF (faithful, presentable, transcribed), vector SVG, hOCR, ALTO, translation segments, and a manifest with sha256 hashes for provenance. Every command accepts --help .

Profiles and hardware

legere info shows the detected hardware, the chosen profile and the optional capabilities. auto resolves to balanced on any machine with 5 GB or more of total RAM, which includes a container's memory limit (below that, light ); accurate is the highest-quality profile. legere info and the legereocr.perfiles log line say why, e.g. auto → minimo: RAM total 4.0 GB < 5 GB . light reads tables with the lighter path (no consensus), so a table can come out with fewer columns than with balanced ; on a 3 to 5 GB machine you can force --perfil balanced (measured: 2.7 GB peak with tables, fine from 3 GB, killed by the OOM killer at 2 GB). All three profiles use the PP-OCRv6 detector det_v6_tiny ; det_server stays available through Legere(det_model="det_server") or LEGERE_DET_MODELO . INT8 quantization was measured and rejected (opt-in only, LEGERE_INT8=1 ). All LEGERE_* variables and the Legere() parameters are in docs/CONFIGURACION.md , and how to get the most out of it (profiles, engine, parallelism, containers) in docs/GUIA-SACARLE-EL-100.md .

Legere runs on ONNX Runtime by default . The rapido extra adds OpenVINO, which LEGERE_MOTOR=auto uses when it is installed. A third engine, Tesela , is optional : a CPU inference engine written in Rust, built for the PP-OCR models Legere runs and for the whole OCR page around them. Legere gives the same results and works the same with or without it; you only need it if you want the extra speed.

balanced profile on a Ryzen 9 9900X with 8 cores, 20 pages, median of three interleaved rounds (machine with other workloads: indicative times). This is a separate run from the Key numbers table, so compare these times only with each other. The f32 row forces f32 on the same CPU; CPUs without AVX-512 were not measured. In that run Tesela takes 41% less time per page than ONNX Runtime and 27% less than OpenVINO.

These times are not the ones in Tesela's own README (0.294 s/page for Tesela and 0.739 s/page for LegereOCR + ONNX Runtime). Those measure the native engine alone on the full page, through Tesela's command line, in a separate run. The times here measure a whole LegereOCR call ( LEGERE_MOTOR=pagina ), including the Python around the engine (ingestion, page orientation, word assembly and result), on 20 images with interleaved rounds. The two sets answer different questions and should not be mixed.

To use Tesela, install its Python extension (module tesela , Python 3.12 or later) in the same virtual environment as Legere. There are wheels for Windows x64 and Linux x64; elsewhere pip tries to build it, which needs a recent stable Rust toolchain:

PowerShell: $env:LEGERE_MOTOR = "pagina" instead of the export line.

Tesela's own README explains how to build it from its repository and how to reproduce its measurements.

Default: LEGERE_MOTOR=auto never loads Tesela. It uses OpenVINO if the rapido extra is installed and ONNX Runtime otherwise. Selecting Tesela without its extension installed stops at startup with a clear error.

Accuracy: on the 172-image ground truth, CER is 0.04731 with Tesela and 0.04733 with ONNX Runtime. 41 of those pages have at least one different line, because re-OCR of doubtful lines is not ported to Tesela.

Fallback: if Tesela fails to start or fails on a page, Legere continues on the normal path.

The mechanism is generic: a domain is pluggable content, not part of the engine, and nothing runs by default. The first one is es-legal (legal/administrative Spanish): contracting-related hotwords and field validators (tax IDs with check digit, dates, amounts in figures and words, budget documents). The domain's KenLM language model is not included in the package: the domain works without it, and you can supply your own with lm_path= .

Hotwords act through the beam decoder: with hotwords= and pyctcdecode installed, the beam rises to dudosas (doubtful lines only) even with no LM; without pyctcdecode , the beam stays off . See docs/CONFIGURACION.md §4.

Anyone can write a domain package (medical, invoices, another language): see legereocr/dominios/ .

PaddleOCR compatibility

legereocr.compat.paddle is a PaddleOCR 3.x adapter on top of the Legere engine (no paddlepaddle ). To migrate, replace from paddleocr import PaddleOCR with from legereocr.compat.paddle import PaddleOCR ; predict(path) and ocr(path) return results with the same keys ( rec_texts , rec_scores , rec_boxes …).

The detailed documentation is in Spanish.

OCR opens untrusted documents. See SEGURIDAD.md (threat model, input limits, output escaping, model hashes, and the recommendation to deploy in a sandbox with bounded resources) and SECURITY.md to report vulnerabilities. with Legere() as ocr: releases the sessions on exit.

Created and maintained by Cristian Camilo Robledo Cuellar · soft-solutions.org · cristiancamilorobledo@gmail.com

Need OCR, document data extraction or an LLM pipeline built for your documents? I build custom software and do consulting: get in touch through the website or by email.

LegereOCR is free software under the Apache License 2.0 ,

The models are not part of the package: they are downloaded from their official sources and keep their authors' licenses (PP-OCR and the other PaddlePaddle models: Apache-2.0; TableFormer: CDLA-Permissive-2.0; Opus-MT: CC-BY-4.0). Code ported from PaddleX (Apache-2.0) and docling-ibm-models (MIT), the bundled resources and the downloaded fonts are detailed in LICENSES.md .

Extracted Entities