加载中…
加载中…
ONNX version of the microsoft/trocr-base-printed model, a TrOCR model for optical character recognition (OCR) on printed text images..
pip install torch transformers optimum-onnx onnxruntime
For GPU inference, install a CUDA-compatible PyTorch build and replace onnxruntime with the compatible onnxruntime-gpu package. Do not install both ONNX Runtime packages in the same environment.
import onnxruntime as ort
from optimum.onnxruntime import ORTModelForVision2Seq
from transformers import TrOCRProcessor
from PIL import Image
model_id = "KvaytG/trocr-base-printed-onnx"
providers = (
["CUDAExecutionProvider", "CPUExecutionProvider"]
if "CUDAExecutionProvider" in ort.get_available_providers()
else ["CPUExecutionProvider"]
)
processor = TrOCRProcessor.from_pretrained(model_id)
model = ORTModelForVision2Seq.from_pretrained(
model_id,
providers=providers,
decoder_file_name="decoder_model.onnx",
decoder_with_past_file_name="decoder_with_past_model.onnx"
)
image = Image.open("image.png").convert("RGB")
inputs = processor(images=image, return_tensors="pt")
generated_ids = model.generate(
inputs.pixel_values,
use_cache=True
)
recognized_text = processor.batch_decode(
generated_ids,
skip_special_tokens=True,
)[0]
print(recognized_text)
Licensed under the MIT license, following the license of the original model.
@misc{kvaytg_trocr_base_printed_onnx,
author = {KvaytG},
title = {TrOCR Base Printed ONNX},
year = {2026},
publisher = {Hugging Face},
journal = {Hugging Face Models},
url = {https://huggingface.co/KvaytG/trocr-base-printed-onnx},
note = {ONNX export of microsoft/trocr-base-printed}
}