Text Recognition Module Tutorial¶
1. Overview¶
The text recognition module is the core component of an OCR (Optical Character Recognition) system, responsible for extracting text information from text regions within images. The performance of this module directly impacts the accuracy and efficiency of the entire OCR system. Typically, the text recognition module takes the bounding boxes of text regions output by the text detection module as input and then converts the text in the images into editable and searchable electronic text through complex image processing and deep learning algorithms. The accuracy of the text recognition results is crucial for subsequent applications such as information extraction and data mining.
2. List of Supported Models¶
Model | Model Download Links | Recognition Avg Accuracy(%) | GPU Inference Time (ms) [Normal Mode / High-Performance Mode] |
CPU Inference Time (ms) [Normal Mode / High-Performance Mode] |
Model Storage Size (M) | Introduction |
---|---|---|---|---|---|---|
PP-OCRv5_server_rec | Inference Model/Pretrained Model | 86.38 | 8.45/2.36 | 122.69/122.69 | 81 M | PP-OCRv5_rec is a next-generation text recognition model. It aims to efficiently and accurately support the recognition of four major languages—Simplified Chinese, Traditional Chinese, English, and Japanese—as well as complex text scenarios such as handwriting, vertical text, pinyin, and rare characters using a single model. While maintaining recognition performance, it balances inference speed and model robustness, providing efficient and accurate technical support for document understanding in various scenarios. |
PP-OCRv5_mobile_rec | Inference Model/Pretrained Model | 81.29 | 1.46/5.43 | 5.32/91.79 | 16 M | |
PP-OCRv4_server_rec_doc | Inference Model/Pretrained Model | 86.58 | 6.65 / 2.38 | 32.92 / 32.92 | 91 M | PP-OCRv4_server_rec_doc is trained on a mixed dataset of more Chinese document data and PP-OCR training data, building upon PP-OCRv4_server_rec. It enhances the recognition capabilities for some Traditional Chinese characters, Japanese characters, and special symbols, supporting over 15,000 characters. In addition to improving document-related text recognition, it also enhances general text recognition capabilities. |
PP-OCRv4_mobile_rec | Inference Model/Pretrained Model | 83.28 | 4.82 / 1.20 | 16.74 / 4.64 | 11 M | A lightweight recognition model of PP-OCRv4 with high inference efficiency, suitable for deployment on various hardware devices, including edge devices. |
PP-OCRv4_server_rec | Inference Model/Pretrained Model | 85.19 | 6.58 / 2.43 | 33.17 / 33.17 | 87 M | The server-side model of PP-OCRv4, offering high inference accuracy and deployable on various servers. |
en_PP-OCRv4_mobile_rec | Inference Model/Pretrained Model | 70.39 | 4.81 / 0.75 | 16.10 / 5.31 | 7.3 M | An ultra-lightweight English recognition model trained based on the PP-OCRv4 recognition model, supporting English and numeric character recognition. |
❗ The above section lists the 6 core models that are primarily supported by the text recognition module. In total, the module supports 20 comprehensive models, including multiple multilingual text recognition models. Below is the complete list of models:
👉Details of the Model List
* PP-OCRv5 Multi-Scenario ModelsModel | Model Download Links | Avg Accuracy for Chinese Recognition (%) | Avg Accuracy for English Recognition (%) | Avg Accuracy for Traditional Chinese Recognition (%) | Avg Accuracy for Japanese Recognition (%) | GPU Inference Time (ms) [Normal Mode / High-Performance Mode] |
CPU Inference Time (ms) [Normal Mode / High-Performance Mode] |
Model Storage Size (M) | Introduction |
---|---|---|---|---|---|---|---|---|---|
PP-OCRv5_server_rec | Inference Model/Pretrained Model | 86.38 | 64.70 | 93.29 | 60.35 | 8.45/2.36 | 122.69/122.69 | 81 M | PP-OCRv5_rec is a next-generation text recognition model. It aims to efficiently and accurately support the recognition of four major languages—Simplified Chinese, Traditional Chinese, English, and Japanese—as well as complex text scenarios such as handwriting, vertical text, pinyin, and rare characters using a single model. While maintaining recognition performance, it balances inference speed and model robustness, providing efficient and accurate technical support for document understanding in various scenarios. |
PP-OCRv5_mobile_rec | Inference Model/Pretrained Model | 81.29 | 66.00 | 83.55 | 54.65 | 1.46/5.43 | 5.32/91.79 | 16 M |
Model | Model Download Links | Avg Accuracy (%) | GPU Inference Time (ms) [Normal Mode / High-Performance Mode] |
CPU Inference Time (ms) [Normal Mode / High-Performance Mode] |
Model Storage Size (M) | Introduction |
---|---|---|---|---|---|---|
PP-OCRv4_server_rec_doc | Inference Model/Pretrained Model | 86.58 | 6.65 / 2.38 | 32.92 / 32.92 | 91 M | PP-OCRv4_server_rec_doc is trained on a mixed dataset of more Chinese document data and PP-OCR training data, building upon PP-OCRv4_server_rec. It enhances the recognition capabilities for some Traditional Chinese characters, Japanese characters, and special symbols, supporting over 15,000 characters. In addition to improving document-related text recognition, it also enhances general text recognition capabilities. |
PP-OCRv4_mobile_rec | Inference Model/Pretrained Model | 83.28 | 4.82 / 1.20 | 16.74 / 4.64 | 11 M | A lightweight recognition model of PP-OCRv4 with high inference efficiency, suitable for deployment on various hardware devices, including edge devices. |
PP-OCRv4_server_rec | Inference Model/Pretrained Model | 85.19 | 6.58 / 2.43 | 33.17 / 33.17 | 87 M | The server-side model of PP-OCRv4, offering high inference accuracy and deployable on various servers. |
PP-OCRv3_mobile_rec | Inference Model/Pretrained Model | 75.43 | 5.87 / 1.19 | 9.07 / 4.28 | 11 M | A lightweight recognition model of PP-OCRv3 with high inference efficiency, suitable for deployment on various hardware devices, including edge devices. |
Model | Model Download Links | Avg Accuracy (%) | GPU Inference Time (ms) [Normal Mode / High-Performance Mode] |
CPU Inference Time (ms) [Normal Mode / High-Performance Mode] |
Model Storage Size (M) | Introduction |
---|---|---|---|---|---|---|
ch_SVTRv2_rec | Inference Model/Pretrained Model | 68.81 | 8.08 / 2.74 | 50.17 / 42.50 | 73.9 M | SVTRv2 is a server-side text recognition model developed by the OpenOCR team of the Vision and Learning Lab (FVL) at Fudan University. It won the first prize in the PaddleOCR Algorithm Model Challenge - Task 1: OCR End-to-End Recognition Task, with a 6% improvement in end-to-end recognition accuracy on Leaderboard A compared to PP-OCRv4. |
Model | Model Download Links | Avg Accuracy (%) | GPU Inference Time (ms) [Normal Mode / High-Performance Mode] |
CPU Inference Time (ms) [Normal Mode / High-Performance Mode] |
Model Storage Size (M) | Introduction |
---|---|---|---|---|---|---|
ch_RepSVTR_rec | Inference Model/Pretrained Model | 65.07 | 5.93 / 1.62 | 20.73 / 7.32 | 22.1 M | RepSVTR is a mobile-side text recognition model based on SVTRv2. It won the first prize in the PaddleOCR Algorithm Model Challenge - Task 1: OCR End-to-End Recognition Task, with a 2.5% improvement in end-to-end recognition accuracy on Leaderboard B compared to PP-OCRv4, while maintaining similar inference speed. |
Model | Model Download Links | Avg Accuracy (%) | GPU Inference Time (ms) [Normal Mode / High-Performance Mode] |
CPU Inference Time (ms) [Normal Mode / High-Performance Mode] |
Model Storage Size (M) | Introduction |
---|---|---|---|---|---|---|
en_PP-OCRv4_mobile_rec | Inference Model/Pretrained Model | 70.39 | 4.81 / 0.75 | 16.10 / 5.31 | 6.8 M | An ultra-lightweight English recognition model trained based on the PP-OCRv4 recognition model, supporting English and numeric character recognition. |
en_PP-OCRv3_mobile_rec | Inference Model/Pretrained Model | 70.69 | 5.44 / 0.75 | 8.65 / 5.57 | 7.8 M | An ultra-lightweight English recognition model trained based on the PP-OCRv3 recognition model, supporting English and numeric character recognition. |
Model | Model Download Links | Avg Accuracy (%) | GPU Inference Time (ms) [Normal Mode / High-Performance Mode] |
CPU Inference Time (ms) [Normal Mode / High-Performance Mode] |
Model Storage Size (M) | Introduction |
---|---|---|---|---|---|---|
korean_PP-OCRv3_mobile_rec | Inference Model/Pretrained Model | 60.21 | 5.40 / 0.97 | 9.11 / 4.05 | 8.6 M | An ultra-lightweight Korean recognition model trained based on the PP-OCRv3 recognition model, supporting Korean and numeric character recognition. |
japan_PP-OCRv3_mobile_rec | Inference Model/Pretrained Model | 45.69 | 5.70 / 1.02 | 8.48 / 4.07 | 8.8 M | An ultra-lightweight Japanese recognition model trained based on the PP-OCRv3 recognition model, supporting Japanese and numeric character recognition. |
chinese_cht_PP-OCRv3_mobile_rec | Inference Model/Pretrained Model | 82.06 | 5.90 / 1.28 | 9.28 / 4.34 | 9.7 M | An ultra-lightweight Traditional Chinese recognition model trained based on the PP-OCRv3 recognition model, supporting Traditional Chinese and numeric character recognition. |
te_PP-OCRv3_mobile_rec | Inference Model/Pretrained Model | 95.88 | 5.42 / 0.82 | 8.10 / 6.91 | 7.8 M | An ultra-lightweight Telugu recognition model trained based on the PP-OCRv3 recognition model, supporting Telugu and numeric character recognition. |
ka_PP-OCRv3_mobile_rec | Inference Model/Pretrained Model | 96.96 | 5.25 / 0.79 | 9.09 / 3.86 | 8.0 M | An ultra-lightweight Kannada recognition model trained based on the PP-OCRv3 recognition model, supporting Kannada and numeric character recognition. |
ta_PP-OCRv3_mobile_rec | Inference Model/Pretrained Model | 76.83 | 5.23 / 0.75 | 10.13 / 4.30 | 8.0 M | An ultra-lightweight Tamil recognition model trained based on the PP-OCRv3 recognition model, supporting Tamil and numeric character recognition. |
latin_PP-OCRv3_mobile_rec | Inference Model/Pretrained Model | 76.93 | 5.20 / 0.79 | 8.83 / 7.15 | 7.8 M | An ultra-lightweight Latin recognition model trained based on the PP-OCRv3 recognition model, supporting Latin and numeric character recognition. |
arabic_PP-OCRv3_mobile_rec | Inference Model/Pretrained Model | 73.55 | 5.35 / 0.79 | 8.80 / 4.56 | 7.8 M | An ultra-lightweight Arabic alphabet recognition model trained based on the PP-OCRv3 recognition model, supporting Arabic alphabet and numeric character recognition. |
cyrillic_PP-OCRv3_mobile_rec | Inference Model/Pretrained Model | 94.28 | 5.23 / 0.76 | 8.89 / 3.88 | 7.9 M | An ultra-lightweight Cyrillic alphabet recognition model trained based on the PP-OCRv3 recognition model, supporting Cyrillic alphabet and numeric character recognition. |
devanagari_PP-OCRv3_mobile_rec | Inference Model/Pretrained Model | 96.44 | 5.22 / 0.79 | 8.56 / 4.06 | 7.9 M | An ultra-lightweight Devanagari alphabet recognition model trained based on the PP-OCRv3 recognition model, supporting Devanagari alphabet and numeric character recognition. |
- Performance Test Environment
- Test Dataset:
- Chinese Recognition Models: A self-built Chinese dataset by PaddleOCR, covering various scenarios such as street views, online images, documents, and handwriting, with 11,000 images for text recognition.
- ch_SVTRv2_rec: The evaluation set of Leaderboard A in the PaddleOCR Algorithm Model Challenge - Task 1: OCR End-to-End Recognition Task.
- ch_RepSVTR_rec: The evaluation set of Leaderboard B in the PaddleOCR Algorithm Model Challenge - Task 1: OCR End-to-End Recognition Task.
- English Recognition Models: A self-built English dataset by PaddleX.
- Multilingual Recognition Models: A self-built multilingual dataset by PaddleX.
- Hardware Configuration:
- GPU: NVIDIA Tesla T4
- CPU: Intel Xeon Gold 6271C @ 2.60GHz
- Other Environment: Ubuntu 20.04 / cuDNN 8.6 / TensorRT 8.5.2.2
- Test Dataset:
- Explanation of Inference Modes
Mode | GPU Configuration | CPU Configuration | Acceleration Technology Combination |
---|---|---|---|
Normal Mode | FP32 Precision / No TRT Acceleration | FP32 Precision / 8 Threads | PaddleInference |
High-Performance Mode | Optimal combination of precision type and acceleration strategy | FP32 Precision / 8 Threads | Selection of the optimal backend (Paddle/OpenVINO/TRT, etc.) |
III. Quick Start¶
❗ Before starting, please install the wheel package of PaddleOCR. For detailed instructions, refer to the Installation Guide.
You can quickly experience the functionality with a single command:
paddleocr text_recognition -i https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/general_ocr_rec_001.png
You can also integrate the model inference of the text recognition module into your project. Before running the following code, please download the sample image to your local machine.
from paddleocr import TextRecognition
model = TextRecognition(model_name="PP-OCRv5_server_rec")
output = model.predict(input="general_ocr_rec_001.png", batch_size=1)
for res in output:
res.print()
res.save_to_img(save_path="./output/")
res.save_to_json(save_path="./output/res.json")
After running, the obtained result is as follows:
{'res': {'input_path': 'general_ocr_rec_001.png', 'page_index': None, 'rec_text': '绿洲仕格维花园公寓', 'rec_score': 0.9823867082595825}}
The meanings of the parameters in the running result are as follows:
- input_path
: Indicates the path to the input image containing the text line to be predicted.
- page_index
: If the input is a PDF file, it indicates which page of the PDF the current text line is from; otherwise, it is None
.
- rec_text
: Indicates the predicted text of the text line image.
- rec_score
: Indicates the confidence score of the predicted text for the text line image.
The visualized image is as follows:
The descriptions of relevant methods and parameters are as follows:
- Instantiate a text recognition model using
TextRecognition
(takingPP-OCRv5_server_rec
as an example here). The specific descriptions are as follows:
Parameter | Description | Type | Options | Default Value |
---|---|---|---|---|
model_name |
Model name | str |
All model names supported by PaddleX | None |
model_dir |
Model storage path | str |
None | None |
device |
Model inference device | str |
Supports specifying specific GPU card numbers, such as "gpu:0", specific card numbers for other hardware, such as "npu:0", and "cpu" for CPU. | gpu:0 |
use_hpip |
Whether to enable the high-performance inference plugin | bool |
None | False |
hpi_config |
High-performance inference configuration | dict | None |
None | None |
-
Among them,
model_name
must be specified. After specifyingmodel_name
, the default model parameters built into PaddleX are used. On this basis, whenmodel_dir
is specified, the user-defined model is used. -
Call the
predict()
method of the text recognition model for inference prediction. This method returns a list of results. In addition, this module also provides thepredict_iter()
method. The two methods are completely consistent in terms of parameter acceptance and result return. The difference is thatpredict_iter()
returns agenerator
, which can process and obtain prediction results step by step. It is suitable for scenarios where large datasets need to be processed or memory savings are desired. You can choose either of these two methods according to your actual needs. The parameters of thepredict()
method includeinput
andbatch_size
, with specific descriptions as follows:
Parameter | Description | Type | Options | Default Value |
---|---|---|---|---|
input |
Data to be predicted, supporting multiple input types | Python Var /str /list |
|
None |
batch_size |
Batch size | int |
Any integer | 1 |
- Process the prediction results. The prediction result for each sample is a corresponding Result object, which supports operations such as printing, saving as an image, and saving as a
json
file:
Method | Description | Parameter | Type | Description | Default Value |
---|---|---|---|---|---|
print() |
Print the result to the terminal | format_json |
bool |
Whether to format the output content using JSON indentation |
True |
indent |
int |
Specifies the indentation level to beautify the output JSON data, making it more readable. Only effective when format_json is True . |
4 | ||
ensure_ascii |
bool |
Controls whether to escape non-ASCII characters as Unicode . When set to True , all non-ASCII characters will be escaped; False retains the original characters. Only effective when format_json is True . |
False |
||
save_to_json() |
Save the result as a file in json format |
save_path |
str |
The file path to save the result. When it is a directory, the saved file name is consistent with the naming of the input file type. | None |
indent |
int |
Specifies the indentation level to beautify the output JSON data, making it more readable. Only effective when format_json is True . |
4 | ||
ensure_ascii |
bool |
Controls whether to escape non-ASCII characters as Unicode . When set to True , all non-ASCII characters will be escaped; False retains the original characters. Only effective when format_json is True . |
False |
||
save_to_img() |
Save the result as a file in image format | save_path |
str |
The file path to save the result. When it is a directory, the saved file name is consistent with the naming of the input file type. | None |
- In addition, it also supports obtaining the visualized image with results and the prediction results through attributes, as follows:
Attribute | Description |
---|---|
json |
Obtain the prediction result in json format |
img |
Obtain the visualized image in dict format |
V. Secondary Development¶
If the performance of the above models does not meet your requirements in your specific scenario, you can follow the steps below for secondary development. Here, we use the training of PP-OCRv5_server_rec
as an example; for other models, simply replace the corresponding configuration files. First, you need to prepare a dataset for text recognition. You can refer to the format of the Text Recognition Demo Dataset for preparation. Once prepared, you can proceed with model training and exporting as described below. After exporting, the model can be quickly integrated into the aforementioned API. This example uses the Text Recognition Demo Dataset. Before training the model, ensure that you have installed the dependencies required by PaddleOCR as per the Installation Guide.
4.1 Dataset and Pre-trained Model Preparation¶
4.1.1 Prepare the Dataset¶
# Download the example dataset
wget https://paddle-model-ecology.bj.bcebos.com/paddlex/data/ocr_rec_dataset_examples.tar
tar -xf ocr_rec_dataset_examples.tar
4.1.2 Download the Pre-trained Model¶
# Download the PP-OCRv5_server_rec pre-trained model
wget https://paddle-model-ecology.bj.bcebos.com/paddlex/official_pretrained_model/PP-OCRv5_server_rec_pretrained.pdparams
4.2 Model Training¶
PaddleOCR modularizes its code. To train the PP-OCRv5_server_rec
recognition model, you need to use its configuration file.
The training commands are as follows:
# Single-GPU training (default training method)
python3 tools/train.py -c configs/rec/PP-OCRv5/PP-OCRv5_server_rec.yml \
-o Global.pretrained_model=./PP-OCRv5_server_rec_pretrained.pdparams
# Multi-GPU training, specify GPU IDs via the --gpus parameter
python3 -m paddle.distributed.launch --gpus '0,1,2,3' tools/train.py -c configs/rec/PP-OCRv5/PP-OCRv5_server_rec.yml \
-o Global.pretrained_model=./PP-OCRv5_server_rec_pretrained.pdparams
4.3 Model Evaluation¶
You can evaluate the weights of a trained model, such as output/xxx/xxx.pdparams
, using the following command:
# Note: Set the path of pretrained_model to a local path. If using a model you trained and saved yourself, ensure to modify the path and filename to {path/to/weights}/{model_name}.
# Evaluation on the demo test set
python3 tools/eval.py -c configs/rec/PP-OCRv5/PP-OCRv5_server_rec.yml -o \
Global.pretrained_model=output/xxx/xxx.pdparams
4.4 Model Exporting¶
python3 tools/export_model.py -c configs/rec/PP-OCRv5/PP-OCRv5_server_rec.yml -o \
Global.pretrained_model=output/xxx/xxx.pdparams \
Global.save_inference_dir="./PP-OCRv5_server_rec_infer/"
After exporting the model, the static graph model will be stored in ./PP-OCRv5_server_rec_infer/
in the current directory. Under this directory, you will see the following files:
At this point, the secondary development is complete. This static graph model can be directly integrated into the PaddleOCR API.