RapidOCR
RapidOCR is an open-source, multi-platform, multi-language Optical Character Recognition (OCR) toolkit designed for fast and offline deployment. It leverages various inference engines like ONNX Runtime, OpenVINO, MNN, PaddlePaddle, TensorRT, and PyTorch, offering both speed and extensive compatibility by converting PaddleOCR models to ONNX format. The library is currently at version 3.8.1 and maintains a very active release cadence.
Warnings
- gotcha RapidOCR automatically downloads models upon first initialization. This can lead to unexpected network requests in offline environments or CI/CD pipelines. In some integrated setups, models might still be downloaded even if OCR processing is explicitly disabled (`do_ocr=False`).
- breaking Starting from v3.4.0, the `RapidOCR` constructor gained new `params` for explicit model selection, including `LangRec`, `ModelType`, and `OCRVersion` to support new PaddleOCRv5 models for languages like English, Thai, and Greek. Older code relying on implicit language detection or default models for these languages might need adjustment.
- gotcha GPU acceleration requires careful setup. If you've installed the CPU version of `onnxruntime`, you must `pip uninstall onnxruntime` before installing `onnxruntime-gpu` to ensure the correct inference provider is used. DirectML for Windows GPUs is noted to affect speed on older devices.
- gotcha RapidOCR is known to have issues when deployed on read-only filesystems (e.g., Databricks environments) due to its model caching mechanism.
Install
-
pip install rapidocr onnxruntime -
pip install rapidocr rapidocr_openvino -
pip install rapidocr rapidocr_paddle -
pip install rapidocr rapidocr_pytorch
Imports
- RapidOCR
from rapidocr import RapidOCR
- LangRec, ModelType, OCRVersion
from rapidocr import LangRec, ModelType, OCRVersion
Quickstart
import os
from rapidocr import RapidOCR
# Initialize the OCR engine. This will automatically download models on first run.
# Ensure 'onnxruntime' or another backend is installed (e.g., pip install rapidocr onnxruntime)
engine = RapidOCR()
# Example image from a public URL
img_url = "https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/resources/test_files/ch_en_num.jpg"
# Process the image
result = engine(img_url)
# Print the extracted text results
for line in result:
# Each line typically contains bounding box, text, and confidence
if len(line) >= 2:
print(f"Text: {line[1]}")
# You can also visualize the results (requires OpenCV)
# result.vis("vis_result.jpg")
# print("Visualization saved to vis_result.jpg")