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
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.
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.
fromrapidocrimportEngineType,RapidOCRengine=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
fromrapidocrimportEngineType,RapidOCRengine=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')
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.
fromrapidocrimportEngineType,RapidOCRengine=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
fromrapidocrimportEngineType,RapidOCRengine=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')
importtorchprint(f"PyTorch version: {torch.__version__}")print(f"MPS available: {torch.backends.mps.is_available()}")iftorch.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:
fromrapidocrimportEngineType,RapidOCRengine=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')
Check the output log. If the following log prints Using engine_name: torch, it proves that the inference engine in use is PyTorch.
You can choose the version you need to install according to your actual situation.
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.
fromrapidocrimportEngineType,RapidOCRengine=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')
Check the output log. If the following log prints Using engine_name: tensorrt, it proves that the inference engine in use is PyTorch.