Skip to main content

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

Project description

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.

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

ocr_postprocess-0.1.0.tar.gz (41.2 kB view details)

Uploaded Source

Built Distribution

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

ocr_postprocess-0.1.0-py3-none-any.whl (59.1 kB view details)

Uploaded Python 3

File details

Details for the file ocr_postprocess-0.1.0.tar.gz.

File metadata

  • Download URL: ocr_postprocess-0.1.0.tar.gz
  • Upload date:
  • Size: 41.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for ocr_postprocess-0.1.0.tar.gz
Algorithm Hash digest
SHA256 12af7e967c477796e016c12c0afc7771cfd55c602e0b5252b7906023a24a8cbb
MD5 ad54b92def9f899d51b09a708ae654cd
BLAKE2b-256 1f1ddd75f9d1040df5adb4a25252685c181d5f705bd58572a51ff85fd479f89d

See more details on using hashes here.

File details

Details for the file ocr_postprocess-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for ocr_postprocess-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ad1428ce0853a15cd40307caea87800dd25f8ea300342c64deda1e8a5dba7965
MD5 c85d309bafb7277ef55cf3b38eb8b51e
BLAKE2b-256 a75845b8dfbfdd0ebc65e95ee8c6e1c52ab3158b880b2e417083610659b90672

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