churro_ocr#
Library-first public API for churro-ocr.
- class churro_ocr.BatchOCRBackend[source]#
Bases:
ProtocolAsync batch OCR backend interface.
- __init__(*args, **kwargs)#
- exception churro_ocr.ChurroError[source]#
Bases:
RuntimeErrorBase exception for package-level failures.
- exception churro_ocr.ConfigurationError[source]#
Bases:
ChurroErrorRaised when a backend is missing required runtime configuration.
- class churro_ocr.DocumentPage[source]#
Bases:
objectA document page image with optional OCR output attached.
- Parameters:
page_index – Page position in the current output sequence.
image – Page image.
source_index – Index of the original source item that produced the page.
bbox – Bounding box in source-image coordinates when available.
polygon – Polygon in source-image coordinates when available.
metadata – Caller-side or detector-side metadata for the page.
text – OCR text attached to the page when OCR has been run.
provider_name – Provider identifier attached by OCR.
model_name – Model name attached by OCR.
ocr_metadata – Provider-returned OCR metadata for this page.
- __init__(page_index, image, source_index, bbox=None, polygon=(), metadata=<factory>, text=None, provider_name=None, model_name=None, ocr_metadata=<factory>)#
- classmethod from_image(image, *, page_index=0, source_index=0, metadata=None)[source]#
Create a document page from an in-memory image.
- Parameters:
- Returns:
New page object with a copied image.
- Return type:
- classmethod from_image_path(path, *, page_index=0, source_index=0, metadata=None)[source]#
Create a document page from an image path.
- Parameters:
- Returns:
New page object loaded from
path.- Return type:
- class churro_ocr.DocumentOCRPipeline[source]#
Bases:
objectRun page detection and OCR as one document-level pipeline.
The pipeline is the highest-level API in the package. It detects pages from an image or PDF, runs OCR on each detected page, and preserves the page objects in the final result.
Create a document OCR pipeline.
- Parameters:
ocr_backend – OCR backend or async OCR callable used for each page.
page_detector – Optional fully constructed page detector to reuse.
detection_backend – Optional low-level detection backend used when
page_detectoris not provided.max_concurrency – Maximum number of page OCR jobs run at once.
- Raises:
ConfigurationError – If
max_concurrencyis less than 1.
- __init__(ocr_backend, *, page_detector=None, detection_backend=None, max_concurrency=8)[source]#
Create a document OCR pipeline.
- Parameters:
ocr_backend (OCRBackend | Callable[[DocumentPage], Awaitable[OCRResult]]) – OCR backend or async OCR callable used for each page.
page_detector (DocumentPageDetector | None) – Optional fully constructed page detector to reuse.
detection_backend (PageDetectionBackend | Callable[[Image], Awaitable[list[PageCandidate]]] | None) – Optional low-level detection backend used when
page_detectoris not provided.max_concurrency (int) – Maximum number of page OCR jobs run at once.
- Raises:
ConfigurationError – If
max_concurrencyis less than 1.- Return type:
None
- async process_image(request, *, ocr_metadata=None)[source]#
Detect pages and OCR a single input image.
- Parameters:
request (PageDetectionRequest) – Image detection request describing the source image.
ocr_metadata (dict[str, Any] | None) – Optional caller-side metadata merged into each page before OCR runs.
- Returns:
Document OCR result preserving page order and page images.
- Return type:
- process_image_sync(request, *, ocr_metadata=None)[source]#
Synchronously detect pages and OCR a single input image.
- Parameters:
request (PageDetectionRequest) – Image detection request describing the source image.
ocr_metadata (dict[str, Any] | None) – Optional caller-side metadata merged into each page before OCR runs.
- Returns:
Document OCR result preserving page order and page images.
- Return type:
- async process_pdf(path, *, dpi=300, trim_margin=30, ocr_metadata=None)[source]#
Rasterize, detect pages, and OCR a PDF.
- Parameters:
- Returns:
Document OCR result across the rasterized PDF pages.
- Return type:
- class churro_ocr.DocumentOCRResult[source]#
Bases:
objectDocument OCR output across all detected pages.
- Parameters:
pages – OCR-enriched pages in output order.
source_type – Input source type, typically
"image"or"pdf".metadata – Document-level metadata carried forward from page detection.
- __init__(pages, source_type, metadata=<factory>)#
- class churro_ocr.DocumentPageDetector[source]#
Bases:
objectDetect pages from raw images or PDFs.
Create a document page detector.
- Parameters:
backend – Optional low-level detection backend or async callable.
- __init__(*, backend=None)[source]#
Create a document page detector.
- Parameters:
backend (PageDetectionBackend | Callable[[Image], Awaitable[list[PageCandidate]]] | None) – Optional low-level detection backend or async callable.
- Return type:
None
- async detect_image(request)[source]#
Detect pages in a single image.
- Parameters:
request (PageDetectionRequest) – Detection request describing the source image.
- Returns:
Detection result for one image input.
- Return type:
- detect_image_sync(request)[source]#
Synchronously detect pages in a single image.
- Parameters:
request (PageDetectionRequest) – Detection request describing the source image.
- Returns:
Detection result for one image input.
- Return type:
- async detect_pdf(path, *, dpi=300, trim_margin=30)[source]#
Rasterize a PDF and detect pages on each image.
- Parameters:
- Returns:
Detection result containing all detected pages from the PDF.
- Return type:
- class churro_ocr.OCRBackend[source]#
Bases:
ProtocolAsync OCR backend interface.
- __init__(*args, **kwargs)#
- async ocr(page)[source]#
Run OCR for one page.
- Parameters:
page (DocumentPage) – Page image and page metadata to transcribe.
- Returns:
Provider-agnostic OCR result for the page.
- Return type:
- class churro_ocr.OCRClient[source]#
Bases:
objectUser-facing OCR client with page-first sync and async entrypoints.
Create an OCR client.
- Parameters:
backend – OCR backend or async callable used for page OCR.
- __init__(backend)[source]#
Create an OCR client.
- Parameters:
backend (OCRBackend | Callable[[DocumentPage], Awaitable[OCRResult]]) – OCR backend or async callable used for page OCR.
- Return type:
None
- async aocr(page)[source]#
Run OCR asynchronously for one page.
- Parameters:
page (DocumentPage) – Page to transcribe.
- Returns:
Copy of
pagewith OCR output attached.- Return type:
- async aocr_image(*, image=None, image_path=None, page_index=0, source_index=0, metadata=None)[source]#
Create a single page from an image input and OCR it.
- Parameters:
image (Image | None) – In-memory page image. Mutually exclusive with
image_path.image_path (str | Path | None) – Path to a page image on disk. Mutually exclusive with
image.page_index (int) – Page position to attach to the generated page.
source_index (int) – Original source index to attach to the generated page.
metadata (dict[str, Any] | None) – Optional caller-side metadata attached before OCR runs.
- Returns:
OCR-enriched page object.
- Raises:
ConfigurationError – If both or neither of
imageandimage_pathare provided.- Return type:
- ocr(page)[source]#
Run OCR synchronously for one page.
- Parameters:
page (DocumentPage) – Page to transcribe.
- Returns:
Copy of
pagewith OCR output attached.- Return type:
- ocr_image(*, image=None, image_path=None, page_index=0, source_index=0, metadata=None)[source]#
Create a single page from an image input and OCR it synchronously.
- Parameters:
image (Image | None) – In-memory page image. Mutually exclusive with
image_path.image_path (str | Path | None) – Path to a page image on disk. Mutually exclusive with
image.page_index (int) – Page position to attach to the generated page.
source_index (int) – Original source index to attach to the generated page.
metadata (dict[str, Any] | None) – Optional caller-side metadata attached before OCR runs.
- Returns:
OCR-enriched page object.
- Raises:
ConfigurationError – If both or neither of
imageandimage_pathare provided.- Return type:
- class churro_ocr.OCRResult[source]#
Bases:
objectProvider-agnostic OCR result.
- Parameters:
text – OCR text after any backend-specific postprocessing.
provider_name – Stable provider identifier attached to the result.
model_name – Human-readable model name attached to the result.
metadata – Provider-returned metadata for this OCR call.
- class churro_ocr.PageDetectionBackend[source]#
Bases:
ProtocolAsync interface for page detection.
- __init__(*args, **kwargs)#
- class churro_ocr.PageDetector[source]#
Bases:
objectDetect one or more page crops from an input image.
Create a page detector.
- Parameters:
backend – Optional low-level backend or async callable. When not provided, the full input image is treated as a single page.
- __init__(backend=None)[source]#
Create a page detector.
- Parameters:
backend (PageDetectionBackend | Callable[[Image], Awaitable[list[PageCandidate]]] | None) – Optional low-level backend or async callable. When not provided, the full input image is treated as a single page.
- Return type:
None
- async adetect(request)[source]#
Asynchronously detect pages for a single image.
- Parameters:
request (PageDetectionRequest) – Detection request describing the source image.
- Returns:
Detected page crops in reading order.
- Return type:
- detect(request)[source]#
Synchronously detect pages for a single image.
- Parameters:
request (PageDetectionRequest) – Detection request describing the source image.
- Returns:
Detected page crops in reading order.
- Return type:
- class churro_ocr.PageCandidate[source]#
Bases:
objectIntermediate page candidate returned by a page detector.
- Parameters:
bbox – Bounding box in source-image coordinates.
image – Optional already-cropped page image. When provided, detection callers use this image directly instead of cropping from
bboxorpolygon.polygon – Optional polygon in source-image coordinates.
metadata – Detector-side metadata attached to the candidate.
- class churro_ocr.PageDetectionRequest[source]#
Bases:
objectRequest payload for image page detection.
- Parameters:
image – In-memory image to detect pages from. Mutually exclusive with
image_path.image_path – Path to an image on disk. Mutually exclusive with
image.trim_margin – Margin in pixels to add around detected crops.
- __init__(image=None, image_path=None, trim_margin=30)#
- require_image()[source]#
Return the input image, loading it from disk when needed.
- Returns:
Copy of the requested image.
- Raises:
ConfigurationError – If both or neither of
imageandimage_pathare provided.- Return type:
- class churro_ocr.PageDetectionResult[source]#
Bases:
objectPage detection output for an image or PDF.
- Parameters:
pages – Detected pages in output order.
source_type – Input source type, typically
"image"or"pdf".metadata – Detection-level metadata, such as PDF rasterization settings.
- exception churro_ocr.ProviderError[source]#
Bases:
ChurroErrorRaised when an OCR or page detection provider returns an unusable response.