Skip to main content

Convert between OCR output formats via a neutral intermediate model.

Project description

ocronverter

PyPI Python versions CI License: MIT Ruff

Convert between OCR output formats through a single neutral intermediate model.

<provider data> --parse--> Document (neutral) --emit--> <provider data>

Instead of writing an N×N matrix of direct converters, every format has one parser (provider → Document) and one emitter (Document → provider). Any source can then be converted to any target.

Installation

pip install ocronverter

Requires Python 3.10+. The library is pure Python with no runtime dependencies.

Supported formats

Engine Canonical name Aliases Data shape
Google Cloud Vision google_vision google, googlevision, vision, gcv JSON
AWS Textract textract aws, aws_textract JSON
Tesseract (image_to_data) tesseract tess, tsv JSON dict / TSV
Azure AI Document Intelligence azure azure_di, document_intelligence, form_recognizer, di JSON
hOCR hocr ocr_html XHTML string
EasyOCR (readtext) easyocr easy_ocr, easy list of detections

Format names are case-insensitive and -/_ interchangeable. ocronverter.list_formats() returns the canonical list at runtime.

Usage

import ocronverter

# Parse provider output into the neutral Document
doc = ocronverter.parse(data, "google_vision")

# Emit the neutral Document as another provider's format
out = ocronverter.emit(doc, "textract")

# Or do both in one step
out = ocronverter.convert(data, "google_vision", "textract")
out = ocronverter.convert(data, "gcv", "hocr", as_json=False)   # aliases OK

data may be a dict, a JSON string, a Python list (EasyOCR's native detections shape), or an hOCR/XHTML string. convert returns a dict (or a list for EasyOCR, or a str for hOCR); pass as_json=True for a JSON string where the target is JSON-based.

Line synthesis (split_lines)

Google Vision groups by paragraph and has no real line level, while line-oriented targets (Textract, Tesseract, Azure, hOCR, EasyOCR) read best with one entry per visual line. convert(..., split_lines=...) controls this:

  • "auto" (default) — split only when going from a paragraph source to a line-oriented target.
  • True / False — force on or off.

The neutral model

Document
 └─ Page[]        size + rotation; the coordinate anchor
     └─ Block[]   ~ paragraph / region
         └─ Line[]
             └─ Word[]
                 └─ Symbol[]   (optional; char/glyph level)

Conventions:

  • Every level is optional. Parsers fill only what the source provides; the library never invents levels on parse. Missing levels are synthesized on demand via synth.py (opt-in).
  • Geometry is stored normalized (0.0–1.0 of the page) as both an axis-aligned bbox and a polygon, kept in sync. Page.width/height/unit anchor denormalization back to provider pixels (or Azure inches).
  • Confidence is always stored 0.0–1.0 internally; parsers rescale.
  • provider_meta on every node carries source-specific fields verbatim, so a same-format round-trip is best-effort lossless.

Format-specific notes

  • Tesseract / hOCR have a paragraph level the model lacks; it is carried on Line.provider_meta (par_num / par_id) and regrouped on emit.
  • Azure uses flat per-page words[]/lines[] lists whose nesting is reconstructed from character span offsets, and may use inch page units.
  • hOCR is an XHTML string: geometry and confidence live in each element's title attribute (bbox, x_wconf). Optional char-level ocrx_cinfo is not modeled (no glyph level).
  • EasyOCR is a flat [polygon, text, confidence] list with no hierarchy and no page dimensions. Each detection becomes a Line + single Word; page dims are inferred from the max extent of all polygons (pass a {"width", "height", "results"} wrapper to supply true dims).

Tests

python -m pytest tests/

Each format has a round-trip test (provider → Document → provider) plus cross-format bridge tests. Tests are stdlib-only and need no OCR binaries, except test_real_tesseract, which runs only if the tesseract binary and PIL are installed.

License

MIT — see LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ocronverter-0.1.1.tar.gz (134.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ocronverter-0.1.1-py3-none-any.whl (35.0 kB view details)

Uploaded Python 3

File details

Details for the file ocronverter-0.1.1.tar.gz.

File metadata

  • Download URL: ocronverter-0.1.1.tar.gz
  • Upload date:
  • Size: 134.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for ocronverter-0.1.1.tar.gz
Algorithm Hash digest
SHA256 5dbbb123c48e2b89028fa3c335743c29854252d33c712d57b0e3c8bc4afe3a6f
MD5 0b205f140df5f6a19907fc03f81dc789
BLAKE2b-256 29a5f273ecabb798e5b6625a380f24a13ad49cda044c0a4d892fe6f4f45c27bd

See more details on using hashes here.

Provenance

The following attestation bundles were made for ocronverter-0.1.1.tar.gz:

Publisher: publish.yml on gxlarson/ocronverter

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ocronverter-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: ocronverter-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 35.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for ocronverter-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 43b849d323d3d4743b428f952cd06842432fee60acd90d21a634314e1c5f8cd7
MD5 23bab940749ce234c68e0d10a489f2c7
BLAKE2b-256 b2e6947482edd487eb2b7fade22bb538111059b14db50dac13c0d0e00d203238

See more details on using hashes here.

Provenance

The following attestation bundles were made for ocronverter-0.1.1-py3-none-any.whl:

Publisher: publish.yml on gxlarson/ocronverter

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page