RapidOCR-ONNXRuntime
RapidOCR-ONNXRuntime is a Python library providing cross-platform Optical Character Recognition (OCR) capabilities, leveraging the ONNX Runtime inference engine for high-speed and efficient offline deployments. It converts PaddleOCR models to the ONNX format, offering a performant solution for text recognition. The library supports multiple programming languages, with its Python interface primarily integrated into the broader RapidOCR ecosystem. The current PyPI version is 1.4.4, released in January 2025, and the associated GitHub project is actively maintained with frequent updates.
Warnings
- gotcha The `rapidocr-onnxruntime` package (PyPI version 1.4.4) is a core component and backend for the broader `RapidOCR` project on GitHub, which uses a `v3.x.x` versioning scheme. The user-facing API is primarily accessed through the `rapidocr` package (e.g., `from rapidocr import RapidOCR`), which integrates this ONNX Runtime backend.
- gotcha For GPU acceleration, installing `onnxruntime-gpu` is necessary. Simply installing `rapidocr-onnxruntime` will often default to `onnxruntime` (CPU). Additionally, `onnxruntime-directml` (which might be the default on some Windows systems) can lead to slower performance on older hardware.
- gotcha While RapidOCR supports Chinese and English by default, using other languages or custom models often requires specific configurations or 'self-service conversion' of models. Directly using models not from the RapidAI/RapidOCR repository may lead to compatibility issues or require manual patching of configurations.
- gotcha Some users have reported that the `TextDetector` component within RapidOCR, when using the ONNX Runtime backend, can be slower for text detection tasks compared to PaddleOCR's native implementation.
Install
-
pip install rapidocr-onnxruntime
Imports
- RapidOCR
from rapidocr import RapidOCR
Quickstart
from rapidocr import RapidOCR
from PIL import Image
import requests
from io import BytesIO
# Initialize the OCR engine
engine = RapidOCR()
# Example image URL (replace with your image path or URL)
img_url = "https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/resources/test_files/ch_en_num.jpg"
# Download and open the image
response = requests.get(img_url)
image = Image.open(BytesIO(response.content))
# Perform OCR
result = engine(image)
# Print the OCR results
print(result)
# Optionally, visualize the result (requires OpenCV installed and a display environment)
# import cv2
# import numpy as np
# image_np = np.array(image)
# for box, text, score in result:
# box = np.array(box).astype(np.int32).reshape((-1, 1, 2))
# cv2.polylines(image_np, [box], True, (0, 255, 0), 2)
# cv2.putText(image_np, text, (box, box - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
# cv2.imshow("OCR Result", image_np)
# cv2.waitKey(0)
# cv2.destroyAllWindows()