LegereOCR
Accurate OCR for real-world scans, on CPU — no GPU, no cloud.
Installation · Quick start · See it in action · Key numbers · Documentation · Español
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; scan.jpg is any image or PDF of yours):
from legereocr import Legere
with Legere() as ocr:
print(ocr.text("scan.jpg"))
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.
See it in action
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):
.venv\Scripts\legere.exe enhance ejemplos\factura_foto.jpg -o page.png
.venv\Scripts\legere.exe tables ejemplos\factura_foto.jpg --json
.venv\Scripts\legere.exe structure ejemplos\factura_foto.jpg --json - --json-keys en
See all outputs → One example per output: searchable, presentable and transcribed PDF, Word, Excel, Markdown, translation, SVG, compare, locate, batch split and anonymization.
What it is and why
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:
PDF/image → ingestion (native text layer if present, render otherwise) → preprocessing driven by a diagnosis
→ PP-OCRv5/v6 detection + recognition (ONNX Runtime, OpenVINO or the optional Tesela)
→ orientation, reading order, re-OCR of doubtful lines, confidence filter
→ text · JSON · blocks and fields · tables · PDF · Word · Excel
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 search, 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).
Key numbers
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).
| Function (benchmark, metric) | LegereOCR | PaddleOCR | Ceiling |
|---|---|---|---|
| Reading, 172-image ground truth (CER) | 0.04733 (balanced) |
0.118 (best) · 0.140 (default v6) | — |
Degraded page, heavy (CER) |
0.11001 | 0.31-0.42 | 0.026 (the same page, clean) |
| Clean page (CER) | 0.02603 | 0.032-0.039 (a tie except against v5 mobile) | — |
| XFUND: loose F1 / free-order CER | 0.9233 / 0.0638 | 0.9190 / 0.0880 (F1 tied) | — |
| 13 hard cases | 10/13 | 9/13 (v5 mobile) · 7/13 (v6) | — |
| 180° line orientation (recall) | 1.000 | 0.271 | 1.0 |
| Photos: CER on 3 benchmarks (UVDoc / SmartDoc-QA / CORD) | 0.081 / 0.147 / 0.090 | 0.321 / 0.251 / 0.229 | — |
| Cropped tables, FinTabNet / PubTabNet (TEDS) | 0.804 / 0.899 | 0.663 / 0.736 (v2) | — |
| Tables in the page, OmniDocBench (TEDS, historical sample) | 67.2 (structure 79.6) | 47.7 (not re-measured) | 68.6 |
| Full-page text, OmniDocBench (ED, tuning / test, historical) | 0.046 / 0.055 | 0.078 / 0.091 (not re-measured) | 0.0675 (other variant) |
| Extreme photometry: 132-image set / hard 186-image set (CER) | 0.0068 / 0.1623 by default (0.0964 with the opt-in rescue) | 0.0251 / 0.0928 | 0.0002 / 0.0683 |
| Table merging across pages (P / R) | 1.00 / 1.00 | 0.52 / 1.00 | reached |
| Stamps with arc-shaped text (CER) | 0.284 | 0.813 | — |
| Checkboxes (F1) | 0.993 | 0.069 | — |
| Word: median position error · Excel: cell F1 | 1.9 pt · 0.99 | 123 pt · 0.66 | — |
| Synthetic tables (12): cell-F1 / Jaccard | 0.9939 / 0.9940 | — | — |
| Time per page, ONNX Runtime 1.31 on both sides | 0.844 s (balanced) |
0.880 s (v5 mobile) | — |
Time per page with the rapido extra (OpenVINO) |
0.695 s (balanced) |
— | — |
| Time per page with the optional Tesela engine (separate run: ONNX Runtime 0.719 s, OpenVINO 0.586 s) | 0.427 s (balanced) |
— | — |
| Peak RAM in the ONNX Runtime 1.31 run (0.844 s row) · installed size | 0.538 GB · 242 MB | 1.496 GB · 758 MB | — |
Full public benchmarks with their official evaluators (not a head-to-head with Paddle):
| Benchmark | Result |
|---|---|
| OmniDocBench v1.6, 1651 pages | Text Edit 0.10756; TEDS 73.70; structure 85.41; reading-order Edit 0.19917; formula Edit 0.77639; 3.4 s/page |
| OmniDocBench with CDM | Overall 65.18 by default and 76.26 with the opt-in formula module (LEGERE_FORMULAS=1) |
| olmOCR-bench, 1403 PDFs, 8413 tests | base 45.0% ± 1.0%; rasterized 46.7% ± 0.9% |
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.
Installation
Requires Python 3.10 or newer; development and measurements use 3.12 (onnxruntime 1.31 needs 3.11+: on 3.10 pip
installs the latest version it can find). Commands are PowerShell (Windows); on Linux they are analogous
(python3.12, .venv/bin/).
py -3.12 -m venv .venv
.venv\Scripts\python.exe -m pip install -U pip
.venv\Scripts\python.exe -m pip install legereocr
With extras: pip install "legereocr[rapido,word,excel]".
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):
.venv\Scripts\python.exe -m legereocr.motores.descarga
They are stored in modelos/ (or in the folder named by LEGERE_MODELOS). To try it on the synthetic scanned PDFs in
ejemplos/ (clone the repository to get them):
.venv\Scripts\legere.exe info
.venv\Scripts\legere.exe ocr ejemplos\acta_inicio_escaneada.pdf --summary
.venv\Scripts\legere.exe markdown ejemplos\registro_tabla_escaneado.pdf --tables
.venv\Scripts\legere.exe pdf ejemplos\certificado_presupuestal_escaneado.pdf
Optional extras (pip install "legereocr[extra]"):
| extra | what for |
|---|---|
rapido |
OpenVINO engine (f32): same text; 0.695 vs 0.844 s/page with ORT |
word / excel |
legere docx / legere xlsx (python-docx / openpyxl) |
beam |
beam decoding with hotwords and LM (pyctcdecode; kenlm separately) |
codigos |
QR and barcodes (legere ocr --barcodes; zxing-cpp) |
heic |
iPhone HEIC/HEIF photos |
gpu |
DirectML (Windows) or CUDA; replaces the CPU onnxruntime |
traducir |
local document translation (docs/TRADUCCION.md) |
servicio |
HTTP service with a job queue (legere-servicio); legere-mcp (MCP) does not need it |
dev |
pytest and audit tooling (rapidfuzz is now a base dependency; benchmark is an empty compatibility extra) |
Quick start
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 types (chart.tabla(), kind/values fields named tipo/valores) are still Spanish only.
By default the engine runs plain and general: it assumes no domain language, document type or data to look for.
from legereocr import Legere
with Legere() as ocr: # profile "auto": adapts to the hardware
text = ocr.text("document.pdf") # plain text in reading order
doc = ocr.analyze("scan.jpg") # Document: lines, boxes, confidences, order
chart = ocr.read_chart("bars.png") # chart type and values with confidence; `chart.tabla()` = [[category, series, value]]
print(doc.pages[0].lines[0].text)
legere text document.pdf # the same from the terminal
legere pdf scan.pdf # searchable PDF (the image is left untouched)
legere layout document.pdf # text with spatial layout, ideal for LLMs
legere markdown document.pdf --tables # semantic Markdown: headings, fields, real tables
legere tables scan.pdf --md # tables as records (headers + rows), no re-OCR
legere xlsx scan.pdf -o tables.xlsx # tables → Excel (cells rebuilt from the real lines)
legere docx scan.pdf -o out.docx # rebuild as EDITABLE Word: paragraphs + native tables + images
legere pdf scan.pdf --presentable --mrc --pdfa # rebuilt PDF + MRC-lite compression + PDF/A-2B
legere pdf scan.pdf --transcribed # "born-digital" style PDF: real selectable text + bold titles; complex tables as an
# image with invisible text; signatures, stamps, logos and photos as images
legere svg photo.jpg -o photo.svg # VECTOR output: letters traced with Bezier curves, not a pasted image;
# --mode calco|calco_buscable|mixto, --pdf for a vector PDF
# (limits LEGERE_SVG_MAX_CONTORNOS/CURVAS/SEGUNDOS/BYTES; see docs/CONFIGURACION.md)
legere segments scan.pdf --json seg.json # translation hook (NO model): paragraphs, titles and cells with id,
# text, box and "do not translate" spans (numbers, amounts, dates, codes, URLs)
legere reinsert scan.pdf trans.json -o es_en.docx # {id: translated text} -> .md / .json / .docx / transcribed PDF with the layout
legere hocr document.pdf # hOCR 1.2 for archive tools (and `legere alto`: ALTO v4)
legere locate scan.jpg --value 202600000926 # is what the LLM said really there? (or --extraction llm.json) page, box (px and PDF pt) or "absent"
legere pdf scan.pdf -o readable.pdf --manifest # + readable.pdf.manifiesto.json: sha256 of source/output/models, profile
legere verify readable.pdf.manifiesto.json # recomputes the sha256 against disk (exit code 1 if anything changed)
legere info # hardware, chosen profile and optional capabilities
The Spanish command names (legere texto, legere tablas, legere excel…) are aliases of the English ones.
Outputs
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 6 GB or more (below that, light); accurate is the highest-quality profile. 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.
Inference engines
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.
| Engine | How to select it | Time per page | Peak RAM |
|---|---|---|---|
| ONNX Runtime (f32) | default | 0.719 s | 522 MB |
| OpenVINO (f32) | rapido extra |
0.586 s | 1622 MB |
| Tesela (f32), optional | LEGERE_MOTOR=pagina, automatic without AVX512_BF16 |
0.476 s | 664 MB |
| Tesela (bf16), optional | LEGERE_MOTOR=pagina |
0.427 s | 689 MB |
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, build its Python extension (module tesela, Python 3.12 or later, a recent stable Rust toolchain) in the
same virtual environment as Legere; there is no prebuilt package:
git clone https://github.com/tesela-engine/tesela
cd tesela\crates\py
python -m pip install maturin
maturin develop --release
$env:LEGERE_MOTOR = "pagina" # then run Legere as usual
Tesela's own README explains the build and how to reproduce its measurements.
- Default:
LEGERE_MOTOR=autonever loads Tesela. It uses OpenVINO if therapidoextra 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.
Optional domains
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=.
ocr = Legere(domain="es-legal") # explicit opt-in
ocr = Legere(hotwords=["ACME S.A.S.", "EXP-2026-001"]) # your own expected words
ocr = Legere(lm_path="my_model.bin") # your own KenLM language model
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…).
Documentation
The detailed documentation is in Spanish.
| Document | Contents |
|---|---|
| docs/COMPARATIVA-PADDLEOCR.md | Function-by-function comparison with PaddleOCR 3.7: reading, structure, tables, outputs, speed |
| docs/CONFIGURACION.md | Every LEGERE_* variable and the Legere() parameters |
| docs/API-EN.md | English API: Python, CLI, HTTP and MCP, with the Spanish equivalents |
| docs/GUIA-SACARLE-EL-100.md | Profiles, engine, parallelism, containers, an LLM on top |
| docs/TRADUCCION.md | Local document translation (opt-in) |
| docs/WORD-EXCEL.md | Word and Excel reconstruction |
| docs/DISENO.md | Design: architecture and technical decisions |
| docs/INVENTARIO.md | Feature inventory |
| benchmark/README.md | Benchmarks and how to reproduce the measurements |
| SEGURIDAD.md | Threat model and defenses against hostile documents (Spanish) |
| SECURITY.md | How to report a vulnerability |
| CONTRIBUTING.md · CHANGELOG.md | How to contribute · change history |
| LICENSES.md | Licenses of models, ported code, resources and fonts |
Security
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.
Author
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.
License
LegereOCR is free software under the Apache License 2.0, © 2026 Cristian Camilo Robledo Cuellar, and includes
some third-party MIT-licensed code (see NOTICE). You may use, modify and redistribute it, commercially too,
keeping LICENSE and NOTICE.
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.
Metadata
Release files for legereocr 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| legereocr-0.3.0.tar.gz | 2.4 MB | Without provenance | Details for legereocr-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| legereocr-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for legereocr-0.3.0-py3-none-any.whl |
Total release size: 4.0 MB
Release files / legereocr-0.3.0.tar.gz
| Download URL | legereocr-0.3.0.tar.gz |
|---|---|
| Size | 2.4 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
400d8e44bf85ea6018071fcc960f0e2b5f6288892f6880ef29380857b0ba2714
|
|
BLAKE2b-256 checksum How to use checksums |
73558ff90b5b116d73df3dbd6dd6c1d0cd227a46c15b70a14582744b2b0cd0cc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.7
|
Release files / legereocr-0.3.0-py3-none-any.whl
| Download URL | legereocr-0.3.0-py3-none-any.whl |
|---|---|
| Size | 1.6 MB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ab1927c05dba86cbdec0b42ea8e46c13599c80de8ef08c930fc8023081922e38
|
|
BLAKE2b-256 checksum How to use checksums |
af5ace2631d750d662d758cb370a7688e0570f88d5f809b598bf728bd5b97eb5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.7
|
Security for legereocr 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.