ocracy.make_backend#
Abstraction tools for building OCR facades.
Writing a new backend should be mostly declarative. This module supplies the reusable machinery so an adapter is just “call the engine, return normalized blocks”:
BaseOcrAdapter— subclass it and implement_read(); kwarg translation (via the backend’sparam_map) is handled for you.make_block()/as_bbox()— build normalizedTextBlock/BBoxfrom whatever shape the engine returned, including confidence-scale normalization (e.g. Tesseract’s0..100->0..1).scaffold_backend()— generate a newocracy/backends/<id>/package from the template, pre-filled from the ledger entry. This is the one-command way to start a facade for any backend in the catalog.make_test_image()/validate_adapter()— smoke-test an adapter end to end so you know a new facade actually works.
These are the “abstraction tools and skills that know the process” — the
companion SKILL.md walks an agent (or human) through using them.
Functions
|
Build a normalized |
|
Coerce common bbox shapes into a |
|
Normalize a raw confidence to |
|
Create a new |
|
Render a black-on-white test image with |
|
Smoke-test a backend adapter end to end, returning a report dict. |
Classes
|
Optional base class for backend adapters. |
- class ocracy.make_backend.BaseOcrAdapter(config)[source]#
Bases:
objectOptional base class for backend adapters.
Stores the config, builds a kwarg translator from
config['param_map'], and implementsreadas: translate normalized kwargs -> native kwargs ->_read(). Subclasses implement_read()and return anOcrResult.Adapters are not required to subclass this — the registry only needs an
Adapterclass with aread(image, **kwargs)method — but doing so removes the boilerplate.- ADAPTER_KWARGS = ('api_key', 'app_key', 'app_id')#
Keyword arguments a remote adapter reads itself (credentials), passed through untranslated: no
param_mapdeclares them, and they must never be dropped. On a local backend they are dropped with a (redacted) note.
- ocracy.make_backend.as_bbox(obj)[source]#
Coerce common bbox shapes into a
BBox.Accepts a
BBox(returned as-is), a 4-tuple(x0, y0, x1, y1), or a polygon[(x, y), ...](4+ points).Nonepasses through.
- ocracy.make_backend.make_block(text, *, bbox=None, confidence=None, conf_scale=1.0, level='word', language=None, **meta)[source]#
Build a normalized
TextBlock.bboxmay be any shape accepted byas_bbox().confidenceis normalized to[0, 1]viaconf_scale(e.g.conf_scale=100for percent-scale engines).- Return type:
- ocracy.make_backend.make_test_image(text='OCR test 123', *, size=(640, 140), font_size=48)[source]#
Render a black-on-white test image with
text(needs Pillow).Uses a real TrueType font (DejaVuSans, then Pillow’s sized default) at a legible size so OCR engines can actually read it — important for
validate_adapter()to be a meaningful smoke test.
- ocracy.make_backend.normalize_confidence(value, *, scale=1.0)[source]#
Normalize a raw confidence to
[0, 1](dividing byscale).Nonepasses through. Usescale=100for engines that report0..100.
- ocracy.make_backend.scaffold_backend(backend_id, *, dest=None, overwrite=False, ledger=None, extra_overrides=None)[source]#
Create a new
ocracy/backends/<id>/package from the template.Pre-fills
config.pyfrom the backend’s ledger entry (if any) so you only have to flesh outparam_mapand implementadapter.py’s_read.- Parameters:
backend_id (
str) – The ledger id (e.g."easyocr","google-vision"). The on-disk module name uses underscores; the configidkeeps the id verbatim.dest (
Union[str,Path,None]) – Target directory (defaults toocracy/backends/<id_underscored>).overwrite (
bool) – Allow writing into an existing non-empty directory.ledger (
Any) – ACatalogto read the record from (defaults to the shipped catalog).extra_overrides (
Optional[dict]) – Extra config-key overrides applied on top of the record.
- Return type:
- Returns:
The path to the created backend package.
- ocracy.make_backend.validate_adapter(backend_id, *, image=None, expect_text=None)[source]#
Smoke-test a backend adapter end to end, returning a report dict.
Loads the adapter (reporting unavailability instead of raising), runs
readon a generated (or supplied) image, and checks the contract: anOcrResultcame back with text and/or blocks. Never raises on a recognition mismatch — it returns what happened so callers can decide.- Return type: