Skip to content

Using Different Inference Engines

Introduction

Starting from rapidocr>=3.0.0, rapidocr can individually specify different inference engines for text detection, text line orientation classification, and text recognition.

For example: text detection uses ONNX Runtime, and text recognition uses PaddlePaddle (params={"Rec.engine_type": EngineType.PADDLE}). At the same time, different versions of OCR can also be flexibly specified via Det.ocr_version.

rapidocr supports 6 inference engines (ONNX Runtime / OpenVINO / PaddlePaddle / PyTorch / MNN (rapidocr>=3.6.0) / TensorRT (rapidocr>=3.7.0)). It is recommended to use the ONNX Runtime CPU version first. The default is ONNX Runtime.

rapidocr selects which inference engine to use by specifying different parameters. Of course, the prerequisite for using different inference engines is that the corresponding inference engine library has been installed beforehand and installed correctly.

Before using different inference engines, please first confirm whether your current rapidocr version supports that inference engine. For details, see: Model List

Using ONNX Runtime

  1. Install ONNX Runtime. It is recommended to use the CPU version of ONNX Runtime; the GPU version is not recommended for use in rapidocr. For the relevant reasons, see: ONNX Runtime GPU Inference.

    pip install onnxruntime
    
  2. ONNX Runtime is the default inference engine and can be used without explicit specification.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    from rapidocr import RapidOCR
    
    engine = RapidOCR()
    
    img_url = "https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/resources/test_files/ch_en_num.jpg"
    result = engine(img_url)
    print(result)
    
    result.vis('vis_result.jpg')
    

    Tip

    Only supported in rapidocr>=3.1.0. ONNX Runtime official documentation: link

    1. Installation

      pip install rapidocr onnxruntime-cann
      
    2. Usage

      1
      2
      3
      4
      5
      6
      7
      8
      9
      from rapidocr import RapidOCR
      
      engine = RapidOCR(params={"EngineConfig.onnxruntime.use_cann": True})
      
      img_url = "https://img1.baidu.com/it/u=3619974146,1266987475&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=516"
      result = engine(img_url)
      print(result)
      
      result.vis("vis_result.jpg")
      

    Tip

    DirectML can only be used on Windows 10 Build 18362 and above. ONNX Runtime official documentation: link

    1. Installation

      pip install rapidocr onnxruntime-directml
      
    2. Usage

      1
      2
      3
      4
      5
      6
      7
      8
      9
      from rapidocr import RapidOCR
      
      engine = RapidOCR(params={"EngineConfig.onnxruntime.use_dml": True})
      
      img_url = "https://img1.baidu.com/it/u=3619974146,1266987475&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=516"
      result = engine(img_url)
      print(result)
      
      result.vis("vis_result.jpg")
      

    Warning

    Only experimentally supported in rapidocr>=3.7.0. ONNX Runtime official documentation: CoreML Execution Provider. For a detailed Benchmark, see: A Look at CoreML Provider vs. CPU Provider Performance in RapidOCR Based on ONNX Runtime

    1. Installation

      Starting from onnxruntime>=1.16, ONNX Runtime itself already supports the CoreML backend on macOS Apple Silicon (M1 / M2 / M3), so there is no need to install the coreml package separately.

      pip install onnxruntime
      
    2. Usage

      1
      2
      3
      4
      5
      6
      7
      8
      9
      from rapidocr import RapidOCR
      
      engine = RapidOCR(params={"EngineConfig.onnxruntime.use_coreml": True})
      
      img_url = "https://img1.baidu.com/it/u=3619974146,1266987475&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=516"
      result = engine(img_url)
      print(result)
      
      result.vis("vis_result.jpg")
      
  3. Check the output log. If Using engine_name: onnxruntime is printed in the following log, it proves that the inference engine in use is ONNX Runtime.

    1
    2
    3
    4
    5
    6
    [INFO] 2025-03-21 09:28:03,457 base.py:30: Using engine_name: onnxruntime
    [INFO] 2025-03-21 09:28:03,553 utils.py:35: File already exists in /Users/joshuawang/projects/_self/RapidOCR/python/rapidocr/models/ch_PP-OCRv4_det_infer.onnx
    [INFO] 2025-03-21 09:28:03,767 base.py:30: Using engine_name: onnxruntime
    [INFO] 2025-03-21 09:28:03,768 utils.py:35: File already exists in /Users/joshuawang/projects/_self/RapidOCR/python/rapidocr/models/ch_ppocr_mobile_v2.0_cls_infer.onnx
    [INFO] 2025-03-21 09:28:03,861 base.py:30: Using engine_name: onnxruntime
    [INFO] 2025-03-21 09:28:03,862 utils.py:35: File already exists in /Users/joshuawang/projects/_self/RapidOCR/python/rapidocr/models/ch_PP-OCRv4_rec_infer.onnx
    

Using OpenVINO

  1. Install OpenVINO.

    pip install openvino
    
  2. Specify OpenVINO as the inference engine.

    from rapidocr import RapidOCR, EngineType
    
    engine = RapidOCR(
        params={
            "Det.engine_type": EngineType.OPENVINO,
            "Cls.engine_type": EngineType.OPENVINO,
            "Rec.engine_type": EngineType.OPENVINO,
        }
    )
    
    img_url = "https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/resources/test_files/ch_en_num.jpg"
    result = engine(img_url)
    print(result)
    
    result.vis('vis_result.jpg')
    
  3. Check the output log. If Using engine_name: openvino is printed in the following log, it proves that the inference engine in use is OpenVINO.

    1
    2
    3
    4
    5
    6
    [INFO] 2025-03-21 09:28:03,457 base.py:30: Using engine_name: openvino
    [INFO] 2025-03-21 09:28:03,553 utils.py:35: File already exists in /Users/joshuawang/projects/_self/RapidOCR/python/rapidocr/models/ch_PP-OCRv4_det_infer.onnx
    [INFO] 2025-03-21 09:28:03,767 base.py:30: Using engine_name: openvino
    [INFO] 2025-03-21 09:28:03,768 utils.py:35: File already exists in /Users/joshuawang/projects/_self/RapidOCR/python/rapidocr/models/ch_ppocr_mobile_v2.0_cls_infer.onnx
    [INFO] 2025-03-21 09:28:03,861 base.py:30: Using engine_name: openvino
    [INFO] 2025-03-21 09:28:03,862 utils.py:35: File already exists in /Users/joshuawang/projects/_self/RapidOCR/python/rapidocr/models/ch_PP-OCRv4_rec_infer.onnx
    

Using MNN

Tip

Supported in rapidocr>=3.6.0.

  1. Install MNN

    pip install MNN
    
  2. Specify MNN as the inference engine.

    from rapidocr import EngineType, RapidOCR
    
    engine = RapidOCR(
        params={
            "Det.engine_type": EngineType.MNN,
            "Cls.engine_type": EngineType.MNN,
            "Rec.engine_type": EngineType.MNN,
        }
    )
    
    img_url = "https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/resources/test_files/ch_en_num.jpg"
    result = engine(img_url)
    print(result)
    
    result.vis('vis_result.jpg')
    

    Coming soon!

  3. Check the output log. If Using engine_name: mnn is printed in the following log, it proves that the inference engine in use is MNN.

    1
    2
    3
    4
    5
    6
    [INFO] 2025-03-21 09:28:03,457 base.py:30: Using engine_name: mnn
    [INFO] 2025-03-21 09:28:03,553 utils.py:35: File already exists in /Users/joshuawang/projects/_self/RapidOCR/python/rapidocr/models/ch_PP-OCRv4_det_infer.mnn
    [INFO] 2025-03-21 09:28:03,767 base.py:30: Using engine_name: mnn
    [INFO] 2025-03-21 09:28:03,768 utils.py:35: File already exists in /Users/joshuawang/projects/_self/RapidOCR/python/rapidocr/models/ch_ppocr_mobile_v2.0_cls_infer.mnn
    [INFO] 2025-03-21 09:28:03,861 base.py:30: Using engine_name: mnn
    [INFO] 2025-03-21 09:28:03,862 utils.py:35: File already exists in /Users/joshuawang/projects/_self/RapidOCR/python/rapidocr/models/ch_PP-OCRv4_rec_infer.mnn
    

Using PaddlePaddle

  1. Install PaddlePaddle.

    See the official PaddlePaddle installation documentation → Quick Install

    You can choose the version you need to install according to your actual situation.

  2. Specify PaddlePaddle as the inference engine.

    from rapidocr import EngineType, RapidOCR
    
    engine = RapidOCR(
        params={
            "Det.engine_type": EngineType.PADDLE,
            "Cls.engine_type": EngineType.PADDLE,
            "Rec.engine_type": EngineType.PADDLE,
        }
    )
    
    img_url = "https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/resources/test_files/ch_en_num.jpg"
    result = engine(img_url)
    print(result)
    
    result.vis('vis_result.jpg')
    
    from rapidocr import EngineType, RapidOCR
    
    engine = RapidOCR(
        params={
        "Det.engine_type": EngineType.PADDLE,
        "EngineConfig.paddle.use_cuda": True,  # Use the PaddlePaddle GPU version for inference
        "EngineConfig.paddle.cuda_ep_cfg.device_id": 0,  # Specify the GPU id
        }
    )
    
    img_url = "https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/resources/test_files/ch_en_num.jpg"
    result = engine(img_url)
    print(result)
    
    result.vis('vis_result.jpg')
    

    Tip

    Only supported in rapidocr>=3.3.0. PaddlePaddle installation documentation for the corresponding version: link

    from rapidocr import EngineType, RapidOCR
    
    engine = RapidOCR(
        params={
        "Det.engine_type": EngineType.PADDLE,
        "EngineConfig.paddle.use_npu": True,  # Use the PaddlePaddle NPU version for inference
        "EngineConfig.paddle.npu_ep_cfg.device_id": 0,  # Specify the NPU id
        }
    )
    
    img_url = "https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/resources/test_files/ch_en_num.jpg"
    result = engine(img_url)
    print(result)
    
    result.vis('vis_result.jpg')
    
  3. Check the output log. If Using engine_name: paddle is printed in the following log, it proves that the inference engine in use is PaddlePaddle.

    1
    2
    3
    4
    5
    6
    7
    8
    [INFO] 2025-03-22 15:20:45,528 utils.py:35: File already exists in /Users/SWHL/projects/RapidOCR/python/rapidocr/models/ch_PP-OCRv4_det_infer/inference.pdmodel
    [INFO] 2025-03-22 15:20:45,529 utils.py:35: File already exists in /Users/SWHL/projects/RapidOCR/python/rapidocr/models/ch_PP-OCRv4_det_infer/inference.pdiparams
    [INFO] 2025-03-22 15:20:45,746 base.py:30: Using engine_name: paddle
    [INFO] 2025-03-22 15:20:45,746 utils.py:35: File already exists in /Users/SWHL/projects/RapidOCR/python/rapidocr/models/ch_ppocr_mobile_v2_cls_infer/inference.pdmodel
    [INFO] 2025-03-22 15:20:45,746 utils.py:35: File already exists in /Users/SWHL/projects/RapidOCR/python/rapidocr/models/ch_ppocr_mobile_v2_cls_infer/inference.pdiparams
    [INFO] 2025-03-22 15:20:45,903 base.py:30: Using engine_name: paddle
    [INFO] 2025-03-22 15:20:45,904 utils.py:35: File already exists in /Users/SWHL/projects/RapidOCR/python/rapidocr/models/ch_PP-OCRv4_rec_infer/inference.pdmodel
    [INFO] 2025-03-22 15:20:45,904 utils.py:35: File already exists in /Users/SWHL/projects/RapidOCR/python/rapidocr/models/ch_PP-OCRv4_rec_infer/inference.pdiparams
    

