Pipeline and Stack

Pipeline

class manuscript.Pipeline(detector=<default>, layout=<default>, recognizer=<default>, corrector=None, layout_after='detector', memory_policy='keep')[source]

Bases: ModelComposition

High-level OCR pipeline with configurable stage ordering.

Default pipeline: detector -> layout -> recognizer. corrector is optional and disabled by default.

Initialize OCR pipeline.

Parameters:
  • detector (object, optional) – Detector instance with predict(image) -> Page. If omitted, default YOLO(weights="yolo26x_obb_text_g1") is used. Detector cannot be disabled.

  • layout (object or None, optional) – Layout model instance with predict(page, image=None) -> Page. If omitted, default SimpleSorting() is used. Pass None to disable layout stage.

  • recognizer (object or None, optional) – Recognizer instance with predict(page, image=None, ...) -> Page. If omitted, default TRBA(weights="trba_lite_g2") is used. Pass None to disable recognition stage.

  • corrector (object or None, optional) – Corrector instance with predict(page, image=None) -> Page. Default is None (disabled).

  • layout_after ({"detector", "recognizer", "corrector"}, optional) – Slot where layout stage is executed. Default is "detector".

  • memory_policy ({"keep", "sequential"}, optional) – Keep loaded model sessions by default. “sequential” releases sessions after each processor call, including on failure, and applies inside nested Stacks and ensembles. Participants require close(); nested parallel execution is incompatible. Shared models need external synchronization across pipelines and direct calls.

predict(image, vis=False, profile=False)[source]

Run pipeline on a single image.

Parameters:
  • image (str, Path, numpy.ndarray, or PIL.Image) – Input image.

  • vis (bool, optional) – If True, returns visualization image together with result.

  • profile (bool, optional) – If True, prints timing per stage.

Return type:

Union[Dict[str, Page], tuple]

get_text(page, format='plain', *, separators=None, separator_priorities=None)[source]

Delegate text serialization to Page.get_text.

Return type:

str

Parameters:

page (Page)

property last_timings: Dict[str, float]

Return elapsed seconds for the last predict call as a dictionary copy.

Keys: detector, layout, recognizer, corrector, load_image, visualization and total. Only executed stages are present. Stage times include tree validation/snapshots and lazy model initialization. Explicit image loading by Pipeline is recorded separately and subtracted from page-stage times. Loading performed inside a model remains part of that stage’s time. Total includes orchestration and optional visualization. On failure, completed stage timings remain available without a total entry.

property last_detection_page: Page | None
property last_layout_page: Page | None
property last_recognition_page: Page | None
property last_correction_page: Page | None

Stack

class manuscript.Stack(stages, *, parallel=False, memory_policy='keep')[source]

Bases: ModelComposition

Combine processors in one Pipeline stage or run them directly.

Parameters:
  • stages (iterable of processors) – Nonempty ordered collection implementing predict(image, …) or predict(page, image=None, …). Nested Stacks are supported.

  • parallel (bool, optional) – Run image detectors concurrently in threads if True; default is False. Results are merged in list order. Only detector-only Stacks support this mode; page processors require the preceding result.

  • memory_policy ({"keep", "sequential"}, optional) – Keep sessions loaded by default. “sequential” releases each processor’s sessions after its call, including on failure, and applies to nested compositions. Requires close() on every participant and disallows parallel=True. Participants must be exclusively owned or externally synchronized when shared with other callers.

Examples

>>> detector = Stack([block_detector, word_detector])
>>> page = detector.predict(image)
>>> recognizer = Stack([recognizer_a, recognizer_b])
>>> page = recognizer.predict(page, image=image)

See also

manuscript.recognizers.MajorityVotingRecognizer

Vote by whole-string agreement.

manuscript.recognizers.MedoidRecognizer

Select by summed edit distance.

predict(page=None, image=None, *, return_raw=False, return_masks=False, return_outputs=False, detector_result=None, **kwargs)[source]

Accept an image, a Page with optional image, or named inputs.

Return Page by default. Raw/mask/output flags return a dictionary with the final Page and detector results grouped by model. Page processors always run in Page mode. No prediction results are cached on the Stack.

Parameters: