Skip to main content

ocr-postprocess

Biến raw OCR text thành structured document — trích xuất trường dữ liệu, xử lý nhiễu, cross-check, và render ra JSON/Markdown.

Installation

pip install ocr-postprocess

Hoặc cài từ source:

git clone https://github.com/your-org/ocr-postprocess
cd ocr-postprocess
pip install -e .

Library usage

from ocr_postprocess import Pipeline, ProcessedDocument, OcrPostprocessError

# Sử dụng profiles bundled sẵn (no extra files needed)
pipeline = Pipeline.from_default()

raw_text = open("scan.txt").read()

try:
    doc: ProcessedDocument = pipeline.process(raw_text)
except OcrPostprocessError as exc:
    print(f"Pipeline error: {exc}")
    raise

# Lấy một trường
name_candidate = doc.get("ho_va_ten")
if name_candidate:
    print(name_candidate.value)       # "NGUYỄN VĂN A"
    print(name_candidate.confidence)  # 0.91

# Toàn bộ trường đã trích
fields = {c.key: c.value for c in doc.candidates}

# Export JSON
import json
print(json.dumps(doc.to_json(), ensure_ascii=False, indent=2))

# Export Markdown
print(doc.markdown)

Custom profiles directory

# Dùng thư mục profiles riêng
pipeline = Pipeline.from_default(profiles_dir="my_profiles/")

Classify only

profile_id, score = pipeline.classify(raw_text)
# "cccd_2024", 0.97

ProcessedDocument fields

Field Type Mô tả
profile_id str Profile được match
profile_score float Điểm classify (0–1)
candidates list[Candidate] Tất cả trường đã trích
overall_confidence float Điểm tin cậy tổng hợp
warnings list[str] Cảnh báo từ pipeline
markdown str Kết quả render Markdown
cross_checks list[CrossCheck] Kết quả cross-check

CLI

# Process a document
ocrpp process scan.txt

# Markdown output
ocrpp process scan.txt --format markdown

# Classify only
ocrpp classify scan.txt

# Validate a profile
ocrpp validate-profile profiles/my_profile.yml

# Custom profiles directory
ocrpp process scan.txt --profiles ./my_profiles/

Adding a custom profile

Tạo file YAML trong thư mục profiles của bạn:

id: my_doc
version: 1
display_name: "My document type"

classify:
  any_of:
    - contains_any: ["MY DOCUMENT HEADER"]

extract:
  - name: document_number
    aliases: ["Document No", "Số chứng từ"]
    extractor: value_in_same_line
    required: true

Sau đó:

pipeline = Pipeline.from_default(profiles_dir="my_profiles/")

Exceptions

from ocr_postprocess import (
    OcrPostprocessError,      # base
    ProfileNotFoundError,
    ProfileValidationError,
    ExtractorNotFoundError,
    TransformError,
)

Development

python -m venv .venv && source .venv/bin/activate
pip install -r requirements-dev.txt
pip install -e .

pytest                    # all tests
pytest tests/unit         # unit only
pytest -m golden          # golden/regression
pytest -n auto --cov      # parallel + coverage
ruff check . && black --check .

Docs

Xem docs/README.md để biết chi tiết về pipeline stages và profile schema.

Metadata

Release files for ocr-postprocess 0.1.2

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

Source distribution (sdist)

Source distribution for ocr-postprocess 0.1.2
File Size Uploaded Provenance Actions
ocr_postprocess-0.1.2.tar.gz 41.7 kB Without provenance Details for ocr_postprocess-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ocr-postprocess 0.1.2
File Interpreter ABI Platform Actions
ocr_postprocess-0.1.2-py3-none-any.whl Python 3 none any Details for ocr_postprocess-0.1.2-py3-none-any.whl

Total release size: 102.0 kB

Release files / ocr_postprocess-0.1.2.tar.gz

Download URL ocr_postprocess-0.1.2.tar.gz
Size 41.7 kB
Tags Source
SHA-256 checksum
How to use checksums
2b4065ab95a72ef35f8be8d5d664c64b850089d99df6ac70a8edca5641fc9fa2
BLAKE2b-256 checksum
How to use checksums
cb38515b2ce01752395d2a4569301620610d8909e7ecbe4b4d1d1e994aaac39b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.12

Release files / ocr_postprocess-0.1.2-py3-none-any.whl

Download URL ocr_postprocess-0.1.2-py3-none-any.whl
Size 60.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d5b48de2d7dc20c48dfb8f29d6fa438c29791b0d89748e451bbbbfd468194945
BLAKE2b-256 checksum
How to use checksums
afa281df8c7e5da0362d20dcec5c9de54aab906e4dbf90bedeb5bbb3e141071b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.12

Security for ocr-postprocess 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files No provenance

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