Skip to main content

SmartKhOCR 🇰🇭

SmartKhOCR is a state-of-the-art Khmer Optical Character Recognition (OCR) and Language Correction engine built with Deep Learning Vision Transformers (ViT) and Khmer phonetic language shaping models.

It provides real-time line-by-line streaming, native PDF multi-page recognition, and contextual Coeng (ជើង) & compound auto-correction.


✨ Features

  • 🚀 Deep Learning Vision Engine: Vision Transformer + CTC/Beam Search specifically fine-tuned on modern document scripts, headers, body text, and complex font styles.
  • ✨ Coeng & Compound Auto-Correction: Context-aware corrector that fixes hanging Coeng (e.g., ឆ្នាំ $\to$ ឆ្នាំ), accidental subscripts (សាលា $\to$ សាលា), and missing Coeng compounds (រាជធានីភំពើញ $\to$ រាជធានីភ្នំពេញ).
  • ⚡ Ultra-Fast Real-Time Streaming: Native line-by-line streaming across images and multi-page PDFs with page numbers and start-of-page markers.
  • 📄 Native Multi-Page PDF Pipeline: Direct PDF processing with on-the-fly rendering, batch execution, or continuous line-by-line streaming across pages.
  • 🔍 Side-by-Side Comparison Studio: Integrated web dashboard with bounding box visualization and parallel Tesseract OCR (khm+eng) benchmark comparisons.
  • 💻 CLI & REST API Ready: Built-in CLI tool and FastAPI Server-Sent Events (SSE) streaming server.

📦 Installation

# Clone the repository
git clone https://github.com/devnsmtech/smart-kh-ocr
cd smart-kh-ocr

# Install dependencies using uv or pip
uv pip install -e .
# or
pip install -e .

🚀 Quickstart (Python API)

1. Basic Image OCR

from SmartKhOCR import SmartKhOCR

# Initialize OCR engine (auto-selects CUDA if available)
model = SmartKhOCR(decode_method="fast")

# Recognize text with Coeng auto-correction
result = model("chapter.png", correct_word=True)

print("Recognized Text:")
print(result.text)

# Save annotated visualization with detected line boxes
result.save("output_annotated.jpg")

2. Multi-Page PDF Processing (Batch)

from SmartKhOCR import SmartKhOCR

model = SmartKhOCR()

# Process multi-page PDF directly (returns list of OCRResult objects)
pages = model("document.pdf", correct_word=True)

for idx, page in enumerate(pages, 1):
    print(f"\n--- Page {idx} ---")
    print(page.text)

3. Native Multi-Page PDF Streaming (Line-by-Line)

Stream lines in real time across pages without waiting for the whole PDF to process:

from SmartKhOCR import SmartKhOCR

model = SmartKhOCR(decode_method="fast")

for line in model("document.pdf", stream=True, correct_word=True):
    # line.page_start is True only on the first line of each page
    if line.page_start:
        print(f"\n=== Starting Page {line.page} ===")
    
    print(f"[{line.box}] (Conf: {line.confidence:.2f}) {line.text}")

4. Character-by-Character Streaming (Typewriter Effect)

For interactive typing or chat-style UI animations:

for chunk in model.stream_chars("document.png"):
    print(chunk["token"], end="", flush=True)

📋 OCRLine Structure

When streaming or inspecting results, each line is an OCRLine object with:

Property Type Description
line.text str Recognized & corrected Khmer text
line.page int 1-based page number (e.g. 1, 2, 3...)
line.page_start bool True for the first line of each page; False otherwise
line.box List[int] Detected bounding box [x, y, w, h]
line.padded_box List[int] Context-padded crop box fed to the ViT recognizer
line.confidence float Model confidence score (0.0 to 1.0)
line.raw_text str Raw decoded text before post-correction
line.to_dict() dict JSON-serializable dictionary with all above fields

🖥️ Command Line Interface (CLI)

SmartKhOCR installs command-line aliases (SmartKhOCR, smart-kh-ocr, smartkhocr):

# Run OCR on an image or PDF
SmartKhOCR document.pdf --correct-word --output-txt transcript.txt

# Export model to high-performance inference engines
SmartKhOCR --export engine       # TensorRT engine
SmartKhOCR --export onnx         # ONNX graph

🌐 Web Studio & Streaming REST API

SmartKhOCR comes with a real-time web dashboard and SSE streaming API:

uv run fastapi run app_ui.py
  1. Web Dashboard: Open http://localhost:8000 in your browser.
  2. REST API Streaming (Server-Sent Events):
curl -N -X POST http://localhost:8000/api/ocr/stream \
  -F "file=@document.pdf" \
  -F "correct_word=true" \
  -F "fast_mode=true" \
  -F "compare_tesseract=true"

🏷️ Package Architecture

Import using either convention:

# Direct class import (Recommended)
from SmartKhOCR import SmartKhOCR, OCRLine, OCRResult

# Snake_case module import
from smart_kh_ocr import SmartKhOCR, KhmerOCR

📄 License

MIT License. Designed and developed for high-accuracy Khmer text processing.

smart-kh-ocr

Metadata

Release files for smart-kh-ocr 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for smart-kh-ocr 0.1.3
File Size Uploaded
smart_kh_ocr-0.1.3.tar.gz 84.6 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for smart-kh-ocr 0.1.3
File Interpreter ABI Platform
smart_kh_ocr-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 169.2 MB

Release files / smart_kh_ocr-0.1.3.tar.gz

Download URL smart_kh_ocr-0.1.3.tar.gz
Size 84.6 MB
Tags Source
SHA-256 checksum
How to use checksums
f45835ee563671c26faf95edb7b20d5e4914bd5d21b600899a3b6e3f3cf686af
BLAKE2b-256 checksum
How to use checksums
aa2b9e8910eee3a122c3bed674247a25331bc37fa5a002edbde881dd27036185
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.20

Release files / smart_kh_ocr-0.1.3-py3-none-any.whl

Download URL smart_kh_ocr-0.1.3-py3-none-any.whl
Size 84.6 MB
Tags Python 3
SHA-256 checksum
How to use checksums
4e3171a77902132b0a019d736738dd38105a3b3e41776d4964d1d1dc46e26401
BLAKE2b-256 checksum
How to use checksums
11ae3cec2f6e23ac2381767c20abc0620321a92c69142eb6dc8be095696f9c00
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.20

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page