Seal Text Detection Module Tutorial¶
I. Overview¶
The seal text detection module typically outputs multi-point bounding boxes around text regions, which are then passed as inputs to the distortion correction and text recognition modules for subsequent processing to identify the textual content of the seal. Recognizing seal text is an integral part of document processing and finds applications in various scenarios such as contract comparison, inventory access auditing, and invoice reimbursement verification. The seal text detection module serves as a subtask within OCR (Optical Character Recognition), responsible for locating and marking the regions containing seal text within an image. The performance of this module directly impacts the accuracy and efficiency of the entire seal text OCR system.
II. Supported Model List¶
Model Name | Model Download Link | 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_seal_det | Inference Model/Training Model | 98.21 | 74.75 / 67.72 | 382.55 / 382.55 | 109 M | The server-side seal text detection model of PP-OCRv4 boasts higher accuracy and is suitable for deployment on better-equipped servers. |
PP-OCRv4_mobile_seal_det | Inference Model/Training Model | 96.47 | 7.82 / 3.09 | 48.28 / 23.97 | 4.6 M | The mobile-side seal text detection model of PP-OCRv4, on the other hand, offers greater efficiency and is suitable for deployment on end devices. |
Test Environment Description:
- Performance Test Environment
- Test Dataset: PaddleX Custom Dataset, Containing 500 Images of Circular Stamps.
- 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 PaddleOCR wheel package. For detailed instructions, refer to PaddleOCR Local Installation Tutorial。
Quickly experience with just one command:
paddleocr seal_text_detection -i https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/seal_text_det.png
You can also integrate the model inference from the layout area detection module into your project. Before running the following code, please download Example Image Go to the local area.
from paddleocr import SealTextDetection
model = SealTextDetection(model_name="PP-OCRv4_server_seal_det")
output = model.predict("seal_text_det.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 is:
{'res': {'input_path': 'seal_text_det.png', 'page_index': None, 'dt_polys': [array([[463, 477],
...,
[428, 505]]), array([[297, 444],
...,
[230, 443]]), array([[457, 346],
...,
[267, 345]]), array([[325, 38],
...,
[322, 37]])], 'dt_scores': [0.9912680344777314, 0.9906849624837963, 0.9847219455533163, 0.9914791724153904]}}
The meanings of the parameters are as follows:
- input_path
: represents the path of the input image to be predicted
- dt_polys
: represents the predicted text detection boxes, where each text detection box contains multiple vertices of a polygon. Each vertex is a list of two elements, representing the x and y coordinates of the vertex respectively
- dt_scores
: represents the confidence scores of the predicted text detection boxes
The visualization image is as follows:
The explanations of related methods and parameters are as follows:
SealTextDetection
instantiates a text detection model (here we takePP-OCRv4_server_seal_det
as an example), and the specific explanations are as follows:
Parameter | Parameter Description | Parameter Type | Options | Default Value |
---|---|---|---|---|
model_name |
Name of the model | str |
All model names supported by PaddleX for seal text detection | None |
model_dir |
Path to store the model | str |
None | None |
device |
The device used for model inference | str |
It supports specifying specific GPU card numbers, such as "gpu:0", other hardware card numbers, such as "npu:0", or CPU, such as "cpu". | gpu:0 |
limit_side_len |
Limit on the side length of the image for detection | int/None |
|
None |
limit_type |
Type of side length limit for detection | str/None |
|
None |
thresh |
In the output probability map, pixels with scores greater than this threshold will be considered as text pixels | float/None |
|
None |
box_thresh |
If the average score of all pixels within a detection result box is greater than this threshold, the result will be considered as a text region | float/None |
|
None |
max_candidates |
Maximum number of text boxes to output | int/None |
|
None |
unclip_ratio |
Expansion ratio for the Vatti clipping algorithm, used to expand the text region | float/None |
|
None |
use_dilation |
Whether to dilate the segmentation result | bool/None |
True/False/None | None |
use_hpip |
Whether to enable the high-performance inference plugin | bool |
None | False |
hpi_config |
High-performance inference configuration | dict | None |
None | None |
-
The
model_name
must be specified. After specifyingmodel_name
, the built-in model parameters of PaddleX will be used by default. On this basis, ifmodel_dir
is specified, the user-defined model will be used. -
The
predict()
method of the seal text detection model is called for inference prediction. The parameters of thepredict()
method includeinput
,batch_size
,limit_side_len
,limit_type
,thresh
,box_thresh
,max_candidates
,unclip_ratio
, anduse_dilation
. The specific descriptions are 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 |
Side length limit for detection | int/None |
|
None |
limit_type |
Type of side length limit for detection | str/None |
|
None |
thresh |
In the output probability map, pixels with scores greater than this threshold will be considered as text pixels | float/None |
|
None |
box_thresh |
If the average score of all pixels within the detection result box is greater than this threshold, the result will be considered as a text area | float/None |
|
None |
max_candidates |
Maximum number of text boxes to be output | int/None |
|
None |
unclip_ratio |
Expansion coefficient of the Vatti clipping algorithm, used to expand the text area | float/None |
|
None |
use_dilation |
Whether to dilate the segmentation result | bool/None |
True/False/None | None |
- Process the prediction results. Each sample's prediction result is a corresponding Result object, and it supports operations such as printing, saving as an image, and saving as a
json
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 to escape non-ASCII characters 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 file in JSON format | save_path |
str |
The file path for saving. When it is a directory, the saved file name will be consistent with 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 to escape non-ASCII characters 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 a file in image format | save_path |
str |
The file path for saving. When it is a directory, the saved file name will be consistent with the input file name | None |
- In addition, it also supports obtaining visual images with results and prediction results through attributes, as follows:
Attribute | Attribute Description |
---|---|
json |
Get the prediction result in json format |
img |
Get the visual image in dict format |
IV. Custom Development¶
If the above model is still not performing well in your scenario, you can try the following steps for secondary development. Here, we'll use training PP-OCRv4_server_seal_det
as an example; you can replace it with the corresponding configuration files for other models. First, you need to prepare a text detection dataset. You can refer to the format of the seal text detection demo data for preparation. Once prepared, you can follow the steps below for model training and export. After export, you can quickly integrate the model into the above API. This example uses a seal text detection demo dataset. Before training the model, please ensure that you have installed the dependencies required by PaddleOCR as per the installation documentation.
4.1 Dataset and Pre-trained Model Preparation¶
4.1.1 Preparing the Dataset¶
wget https://paddle-model-ecology.bj.bcebos.com/paddlex/data/ocr_curve_det_dataset_examples.tar -P ./dataset
tar -xf ./dataset/ocr_curve_det_dataset_examples.tar -C ./dataset/
4.1.1 Preparing the pre-trained model¶
wget https://paddle-model-ecology.bj.bcebos.com/paddlex/official_pretrained_model/PP-OCRv4_server_seal_det_pretrained.pdparams
4.2 Model Training¶
PaddleOCR has modularized the code, and when training the PP-OCRv4_server_seal_det
model, you need to use the configuration file for PP-OCRv4_server_seal_det
.
The training commands are as follows:
# Single GPU training (default training method)
python3 tools/train.py -c configs/det/PP-OCRv4/PP-OCRv4_server_seal_det.yml \
-o Global.pretrained_model=./PP-OCRv4_server_seal_det_pretrained.pdparams
# Multi-GPU training, specify GPU ids using the --gpus parameter
python3 -m paddle.distributed.launch --gpus '0,1,2,3' tools/train.py -c configs/det/PP-OCRv4/PP-OCRv4_server_seal_det.yml \
-o Global.pretrained_model=./PP-OCRv4_server_seal_det_pretrained.pdparams
4.3 Model Evaluation¶
You can evaluate the trained weights, such as output/xxx/xxx.pdparams
, using the following command:
# Make sure to set the pretrained_model path to the local path. If using a model that was trained and saved by yourself, be sure to modify the path and filename to {path/to/weights}/{model_name}.
# Demo test set evaluation
python3 tools/eval.py -c configs/det/PP-OCRv4/PP-OCRv4_server_seal_det.yml -o \
Global.pretrained_model=output/xxx/xxx.pdparams
4.4 Model Export¶
python3 tools/export_model.py -c configs/det/PP-OCRv4/PP-OCRv4_server_seal_det.yml -o \
Global.pretrained_model=output/xxx/xxx.pdparams \
save_inference_dir="./PP-OCRv4_server_seal_det_infer/"
After exporting the model, the static graph model will be stored in the ./PP-OCRv4_server_seal_det_infer/
directory. In this directory, you will see the following files: