MonOCR (Rust SDK)
The official Rust SDK for Mon language OCR, powered by ONNX Runtime.
Installation
Add this to your Cargo.toml:
[]
= "0.3"
= { = "1", = ["full"] }
The crate is monocr; the library it exposes is monocr_onnx, so imports read
use monocr_onnx::MonOcr.
The model
Weights are downloaded from
janakhpon/monocr, pinned to revision
d3d9d5e (model_manager::MODEL_REVISION). That artifact takes a
[batch, 1, 160, 1024] input and emits [batch, sequence, 277] logits: 276
characters plus the CTC blank. Height and width are both static; batch is
the only dynamic axis. This line previously read [1, 1, 160, width], which had
both halves backwards.
The charset, the input height and the classifier width are one contract. If they
drift apart the model still runs and still returns text — it is just the wrong
text, with no error anywhere. So the SDK reads the real graph on load and returns
a ModelContractError when it disagrees with the charset it holds:
model contract violation: charset/model mismatch.
charset: 276 characters -> expects 277 classes (276 + CTC blank)
model (/…/monocr.onnx): 225 classes
Downloads are cached per revision under ~/.monocr/models/<revision>/, so
re-pinning is a cache miss rather than a silent reuse of the previous artifact.
Features
- Auto-Model Management: Downloads and caches the pinned ONNX weights and
their charset to
~/.monocr/models/<revision>/. - Fail-closed loading: Refuses to run a model whose input height or class count disagrees with the charset, instead of returning the wrong text.
- Memory Efficient: Uses
ndarrayfor tensor construction. - Line segmentation: Horizontal projection profile over a flat global
threshold at 128 for full-page OCR (
src/segmenter.rs:288). Not adaptive — the crate's own docs atsrc/segmenter.rs:265state it correctly.
Quick Start
use MonOcr;
async
Or the one-shot free functions, which build a MonOcr for you:
use ;
async
Using your own model
use MonOcr;
async
The charset is stripped of line terminators only. Its first character is
U+0020 — a space is one of the classes the model emits, so trimming it with
.trim() shifts every index in the decode by one.
Prerequisites
This crate requires the ONNX Runtime shared library to be available on your system.
- macOS:
brew install onnxruntime - Linux: Download
libonnxruntime.soand add toLD_LIBRARY_PATH.
License
MIT