Using PyTorch

  1. Install PyTorch.

    Refer to the official PyTorch installation documentation → Install PyTorch.

    You can choose the version you need to install according to your actual situation.

  2. Specify PyTorch as the inference engine.

    from rapidocr import RapidOCR
    
    engine = RapidOCR(
        params={
            "Det.engine_type": EngineType.TORCH,
            "Cls.engine_type": EngineType.TORCH,
            "Rec.engine_type": EngineType.TORCH,
        }
    )
    
    img_url = "https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/resources/test_files/ch_en_num.jpg"
    result = engine(img_url)
    print(result)
    
    result.vis('vis_result.jpg')
    
    from rapidocr import EngineType, RapidOCR
    
    engine = RapidOCR(
        params={
            "Det.engine_type": EngineType.TORCH,
            "Cls.engine_type": EngineType.TORCH,
            "Rec.engine_type": EngineType.TORCH,
            "EngineConfig.torch.use_cuda": True,  # Use the torch GPU version for inference
            "EngineConfig.torch.cuda_ep_cfg.device_id": 0,  # Specify the GPU id
        }
    )
    
    img_url = "https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/resources/test_files/ch_en_num.jpg"
    result = engine(img_url)
    print(result)
    
    result.vis('vis_result.jpg')
    

    Tip

    Only supported in rapidocr>=3.4.2. Official torch_npu documentation: link

    1. Install torch_npu; refer to: docs.

    2. Usage

      from rapidocr import EngineType, RapidOCR
      
      engine = RapidOCR(
          params={
              "Det.engine_type": EngineType.TORCH,
              "Cls.engine_type": EngineType.TORCH,
              "Rec.engine_type": EngineType.TORCH,
              "EngineConfig.torch.use_npu": True,  # Use the torch NPU version for inference
              "EngineConfig.torch.npu_ep_cfg.device_id": 0,  # Specify the NPU id
          }
      )
      
      img_url = "https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/resources/test_files/ch_en_num.jpg"
      result = engine(img_url)
      print(result)
      
      result.vis('vis_result.jpg')
      

    Tip

    Only supported in rapidocr>=3.7.0. For detailed benchmark data, see: RapidOCR supports PyTorch MPS as a compute backend

    1. Install torch; refer to: docs. After installation, you can run the script below to determine whether the machine can use the Apple GPU:

      import torch
      
      print(f"PyTorch version: {torch.__version__}")
      print(f"MPS available: {torch.backends.mps.is_available()}")
      
      if torch.backends.mps.is_available():
          device = torch.device("mps")
          print(f"✅ Successfully using the Apple GPU (MPS): {device}")
      else:
          print("❌ Please check your PyTorch installation; you may need to reinstall a build that supports MPS.")
      

      Output similar to the following indicates that the machine can use the Apple GPU:

      1
      2
      3
      PyTorch version: 2.6.0
      MPS available: True
      ✅ Successfully using the Apple GPU (MPS): mps
      
    2. Usage

      from rapidocr import EngineType, RapidOCR
      
      engine = RapidOCR(
          params={
              "Det.engine_type": EngineType.TORCH,
              "Cls.engine_type": EngineType.TORCH,
              "Rec.engine_type": EngineType.TORCH,
              "EngineConfig.torch.use_mps": True,  # Use the torch MPS version for inference
          }
      )
      
      img_url = "https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/resources/test_files/ch_en_num.jpg"
      result = engine(img_url)
      print(result)
      
      result.vis('vis_result.jpg')
      
  3. Check the output log. If the following log prints Using engine_name: torch, it proves that the inference engine in use is PyTorch.

    1
    2
    3
    4
    5
    6
    [INFO] 2025-03-22 15:39:13,241 base.py:30: Using engine_name: torch
    [INFO] 2025-03-22 15:39:13,956 utils.py:35: File already exists in .../site-packages/rapidocr/models/ch_PP-OCRv4_det_infer.pth
    [INFO] 2025-03-22 15:39:14,136 base.py:30: Using engine_name: torch
    [INFO] 2025-03-22 15:39:14,136 utils.py:35: File already exists in .../site-packages/rapidocr/models/ch_ptocr_mobile_v2.0_cls_infer.pth
    [INFO] 2025-03-22 15:39:14,168 base.py:30: Using engine_name: torch
    [INFO] 2025-03-22 15:39:14,168 utils.py:35: File already exists in .../site-packages/rapidocr/models/ch_PP-OCRv4_rec_infer.pth
    

Using TensorRT

Tip

Supported in rapidocr>=3.7.0. Related blog: RapidOCR supports the TensorRT inference engine.

  1. Install the TensorRT runtime environment.

    I only provide a reference configuration environment here:

    You can choose the version you need to install according to your actual situation.

  2. Specify TensorRT as the inference engine.

    Run the following code; the program will automatically download the corresponding ONNX models and convert them to .engine format. This conversion is only performed on the first run; therefore, the first run is slow, and subsequent runs will be significantly faster.

    For detailed model support, see the blog: RapidOCR supports the TensorRT inference engine

    from rapidocr import EngineType, RapidOCR
    
    engine = RapidOCR(
        params={
            "Det.engine_type": EngineType.TENSORRT,
            "Cls.engine_type": EngineType.TENSORRT,
            "Rec.engine_type": EngineType.TENSORRT,
            "EngineConfig.tensorrt.use_fp16": False,
            "EngineConfig.tensorrt.device_id": 0,  # Specify the GPU id
        }
    )
    
    img_url = "https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/resources/test_files/ch_en_num.jpg"
    result = engine(img_url)
    print(result)
    
    result.vis('vis_result.jpg')
    
  3. Check the output log. If the following log prints Using engine_name: tensorrt, it proves that the inference engine in use is PyTorch.

    1
    2
    3
    4
    5
    6
    [INFO] 2025-03-22 15:39:13,241 base.py:30: Using engine_name: tensorrt
    [INFO] 2025-03-22 15:39:13,956 utils.py:35: File already exists in /Users/SWHL/projects/RapidOCR/python/rapidocr/models/ch_PP-OCRv4_det_mobile_sm80_fp32.engine
    [INFO] 2025-03-22 15:39:14,136 base.py:30: Using engine_name: tensorrt
    [INFO] 2025-03-22 15:39:14,136 utils.py:35: File already exists in /Users/SWHL/projects/RapidOCR/python/rapidocr/models/ch_ptocr_mobile_v2.0_cls_sm80_fp32.engine
    [INFO] 2025-03-22 15:39:14,168 base.py:30: Using engine_name: tensorrt
    [INFO] 2025-03-22 15:39:14,168 utils.py:35: File already exists in /Users/SWHL/projects/RapidOCR/python/rapidocr/models/ch_PP-OCRv4_rec_mobile_sm80_fp32.engine
    

Comments