ocracy.base#
Core types and normalized result objects for ocracy.
OCR engines disagree wildly on what they return: Tesseract emits TSV rows of words with pixel boxes and confidences; cloud APIs return nested JSON of pages/blocks/paragraphs/words; VLM-based engines often return just a Markdown string. ocracy normalizes all of that into a small, stable set of dataclasses so that callers get the same shape regardless of which backend produced the text:
OcrResult— the full result of reading an image: a concatenatedtext(in reading order) plus a list of structuredblockscarrying bounding boxes and confidences, plus the untouchedrawbackend output for power users who need engine-specific detail.TextBlock— one recognized unit of text (word/line/paragraph/block) with an optionalBBoxandconfidence.BBox— an axis-aligned bounding box, with an optionalpolygonfor rotated or quadrilateral regions (common in scene-text engines).
The input side is normalized too: every facade function accepts an
ImageInput — a path, URL, bytes, PIL image, or numpy array. Concrete
decoding happens lazily in ocracy.util, so importing ocracy never
requires Pillow or numpy.
The design goal is progressive disclosure: str(result) gives you the text,
result.text is the same string, iterating the result yields its lines, and
result.blocks / result.raw are there when you need structure.
Classes
|
An axis-aligned bounding box in pixel coordinates (origin = top-left). |
|
The normalized result of reading an image with any backend. |
|
One recognized unit of text. |
- class ocracy.base.BBox(x0, y0, x1, y1, polygon=None)[source]#
Bases:
objectAn axis-aligned bounding box in pixel coordinates (origin = top-left).
polygonoptionally carries the original (possibly rotated) vertices as a sequence of(x, y)points; the axis-alignedx0/y0/x1/y1are always populated (derived from the polygon if a backend only gives one).- property as_tuple: Tuple[float, float, float, float]#
(x0, y0, x1, y1)— the convention PIL’scropexpects.
- classmethod from_polygon(points)[source]#
Build a box from polygon vertices
[(x, y), ...].The axis-aligned extent is computed from the vertices and the original polygon is preserved for callers that care about rotation.
- Return type:
- class ocracy.base.OcrResult(text, blocks=list(), backend='', raw=None, meta=dict())[source]#
Bases:
objectThe normalized result of reading an image with any backend.
textis the headline payload: the full recognized text in reading order.blockscarries the structured units (with boxes/confidences) when the backend provides them.rawis the untouched backend output.metaholds cross-cutting extras (languages, page count, a Markdown rendering, timing, …).Progressive disclosure:
result = ocracy.ocr("scan.png") print(result) # -> the text result.text # -> the same string for line in result: # -> iterate TextBlocks (lines by default) print(line.text, line.confidence) result.words # -> only word-level blocks result.mean_confidence # -> average confidence, if available result.raw # -> engine-specific structure
- filter_confidence(min_confidence)[source]#
Return a copy keeping only blocks at or above
min_confidence.Blocks without a confidence are dropped.
textis rebuilt from the surviving blocks (joined by newline).- Return type:
- classmethod from_blocks(blocks, *, backend='', raw=None, text=None, joiner='\n', **meta)[source]#
Build a result from structured blocks.
If
textis not given it is synthesized by joining the blocks’ text in their given order withjoiner(callers should pass blocks already in reading order, or pre-join and passtextexplicitly).- Return type:
- classmethod from_text(text, *, backend='', raw=None, **meta)[source]#
Build a minimal result from just a text string (no geometry).
- Return type:
- property lines: List[TextBlock]#
The blocks at
linelevel (empty if the backend reports another level).