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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ocr_postprocess-0.1.2.tar.gz.
File metadata
- Download URL: ocr_postprocess-0.1.2.tar.gz
- Upload date:
- Size: 41.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2b4065ab95a72ef35f8be8d5d664c64b850089d99df6ac70a8edca5641fc9fa2
|
|
| MD5 |
f97bc895136e936f0e3e4856adddaae8
|
|
| BLAKE2b-256 |
cb38515b2ce01752395d2a4569301620610d8909e7ecbe4b4d1d1e994aaac39b
|
File details
Details for the file ocr_postprocess-0.1.2-py3-none-any.whl.
File metadata
- Download URL: ocr_postprocess-0.1.2-py3-none-any.whl
- Upload date:
- Size: 60.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d5b48de2d7dc20c48dfb8f29d6fa438c29791b0d89748e451bbbbfd468194945
|
|
| MD5 |
0926c28e987f110fd3ee398614661988
|
|
| BLAKE2b-256 |
afa281df8c7e5da0362d20dcec5c9de54aab906e4dbf90bedeb5bbb3e141071b
|