TrueOCR
OCR tiếng Việt chạy trên CPU bằng ONNX Runtime. Không cần GPU hay PyTorch. Đọc ảnh nhiều dòng (tự xoay, khử nghiêng, phát hiện dòng, sắp thứ tự đọc) hoặc ảnh đã crop sát một dòng chữ.
Cài đặt
pip install trueocr # khi gói đã có trên PyPI
pip install -e . # từ source
pip install "trueocr[notebook]" # thêm IPython cho result.show()
Python 3.10 trở lên. Phụ thuộc: NumPy, OpenCV (headless), ONNX Runtime, Pillow.
Model
Gói kèm sẵn weights trong trueocr/models/ (~8 MB), cài xong dùng được ngay.
Thư mục model gồm:
| File | Vai trò |
|---|---|
rec.onnx |
Nhận dạng ký tự trên crop từng dòng (CTC) |
det.onnx |
Phát hiện bbox từng dòng chữ |
orient.onnx |
Xác định hướng xoay của ảnh |
order.onnx |
Sắp thứ tự đọc các dòng |
meta.json |
Tên model, bảng ký tự, tham số tiền xử lý |
Chế độ line chỉ cần rec.onnx và meta.json. Dùng model khác thì truyền
đường dẫn bằng model_dir= (Python) hoặc --model-dir (CLI).
Dùng nhanh
from trueocr import load_model
model = load_model(model_dir="/duong/dan/models")
result = model.predict("examples/sample.png")
print(result.text)
for line in result.lines:
print(line["bbox"], line["confidence"], line["text"])
Model nạp một lần, dùng lại cho các ảnh sau. predict nhận đường dẫn, bytes
ảnh đã mã hóa hoặc ảnh PIL. Trong Jupyter, result.show() hiển thị text, ảnh
khung dòng và bảng confidence. result.save("thu_muc") lưu .txt UTF-8,
.json và ảnh khung dòng.
trueocr anh.jpg --model-dir /duong/dan/models # in text
trueocr crop.png --model-dir /duong/dan/models --mode line # crop một dòng
trueocr *.jpg --model-dir /duong/dan/models --json --out ket_qua/
Chỉ cần đọc từng crop một dòng, không cần bbox: dùng thẳng Recognizer.
from trueocr import Recognizer
ocr = Recognizer("/duong/dan/models", threads=1)
predictions = ocr.predict([bgr_array, png_bytes, "crop.jpg"], batch_size=32)
print(predictions[0].text, predictions[0].confidence)
Hai chế độ
page (mặc định), cho ảnh nhiều dòng:
- Đọc ảnh, áp EXIF, ghép nền trắng cho ảnh trong suốt, chuyển grayscale.
- Ước lượng độ nghiêng (tối đa 45°),
orient.onnxxác định hướng xoay, rồi xoay và khử nghiêng. det.onnxphát hiện bbox từng dòng; ảnh chữ nhỏ được phát hiện lại ở độ phân giải cao hơn.- Cắt từng dòng,
rec.onnxnhận dạng, giải mã CTC greedy, chuẩn hóa Unicode NFC. order.onnxgom khối và sắp thứ tự đọc; ghép các dòng bằng ký tự xuống dòng.
line, cho ảnh đã crop sát một dòng nằm ngang: bỏ qua bước 2, 3 và 5.
model.predict("crop.png", mode="line")
Kết quả
result.data (cũng là JSON được lưu) gồm:
text: toàn bộ text, mỗi dòng một hàng.lines:index,text,confidence,bbox[x0, y0, x1, y1],low_confidence.rotation_clockwise_degrees,skew_degrees: góc đã xoay và khử nghiêng.bbox_coordinate_system: ở chế độpage, bbox thuộc ảnh đã xoay/khử nghiêng (result.image); ở chế độline, bbox thuộc ảnh gốc sau khi áp EXIF.model,mode,image_size,elapsed_ms,created_at.
confidence là trung bình xác suất đỉnh CTC, chưa hiệu chuẩn thành xác suất
text đúng. low_confidence chỉ đánh dấu dòng cần xem lại, text vẫn được giữ.
Cấu hình
from trueocr import OCRConfig, load_model
model = load_model(OCRConfig(model_dir="/duong/dan/models", threads=2, low_confidence=0.9))
model = load_model(model_dir="/duong/dan/models", mode="line") # ghi đè từng trường
| Trường | Mặc định | Ý nghĩa |
|---|---|---|
model_dir |
trueocr/models trong gói |
Thư mục model |
mode |
"page" |
"page" hoặc "line" |
threads |
min(4, số CPU) |
Số CPU thread của ONNX Runtime |
batch_size |
32 |
Số dòng mỗi lần chạy recognizer |
auto_rotate |
True |
Dùng orient.onnx để xoay ảnh |
low_confidence |
0.80 |
Ngưỡng đánh dấu low_confidence |
save_outputs |
False |
Tự lưu kết quả sau mỗi predict |
output_dir |
trueocr_outputs |
Nơi lưu khi save_outputs=True |
Chất lượng và giới hạn
Model trueocr-vi-r01 có 1.060.035 tham số, file rec.onnx FP32 4,08 MiB.
Trên 22.280 crop test nội bộ (giấy tờ tùy thân và hóa đơn), model đọc đúng toàn
dòng 96,72%, tỷ lệ lỗi ký tự (CER) 0,66%. Tốc độ trung vị 1,62 ms/crop (p95
2,46 ms) với 1 thread CPU Intel Xeon Platinum 8358P, batch 1.
- Recognizer chỉ đọc tiếng Việt và ký tự Latin cơ bản (222 ký tự), không dùng language model.
- Crop một dòng cần sát chữ; lề quá rộng hoặc nhiều dòng có thể sinh thêm ký tự.
- Model có thể nhận sai dấu tiếng Việt, nhất là ở font và bố cục khác dữ liệu huấn luyện.
- Chất lượng chế độ
pagephụ thuộc cả bước phát hiện và cắt dòng, không suy ra trực tiếp từ điểm trên crop.
Phát triển
pip install -e ".[test]"
TRUEOCR_MODEL_DIR=/duong/dan/models pytest
Thiếu TRUEOCR_MODEL_DIR thì các test cần model được bỏ qua.
Phát hành lên PyPI:
python -m pip install build twine
python -m build
twine check dist/*
twine upload dist/*
Weights không nằm trong git: chỉ build từ thư mục đã có đủ
file trong trueocr/models/, và kiểm tra unzip -l dist/*.whl | grep models
trước khi upload.
License
Metadata
Release files for trueocr 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| trueocr-0.1.0.tar.gz | 7.4 MB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| trueocr-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 14.8 MB
Release files / trueocr-0.1.0.tar.gz
| Download URL | trueocr-0.1.0.tar.gz |
|---|---|
| Size | 7.4 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9e92b90e02a694e30bdc5db9273a6b97c9a7f2dddfbdceb42cf005d529a48f36
|
|
BLAKE2b-256 checksum How to use checksums |
24001b101581581bc4d15c1610d45c4af2d3a965ed7fa6156aca1f15f36715f7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.10.19
|
Release files / trueocr-0.1.0-py3-none-any.whl
| Download URL | trueocr-0.1.0-py3-none-any.whl |
|---|---|
| Size | 7.4 MB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bf7f809adff1bd0713d23b4dd3fea4f5dc6cd9aea170c2884ce95b5ac891aaf2
|
|
BLAKE2b-256 checksum How to use checksums |
be2a35285b70c8408825bb2110de868f2deb6812c515ba96e019ad7cc3829921
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.10.19
|
Security for trueocr 0.1.0
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.