pp_ocr library

The web-safe core of pdf_ocr_ondevice: the PP-OCR detect-then-recognize pipeline in plain Dart (PpOcrPipeline) over a pluggable network backend (PpOcrInference), and the OcrRunnerEngine that turns its lines into a PdfOcrEngine's spans.

The package's main library adds native ONNX Runtime (OnnxOcrModelRunner), the worker isolate and the model downloader, none of which compile for the web. A browser build imports this library instead and supplies an onnxruntime-web PpOcrInference, so both platforms run the same pre- and post-processing - and read a page the same way.

Classes

CtcDecoder
Greedy CTC decoder over a fixed character dictionary.
CtcResult
The text decoded from a recognizer's per-timestep scores.
DetectedBox
A text region found by detection, as an axis-aligned box in the original image's pixel space (top-left origin, y down) with the mean detector probability over its pixels.
DetectionResize
The target size and the per-axis scale factors of a detection resize.
OcrImage
A decoded RGBA raster in plain Dart memory - the form the OCR pipeline works in, so all of its geometry/cropping/resizing is pure Dart and unit testable off a GPU.
OcrModelRunner
The inference backend behind OnDeviceOcrEngine: turns a rasterized page into recognized text lines.
OcrRunnerEngine
A PdfOcrEngine over any OcrModelRunner: reads the page raster into an OcrImage, runs the backend, and maps each recognized line's pixel box to PDF user space via PdfOcrPageImage.userSpaceRect. The engine (and the geometry it owns) is independent of which recognizer runs - the native OnDeviceOcrEngine and the web build both use it.
PpOcrInference
The two network calls of a PP-OCR pipeline - everything PpOcrPipeline cannot do in plain Dart. Natively that is ONNX Runtime over FFI (OnnxOcrModelRunner); in a browser it is onnxruntime-web behind a JS bridge. Both run the same Dart pre- and post-processing around it, so the platforms read a page identically.
PpOcrPipeline
A PP-OCR-style detect-then-recognize pipeline over a PpOcrInference:
RecognizedTextLine
One recognized text line: the characters and where they sit in the page raster's pixel space (top-left origin, y down - the same space the OCR model worked in). OnDeviceOcrEngine maps pixelBounds back to PDF user space.

Functions

cleanRecognizedText(String text) → String?
Cleans one recognized line for a searchable text layer, or returns null when nothing worth searching for is left.
detectionResize(int srcWidth, int srcHeight, {int sideLimit = 4000, int multiple = 32}) → DetectionResize
Computes the detection input size for an srcWidth x srcHeight page: longest side scaled down to at most sideLimit, each side rounded to a multiple of multiple (and never below it). Pages already within the limit are only rounded, never upscaled.
extractDetectionBoxes(Float32List probMap, int width, int height, {double threshold = 0.3, double boxScoreThreshold = 0.6, double unclipRatio = 1.5, int minSize = 3, double scaleX = 1.0, double scaleY = 1.0}) → List<DetectedBox>
Extracts text boxes from a DB-style detection probability map.
parseDictionary(String contents, {bool addSpace = true}) → List<String>
Parses a PP-OCR character dictionary file: one token per line, blank lines preserved as a single space (some dicts encode the space as an empty line). A trailing space token is appended when the file does not already end with one, matching PP-OCR's use_space_char default.
recognitionInput(OcrImage crop, {int targetHeight = 48, int minWidth = 320, int maxWidth = 3200}) → ({int paddedWidth, Float32List tensor, int width})
Recognition preprocessing for one cropped text line: resize to the network's fixed targetHeight keeping aspect, then normalize to NCHW float32 in [-1, 1] (PP-OCR rec convention: (pixel/255 - 0.5) / 0.5).
toNchwFloat32(OcrImage image, {List<double> mean = const [0.485, 0.456, 0.406], List<double> std = const [0.229, 0.224, 0.225]}) → Float32List
Normalizes image into an NCHW float32 tensor ([1, 3, height, width], channel order R, G, B) using per-channel mean and std on the 0..1 range - the standard ImageNet-style detection input.

Typedefs

OcrTensor = ({Float32List data, List<int> shape})
A float tensor's elements (row-major) and its shape.