Skip to content

Overview

PP-OCR text-recognizer family, its backends, and pinned model catalogue.

Importing this package registers every PP-OCR recognizer backend under the PPOCRTextRecognizer family.

Exported Classes

OnnxPPOCRTextRecognizer(source=DEFAULT_MODEL_SOURCE, dictionary=None, cache_dir=None, batch_size=DEFAULT_BATCH_SIZE, num_threads=None, providers=None)

Bases: PPOCRTextRecognizer

Run a pinned or locally supplied PP-OCRv6 recognition model with ONNX Runtime.

Construction only validates options. Built-in weights and the character dictionary are downloaded by ensure_ready, and the ONNX Runtime session is created lazily, under a lock, on the first recognize call. Both are released by close.

Requires the ocr, video-ffmpeg, or video-gst extra, which installs ONNX Runtime.

Attributes:

Name Type Description
source str | Path

Built-in recognizer name (str) or readable local ONNX path.

cache_dir str | Path | None

Persistent weight cache root for built-in models.

batch_size int

Maximum number of crops recognized in one ONNX Runtime call.

num_threads int | None

Optional CPU intra-op thread count.

providers tuple[ExecutionProvider, ...]

ONNX Runtime execution providers in priority order.

Validate the model source and options without I/O.

Parameters:

Name Type Description Default
source str | Path

Exact built-in recognizer name, one of ppocrv6_tiny_rec, ppocrv6_small_rec, or ppocrv6_medium_rec, or a path to an existing readable local ONNX file. Built-in names take priority over same-named local files. Defaults to ppocrv6_tiny_rec.

DEFAULT_MODEL_SOURCE
dictionary str | Path | Sequence[str] | None

CTC dictionary characters (excluding the blank class and trailing space), a path to a UTF-8 text file with one character per line, or None to download the pinned dictionary of a built-in source in ensure_ready. Required for a local source. Defaults to None.

None
cache_dir str | Path | None

Persistent cache root for built-in weights. When None, verified weights are kept in memory for the recognizer's lifetime. Must be None for a local path source. Defaults to None.

None
batch_size int

Maximum number of crops recognized in one ONNX Runtime call. Must be at least 1. Defaults to 8.

DEFAULT_BATCH_SIZE
num_threads int | None

ONNX Runtime CPU intra-op thread count. None or 0 lets ONNX Runtime choose. Defaults to None.

None
providers Sequence[ExecutionProvider] | None

ONNX Runtime execution providers in priority order, each a provider name such as "OpenVINOExecutionProvider" or a (name, options) pair. Providers other than CPU need the ONNX Runtime package that ships them installed in place of onnxruntime. None uses CPUExecutionProvider. Defaults to None.

None

Raises:

Type Description
ComponentConfigError

If the source is unsupported, no dictionary is available for a local source, cache_dir is supplied for a local path, or an option or execution provider is invalid.

ComponentModelError

If a local path source is not a readable file.

num_threads property

Return the ONNX Runtime CPU intra-op thread count.

Returns:

Type Description
int | None

int | None: Thread count, or None when ONNX Runtime chooses.

providers property

Return the ONNX Runtime execution providers.

Returns:

Type Description
tuple[ExecutionProvider, ...]

tuple[ExecutionProvider, ...]: Execution providers in priority order.

close()

Idempotently release the ONNX session and memory-backed model bytes.

This method is safe to call multiple times and from multiple threads.

ensure_ready() async

Resolve built-in weights and dictionary once so recognize can run.

Local path sources need no weight download, but still need a dictionary given at construction. Concurrent callers share one download of each artifact. The steps are: 1. Check that the recognizer is open, and return early when nothing is left to resolve. 2. Load the built-in character dictionary from its inference config, once. 3. Resolve the built-in weights through the shared ONNX session, once.

Raises:

Type Description
MediaOperationFailedError

If the recognizer has been closed.

ComponentModelError

If downloading, verifying, or caching a built-in artifact fails.

ValueError

If the downloaded configuration has no valid character dictionary.

CancelledError

If the caller cancels resolution.

PPOCRTextRecognizer(batch_size=DEFAULT_BATCH_SIZE, dictionary=None)

Bases: FamilyBuildMixin, BackendSelectableProcessor[Attachment, Attachment], BaseTextRecognizer, ABC

Family base for PP-OCRv6 text recognizers with pluggable model runtimes.

The family is built by name, so backend selects the runtime and the remaining keyword arguments go to that backend's constructor. Calling the family base directly, such as PPOCRTextRecognizer("ppocrv6_tiny_rec"), builds the same default backend as PPOCRTextRecognizer.build. The default backend is ONNX Runtime, OnnxPPOCRTextRecognizer.

Construction only validates options and loads a caller-supplied dictionary. Backends prepare their model in ensure_ready and release it in close.

Preprocessing follows PaddleOCR's recognition pipeline: 1. Crops stay in BGR order and are resized to height 48, with the width rounded up from the crop's aspect ratio. 2. Pixels are scaled to [-1, 1]. 3. Each batch is right-padded with zeros to its widest crop, and is never narrower than 320 pixels.

Attributes:

Name Type Description
batch_size int

Maximum number of crops recognized in one model call.

Validate the batch size and load the caller-supplied dictionary without I/O.

Parameters:

Name Type Description Default
batch_size int

Maximum number of crops recognized in one model call. Must be at least 1. Defaults to 8.

DEFAULT_BATCH_SIZE
dictionary str | Path | Sequence[str] | None

CTC dictionary characters (excluding the blank class and trailing space), or a path to a UTF-8 text file with one character per line. None leaves the dictionary to the backend, which loads it in ensure_ready. Defaults to None.

None

Raises:

Type Description
ComponentConfigError

If batch_size is not a positive integer, or the dictionary is empty, is not a readable UTF-8 file, or has no characters.

close() abstractmethod

Idempotently release the backend model and its native resources.

ensure_ready() abstractmethod async

Prepare the backend model and dictionary once so recognize can run.

Implementations must be idempotent and safe to await concurrently.

Raises:

Type Description
MediaOperationFailedError

If the recognizer has been closed.

ComponentModelError

If the built-in weights or dictionary cannot be downloaded, cached, or verified.

recognize(crops)

Recognize upright BGR uint8 crops and greedily decode each one's CTC output.

Crops are batched in order of aspect ratio, so crops of similar resized width share a batch and limit each other's padding; results are returned in input order.

Parameters:

Name Type Description Default
crops list[ndarray]

Non-empty three-channel BGR uint8 crops of any size.

required

Returns:

Type Description
list[TextRecognition]

list[TextRecognition]: One (text, confidence) pair per crop, in input order. ("", 0.0) when nothing is decoded for a crop.

Raises:

Type Description
MediaOperationFailedError

If the recognizer is closed.

MediaOperationConfigError

If ensure_ready was not awaited.

MediaInputInvalidError

If crops is not a list of non-empty BGR uint8 images.

MediaResultOperationError

If the model output does not match the batch size.

ComponentConfigError

If the model output does not match the dictionary.