Skip to main content

Khmer OCR

Project description

Mer (មើល)

Mer (មើល) is a lightweight bilingual (Khmer/English) OCR recognizer built around my custom CNN-Transformer network exported to ONNX.
This repository now focuses exclusively on line-level OCR — the Surya-powered layout, table, and LaTeX helpers have been removed, so the package is small and easy to embed anywhere you just need text from a single line image.

Installation

pip install mer

Getting started

from mer import Mer, postprocess_text

ocr = Mer()
ocr.load()  # optional; kept for backwards compatibility

text = ocr.recognize_line("samples/sample_1.png")
print("Line text:", text)

# predict() is an alias for recognize_line()
json_result = ocr.predict("samples/sample_1.png", json_result=True)
print(json_result["text"])

# Optional extra cleanup
print(postprocess_text("ទៀតផង ។"))  # -> "ទៀតផង។"

Configuration options

All options control the ONNX Runtime predictor:

  • device: "cpu", "cuda", or specific device strings. Defaults to "cuda" with automatic CPU fallback.
  • providers: optional explicit ONNX Runtime provider list. When omitted, providers are derived from device.
  • model_path: point to a directory containing khmer_ocr.onnx and config.json to skip Hugging Face downloads.
  • cache_dir / repo_id: control where artifacts are downloaded from Hugging Face Hub (metythorn/ocr-stn-cnn-transformer-base by default).
  • max_length: override the configured maximum decoding length.
  • postprocess: disable built-in whitespace cleanup if you prefer the raw model output.
  • json_result: default return type for predict(). When True, predict() returns {"text": ...}; otherwise it returns a raw string. You can always override this per-call.

Using local model files

If you already have the ONNX weights and config on disk, point Mer at the folder to skip any Hugging Face download:

from mer import Mer

ocr = Mer(model_path="/path/to/local/model_dir", device="cpu")
ocr.load()
print(ocr.recognize_line("line.png"))

Post-processing helper

postprocess_text is exposed as a standalone helper so you can reuse the same Khmer punctuation cleanup on your own strings:

from mer import postprocess_text

assert postprocess_text(" ទៀត\tផង  ។ ") == "ទៀត ផង។"

Sample data

The samples/ directory contains a few PNGs you can use for quick manual testing. They are untouched and meant purely for experimentation with the line recognizer.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mer-1.2.1.tar.gz (14.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mer-1.2.1-py3-none-any.whl (14.3 kB view details)

Uploaded Python 3

File details

Details for the file mer-1.2.1.tar.gz.

File metadata

  • Download URL: mer-1.2.1.tar.gz
  • Upload date:
  • Size: 14.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for mer-1.2.1.tar.gz
Algorithm Hash digest
SHA256 9966755bdff9ec356bf5610d95fb895437da5241c610ae42686f44c484c6ddd6
MD5 abb034c4ef38ddf637a5c25280700ae0
BLAKE2b-256 78155aa2857406eb720d2a80638b50b0f980206b1bef80c6662afe3bdd84d3c2

See more details on using hashes here.

File details

Details for the file mer-1.2.1-py3-none-any.whl.

File metadata

  • Download URL: mer-1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 14.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for mer-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4cfb717b0705e6326e283af72a93113bd991318646974f1c54cc30a385cb53cb
MD5 4e595c76af97f0a316e86e6e714f937e
BLAKE2b-256 fd9e7eb2c3d8d856be960d2d95706ef0774c3517e80164f4706bd0dd5afbbe6c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page