Skip to main content

Crate monocr_onnx

Crate monocr_onnx 

Source
Expand description

MonOcr - Mon language OCR library using ONNX models

This library provides OCR (Optical Character Recognition) functionality for Mon text using deep learning models. It supports reading text from images and PDFs, with optional accuracy measurement against ground truth text.

Mon (mnw) is a Mon-Khmer language of Myanmar and Thailand, written in a Myanmar-script orthography. It is unrelated to Mongolian.

§The model

Weights are downloaded from janakhpon/monocr, pinned to revision model_manager::MODEL_REVISION. That artifact takes a [1, 1, 160, 1024] input and emits [1, sequence, 277] logits: 276 characters plus the CTC blank. The width is static: v3.5 accepts 1024 and nothing else, where v2 accepted any width.

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 graph is read on load and a disagreement yields ModelContractError instead of a result.

§Quick Start

use monocr_onnx::read_image;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let text = read_image("path/to/image.png").await?;
    println!("Recognized text: {}", text);
    Ok(())
}

§Features

  • Read text from single images (PNG, JPG, etc.)
  • Read text from multiple images in batch
  • Read text from PDF files (requires poppler-utils)
  • Measure OCR accuracy against ground truth
  • Customizable model paths and character sets
  • Line segmentation for full page OCR
  • Lines too wide for the 1024px model window are tiled at whitespace columns rather than squeezed into it; see MonOcr::predict_page for the measured reason

Re-exports§

pub use model_manager::ModelManager;
pub use segmenter::cut_column;
pub use segmenter::tile_line;
pub use segmenter::CUT_INK_THRESHOLD;
pub use segmenter::CUT_SEARCH_FRACTION;
pub use segmenter::DEFAULT_DENSITY_THRESHOLD_RATIO;

Modules§

model_manager
Model Manager
segmenter
Line Segmentation

Structs§

BBox
Bounding box for a line or text region
LineResult
Result from line prediction
ModelContractError
A model artifact that disagrees with the charset or the input geometry this binding was built for.
MonOcr
Main OCR engine for text recognition
MonOcrBuilder
Builder for configuring and creating MonOcr instances
OcrResult
OCR result containing recognized text and accuracy measurement

Constants§

DEFAULT_INPUT_WIDTH
Padded canvas width fed to the model.
EXPECTED_INPUT_HEIGHT
Input height this binding preprocesses for.

Functions§

calculate_accuracy
Calculate character error rate (CER) between predicted and ground truth Returns accuracy percentage (0-100)
normalize_charset
Strip line terminators, and nothing else.
normalize_polarity
Return image as dark-text-on-light, inverting it when the background is dark.
page_text
Join one page’s line texts the way MonOcr::read_image does.
read_image
Read text from a single image file
read_image_with_accuracy
Read text from an image with accuracy measurement
read_images
Read text from multiple image files
read_pdf
Read text from a PDF file