Text Detection Module Development Tutorial¶
I. Overview¶
The text detection module is a crucial component in OCR (Optical Character Recognition) systems, responsible for locating and marking regions containing text within images. The performance of this module directly impacts the accuracy and efficiency of the entire OCR system. The text detection module typically outputs bounding boxes for text regions, which are then passed on to the text recognition module for further processing.
II. Supported Models¶
Model | Model Download Link | Detection Hmean (%) | GPU Inference Time (ms) [Normal Mode / High-Performance Mode] |
CPU Inference Time (ms) [Normal Mode / High-Performance Mode] |
Model Size (M) | Description |
---|---|---|---|---|---|---|
PP-OCRv4_server_det | Inference Model/Trained Model | 82.69 | 83.34 / 80.91 | 442.58 / 442.58 | 109 | The server-side text detection model of PP-OCRv4, featuring higher accuracy and suitable for deployment on high-performance servers |
PP-OCRv4_mobile_det | Inference Model/Trained Model | 77.79 | 8.79 / 3.13 | 51.00 / 28.58 | 4.7 | The mobile text detection model of PP-OCRv4, optimized for efficiency and suitable for deployment on edge devices |
Test Environment Description:
- Performance Test Environment
- Test Dataset: PaddleOCR Self-built Dataset for Chinese and English, Covering Various Scenarios
-
Hardware Configuration:
- GPU: NVIDIA Tesla T4
- CPU: Intel Xeon Gold 6271C @ 2.60GHz
- Other Environments: Ubuntu 20.04 / cuDNN 8.6 / TensorRT 8.5.2.2
-
Inference Mode Description
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 pre-selected precision types and acceleration strategies | FP32 Precision / 8 Threads | Pre-selected optimal backend (Paddle/OpenVINO/TRT, etc.) |
III. Quick Integration¶
❗ Before quick integration, please install the PaddleX wheel package. For detailed instructions, refer to the PaddleX Local Installation Guide.
Just a few lines of code can complete the inference of the text detection module, allowing you to easily switch between models under this module. You can also integrate the model inference of the text detection module into your project. Before running the following code, please download the demo image to your local machine.
from paddlex import create_model
model = create_model(model_name="PP-OCRv4_mobile_det")
output = model.predict("general_ocr_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 result obtained is:
{'res': {'input_path': 'general_ocr_001.png', "page_index": None, 'dt_polys': [[[73, 552], [453, 542], [454, 575], [74, 585]], [[17, 506], [515, 486], [517, 535], [19, 555]], [[189, 457], [398, 449], [399, 482], [190, 490]], [[41, 412], [484, 387], [486, 433], [43, 457]]], 'dt_scores': [0.7555687038101032, 0.701620896397861, 0.8839516283528792, 0.8123399529333318]}}
The meanings of the running result parameters are as follows:
- input_path
: Indicates the path of the input image to be predicted.
- page_index
: If the input is a PDF file, it indicates which page of the PDF it is; otherwise, it is None
.
- dt_polys
: Indicates the predicted text detection boxes, where each text detection box contains four vertices of a quadrilateral. Each vertex is a tuple representing the x and y coordinates of the vertex.
- dt_scores
: Indicates the confidence scores of the predicted text detection boxes.
The visualization image is as follows:
Note: Due to network issues, the above URL may not be successfully parsed. If you need the content of this webpage, please check the validity of the link and try again. Alternatively, if parsing this link is not necessary for your question, please proceed with other questions.
Relevant methods, parameters, and explanations are as follows:
create_model
instantiates a text detection model (here usingPP-OCRv4_mobile_det
as an example). The specific explanation is as follows:
Parameter | Parameter Description | Parameter Type | Options | Default Value |
---|---|---|---|---|
model_name |
Name of the model | str |
All text detection model names supported by PaddleX | None |
model_dir |
Path to store the model | str |
None | None |
limit_side_len |
Limit on the side length of the detection image | int/None |
|
None |
limit_type |
Type of side length limit for detection | str/None |
|
None |
thresh |
Threshold for considering a pixel as a text pixel in the output probability map | float/None |
|
None |
box_thresh |
Threshold for considering a detected box as a text region based on the average score of pixels inside the box | float/None |
|
None |
unclip_ratio |
Expansion ratio for text regions using the Vatti clipping algorithm | float/None |
|
None |
-
The
model_name
must be specified. After specifyingmodel_name
, the default model parameters built into PaddleX will be used. Ifmodel_dir
is specified, the user-defined model will be used. -
The
predict()
method of the text detection model is called for inference prediction. The parameters of thepredict()
method areinput
,batch_size
,limit_side_len
,limit_type
,thresh
,box_thresh
,max_candidates
,unclip_ratio
, anduse_dilation
. The specific explanation is as follows:
Parameter | Parameter Description | Parameter Type | Options | Default Value |
---|---|---|---|---|
input |
Data to be predicted, supporting multiple input types | Python Var /str /dict /list |
|
None |
batch_size |
Batch size | int |
Any integer greater than 0 | 1 |
limit_side_len |
Limit on the side length of the detection image | int/None |
|
None |
limit_type |
Type of side length limit for detection | str/None |
|
None |
thresh |
Threshold for considering a pixel as a text pixel in the output probability map | float/None |
|
None |
box_thresh |
Threshold for considering a detected box as a text region based on the average score of pixels inside the box | float/None |
|
None |
unclip_ratio |
Expansion ratio for text regions using the Vatti clipping algorithm | float/None |
|
None |
- The prediction results are processed, with each sample's prediction result being of type
dict
, and supporting operations such as printing, saving as an image, and saving as ajson
file:
Method | Method Description | Parameter | Parameter Type | Parameter 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 |
Specify the indentation level to beautify the output JSON data, making it more readable. This is only effective when format_json is True |
4 | ||
ensure_ascii |
bool |
Control whether non-ASCII characters are escaped to Unicode . When set to True , all non-ASCII characters will be escaped; False retains the original characters. This is only effective when format_json is True |
False |
||
save_to_json() |
Save the result as a JSON file | save_path |
str |
The file path for saving. When it is a directory, the saved file name will match the input file name | None |
indent |
int |
Specify the indentation level to beautify the output JSON data, making it more readable. This is only effective when format_json is True |
4 | ||
ensure_ascii |
bool |
Control whether non-ASCII characters are escaped to Unicode . When set to True , all non-ASCII characters will be escaped; False retains the original characters. This is only effective when format_json is True |
False |
||
save_to_img() |
Save the result as an image file | save_path |
str |
The file path for saving. When it is a directory, the saved file name will match the input file name | None |
- Additionally, it also supports obtaining visualized images with results and prediction results through attributes, as follows:
Attribute | Attribute Description |
---|---|
json |
Get the prediction result in json format |
img |
Get the visualized image in dict format |
For more information on using PaddleX's single-model inference APIs, refer to the PaddleX Single Model Python Script Usage Instructions.
IV. Custom Development¶
If you seek even higher accuracy from existing models, you can leverage PaddleX's custom development capabilities to develop better text detection models. Before developing text detection models with PaddleX, ensure you have installed the PaddleOCR plugin for PaddleX. The installation process can be found in the PaddleX Local Installation Guide.
4.1 Data Preparation¶
Before model training, you need to prepare a dataset for the specific task module. PaddleX provides data validation functionality for each module, and only data that passes validation can be used for model training. Additionally, PaddleX provides demo datasets for each module, which you can use to complete subsequent development. If you wish to use private datasets for model training, refer to the PaddleX Text Detection/Text Recognition Task Module Data Annotation Tutorial.
4.1.1 Demo Data Download¶
You can use the following commands to download the demo dataset to a specified folder:
wget https://paddle-model-ecology.bj.bcebos.com/paddlex/data/ocr_det_dataset_examples.tar -P ./dataset
tar -xf ./dataset/ocr_det_dataset_examples.tar -C ./dataset/
4.1.2 Data Validation¶
A single command can complete data validation:
python main.py -c paddlex/configs/modules/text_detection/PP-OCRv4_mobile_det.yaml \
-o Global.mode=check_dataset \
-o Global.dataset_dir=./dataset/ocr_det_dataset_examples
After executing the above command, PaddleX will validate the dataset and gather basic information about it. Once the command runs successfully, Check dataset passed !
will be printed in the log. The validation result file is saved in ./output/check_dataset_result.json
, and related outputs will be stored in the ./output/check_dataset
directory in the current directory. The output directory includes sample images and histograms of sample distribution.
👉 Validation Result Details (Click to Expand)
The specific content of the validation result file is:
{
"done_flag": true,
"check_pass": true,
"attributes": {
"train_samples": 200,
"train_sample_paths": [
"../dataset/ocr_det_dataset_examples/images/train_img_61.jpg",
"../dataset/ocr_det_dataset_examples/images/train_img_289.jpg"
],
"val_samples": 50,
"val_sample_paths": [
"../dataset/ocr_det_dataset_examples/images/val_img_61.jpg",
"../dataset/ocr_det_dataset_examples/images/val_img_137.jpg"
]
},
"analysis": {
"histogram": "check_dataset/histogram.png"
},
"dataset_path": "./dataset/ocr_det_dataset_examples",
"show_type": "image",
"dataset_type": "TextDetDataset"
}
In the above validation result, check_pass
being true
indicates that the dataset format meets the requirements. The explanation of other metrics is as follows:
attributes.train_samples
: The number of training samples in the dataset is 200;attributes.val_samples
: The number of validation samples in the dataset is 50;attributes.train_sample_paths
: List of relative paths for visualizing training sample images in the dataset;attributes.val_sample_paths
: List of relative paths for visualizing validation sample images in the dataset;
Additionally, the dataset validation also analyzed the distribution of the length and width of all images in the dataset and plotted a distribution histogram (histogram.png):
4.1.3 Dataset Format Conversion/Dataset Splitting (Optional)¶
After completing data validation, you can convert the dataset format and re-split the training/validation ratio by modifying the configuration file or appending hyperparameters.
👉 Details on Format Conversion/Dataset Splitting (Click to Expand)
(1) Dataset Format Conversion
Text detection does not support data format conversion.
(2) Dataset Splitting
The parameters for dataset splitting can be set by modifying the CheckDataset
section in the configuration file. Below are some example explanations for the parameters in the configuration file:
CheckDataset
:split
:enable
: Whether to re-split the dataset. Set toTrue
to enable dataset splitting, default isFalse
;train_percent
: If re-splitting the dataset, set the percentage of the training set. The type is any integer between 0-100, and the sum withval_percent
must be 100;
For example, if you want to re-split the dataset with a 90% training set and a 10% validation set, modify the configuration file as follows:
......
CheckDataset:
......
split:
enable: True
train_percent: 90
val_percent: 10
......
Then execute the command:
python main.py -c paddlex/configs/modules/text_detection/PP-OCRv4_mobile_det.yaml \
-o Global.mode=check_dataset \
-o Global.dataset_dir=./dataset/ocr_det_dataset_examples
After dataset splitting, the original annotation files will be renamed to xxx.bak
in the original path.
The above parameters can also be set by appending command-line arguments:
python main.py -c paddlex/configs/modules/text_detection/PP-OCRv4_mobile_det.yaml \
-o Global.mode=check_dataset \
-o Global.dataset_dir=./dataset/ocr_det_dataset_examples \
-o CheckDataset.split.enable=True \
-o CheckDataset.split.train_percent=90 \
-o CheckDataset.split.val_percent=10
4.2 Model Training¶
Model training can be completed with a single command. Here's an example of training the PP-OCRv4 mobile text detection model (PP-OCRv4_mobile_det
):
python main.py -c paddlex/configs/modules/text_detection/PP-OCRv4_mobile_det.yaml \
-o Global.mode=train \
-o Global.dataset_dir=./dataset/ocr_det_dataset_examples
- Specify the path to the model's
.yaml
configuration file (here it'sPP-OCRv4_mobile_det.yaml
,When training other models, you need to specify the corresponding configuration files. The relationship between the model and configuration files can be found in the PaddleX Model List (CPU/GPU)) - Set the mode to model training:
-o Global.mode=train
- Specify the path to the training dataset:
-o Global.dataset_dir
Other related parameters can be set by modifying theGlobal
andTrain
fields in the.yaml
configuration file or adjusted by appending parameters in the command line. For example, to specify training on the first two GPUs:-o Global.device=gpu:0,1
; to set the number of training epochs to 10:-o Train.epochs_iters=10
. For more modifiable parameters and their detailed explanations, refer to the PaddleX Common Configuration Parameters Documentation.
👉 More Information (Click to Expand)
- During model training, PaddleX automatically saves the model weight files, with the default being
output
. If you need to specify a save path, you can set it through the-o Global.output
field in the configuration file. - PaddleX shields you from the concepts of dynamic graph weights and static graph weights. During model training, both dynamic and static graph weights are produced, and static graph weights are selected by default for model inference.
-
After completing the model training, all outputs are saved in the specified output directory (default is
./output/
), typically including: -
train_result.json
: Training result record file, recording whether the training task was completed normally, as well as the output weight metrics, related file paths, etc.; train.log
: Training log file, recording changes in model metrics and loss during training;config.yaml
: Training configuration file, recording the hyperparameter configuration for this training session;.pdparams
,.pdema
,.pdopt.pdstate
,.pdiparams
,.pdmodel
: Model weight-related files, including network parameters, optimizer, EMA, static graph network parameters, static graph network structure, etc.;
4.3 Model Evaluation¶
After completing model training, you can evaluate the specified model weight file on the validation set to verify the model's accuracy. Using PaddleX for model evaluation can be done with a single command:
python main.py -c paddlex/configs/modules/text_detection/PP-OCRv4_mobile_det.yaml \
-o Global.mode=evaluate \
-o Global.dataset_dir=./dataset/ocr_det_dataset_examples
Similar to model training, the following steps are required:
- Specify the path to the model's
.yaml
configuration file (in this case,PP-OCRv4_mobile_det.yaml
) - Specify the mode as model evaluation:
-o Global.mode=evaluate
- Specify the path to the validation dataset:
-o Global.dataset_dir
Other related parameters can be set by modifying the fields under Global
and Evaluate
in the .yaml
configuration file. For details, please refer to PaddleX General Model Configuration File Parameter Instructions.
👉 More Instructions (Click to Expand)
During model evaluation, you need to specify the path to the model weight file. Each configuration file has a built-in default weight save path. If you need to change it, you can set it by adding a command line argument, such as -o Evaluate.weight_path=./output/best_accuracy/best_accuracy.pdparams
.
After completing the model evaluation, an evaluate_result.json
will be generated, which records the evaluation results. Specifically, it records whether the evaluation task was completed successfully and the model's evaluation metrics, including precision
, recall
, and hmean
.
4.4 Model Inference and Model Integration¶
After completing model training and evaluation, you can use the trained model weights for inference predictions or Python integration.
4.4.1 Model Inference¶
To perform inference predictions via the command line, simply use the following command. Before running the following code, please download the demo image to your local machine.
python main.py -c paddlex/configs/modules/text_detection/PP-OCRv4_mobile_det.yaml \
-o Global.mode=predict \
-o Predict.model_dir="./output/best_accuracy/inference" \
-o Predict.input="general_ocr_001.png"
- Specify the
.yaml
configuration file path of the model (here it'sPP-OCRv4_mobile_det.yaml
) - Set the mode to model inference prediction:
-o Global.mode=predict
- Specify the model weights path:
-o Predict.model_dir="./output/best_accuracy/inference"
-
Specify the input data path:
-o Predict.input="..."
Other related parameters can be set by modifying the fields underGlobal
andPredict
in the.yaml
configuration file. For details, refer to PaddleX Common Model Configuration File Parameter Description. -
Alternatively, you can use the PaddleX wheel package for inference, easily integrating the model into your own projects.
4.4.2 Model Integration¶
Models can be directly integrated into PaddleX pipelines or into your own projects.
1.Pipeline Integration
The text detection module can be integrated into PaddleX pipelines such as the General OCR Pipeline, Table Recognition Pipeline, and PP-ChatOCRv3-doc. Simply replace the model path to update the text detection module of the relevant pipeline.
2.Module Integration
The model weights you produce can be directly integrated into the text detection module. Refer to the Python example code in Quick Integration, and simply replace the model with the path to your trained model.
You can also use the PaddleX high-performance inference plugin to optimize the inference process of your model and further improve efficiency. For detailed procedures, please refer to the PaddleX High-Performance Inference Guide.