Seal Text Recognition Pipeline Usage Tutorial¶
1. Overview¶
Table structure recognition is an important component of table recognition systems, capable of converting non-editable table images into editable table formats (such as HTML). The goal of table structure recognition is to identify the positions of rows, columns, and cells in tables. The performance of this module directly affects the accuracy and efficiency of the entire table recognition system. The table structure recognition module usually outputs HTML code for the table area, which is then passed as input to the tabl recognition pipeline for further processing.
2. Supported Model List¶
Model | Model Download Link | Accuracy (%) | GPU Inference Time (ms) [Normal Mode / High Performance Mode] |
CPU Inference Time (ms) [Normal Mode / High Performance Mode] |
Model Storage Size (M) | Description |
---|---|---|---|---|---|---|
SLANet | Inference Model/Training Model | 59.52 | 103.08 / 103.08 | 197.99 / 197.99 | 6.9 M | SLANet is a table structure recognition model independently developed by Baidu PaddlePaddle Vision Team. By adopting a CPU-friendly lightweight backbone network PP-LCNet, high-low level feature fusion module CSP-PAN, and SLA Head, a feature decoding module aligning structure and position information, this model greatly improves the accuracy and inference speed of table structure recognition. |
SLANet_plus | Inference Model/Training Model | 63.69 | 140.29 / 140.29 | 195.39 / 195.39 | 6.9 M | SLANet_plus is an enhanced version of the table structure recognition model SLANet independently developed by the Baidu PaddlePaddle Vision Team. Compared to SLANet, SLANet_plus has greatly improved the recognition ability for wireless and complex tables, and reduced the model's sensitivity to table positioning accuracy. Even if the table positioning is offset, it can still be accurately recognized. |
SLANeXt_wired | Inference Model/Training Model | 69.65 | -- | -- | 351M | The SLANeXt series is a new generation of table structure recognition models independently developed by the Baidu PaddlePaddle Vision Team. Compared to SLANet and SLANet_plus, SLANeXt focuses on table structure recognition, and trains dedicated weights for wired and wireless tables separately. The recognition ability for all types of tables has been significantly improved, especially for wired tables. |
SLANeXt_wireless | Inference Model/Training Model |
Test Environment Description:
- Performance Test Environment
- Test Dataset: High-difficulty Chinese table recognition dataset.
- 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
- 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 prior precision type and acceleration strategy | FP32 precision / 8 threads | Selects the prior optimal backend (Paddle/OpenVINO/TRT, etc.) |
3. Quick Start¶
❗ Before getting started, please install the PaddleOCR wheel package. For details, please refer to the Installation Tutorial.
Quickly experience with a single command:
paddleocr table_structure_recognition -i https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/table_recognition.jpg
The command line supports more parameter settings. Click to expand for detailed explanations of command line parameters.
Parameter | Description | Parameter Type | Default Value |
---|---|---|---|
input |
Data to be predicted, required.
Local path of image or PDF file, e.g., /root/data/img.jpg ; URL link, e.g., network URL of image or PDF file: Example; Local directory, the directory should contain images to be predicted, e.g., local path: /root/data/ (currently does not support prediction of PDF files in directories; PDF files must be specified with a specific file path).
|
str |
|
save_path |
Specify the path to save the inference results file. If not set, the inference results will not be saved locally. | str |
|
doc_orientation_classify_model_name |
The name of the document orientation classification model. If not set, the default model in pipeline will be used. | str |
|
doc_orientation_classify_model_dir |
The directory path of the document orientation classification model. If not set, the official model will be downloaded. | str |
|
doc_unwarping_model_name |
The name of the text image unwarping model. If not set, the default model in pipeline will be used. | str |
|
doc_unwarping_model_dir |
The directory path of the text image unwarping model. If not set, the official model will be downloaded. | str |
|
layout_detection_model_name |
The name of the layout detection model. If not set, the default model in pipeline will be used. | str |
|
layout_detection_model_dir |
The directory path of the layout detection model. If not set, the official model will be downloaded. | str |
|
seal_text_detection_model_name |
The name of the seal text detection model. If not set, the pipeline's default model will be used. | str |
|
seal_text_detection_model_dir |
The directory path of the seal text detection model. If not set, the official model will be downloaded. | str |
|
text_recognition_model_name |
Name of the text recognition model. If not set, the default pipeline model is used. | str |
|
text_recognition_model_dir |
Directory path of the text recognition model. If not set, the official model is downloaded. | str |
|
text_recognition_batch_size |
Batch size for the text recognition model. If not set, defaults to 1 . |
int |
|
use_doc_orientation_classify |
Whether to load and use document orientation classification module. If not set, defaults to pipeline initialization value (True ). |
bool |
|
use_doc_unwarping |
Whether to load and use text image correction module. If not set, defaults to pipeline initialization value (True ). |
bool |
|
use_layout_detection |
Whether to load and use the layout detection module. If not set, the parameter will default to the value initialized in the pipeline, which is True . |
bool |
|
layout_threshold |
Score threshold for the layout model. Any value between 0-1 . If not set, the default value is used, which is 0.5 .
|
float |
|
layout_nms |
Whether to use Non-Maximum Suppression (NMS) as post-processing for layout detection. If not set, the parameter will default to the value initialized in the pipeline, which is set to True by default. |
bool |
|
layout_unclip_ratio |
Unclip ratio for detected boxes in layout detection model. Any float > 0 . If not set, the default is 1.0 .
|
float |
|
layout_merge_bboxes_mode |
The merging mode for the detection boxes output by the model in layout region detection.
large .
|
str |
|
seal_det_limit_side_len |
Image side length limit for seal text detection.
Any integer > 0 . If not set, the default is 736 .
|
int |
|
seal_det_limit_type |
Limit type for image side in seal text detection.
Supports min and max ; min ensures shortest side ≥ det_limit_side_len , max ensures longest side ≤ limit_side_len . If not set, the default is min .
|
str |
|
seal_det_thresh |
Pixel threshold. Pixels with scores above this value in the probability map are considered text.
any float > 0 . If not set, the default is 0.2 .
|
float |
|
seal_det_box_thresh |
Box threshold. Boxes with average pixel scores above this value are considered text regions.
any float > 0 . If not set, the default is 0.6 .
|
float |
|
seal_det_unclip_ratio |
Expansion ratio for seal text detection. Higher value means larger expansion area.
Any float > 0 . If not set, the default is 0.5 .
|
float |
|
seal_rec_score_thresh |
Recognition score threshold. Text results above this value will be kept.
Any float > 0 . If not set, the default is 0.0 (no threshold).
|
float |
|
device |
The device used for inference. Support for specifying specific card numbers:
|
str |
|
enable_hpi |
Whether to enable high-performance inference. | bool |
False |
use_tensorrt |
Whether to use the Paddle Inference TensorRT subgraph engine. For Paddle with CUDA version 11.8, the compatible TensorRT version is 8.x (x>=6), and it is recommended to install TensorRT 8.6.1.6. For Paddle with CUDA version 12.6, the compatible TensorRT version is 10.x (x>=5), and it is recommended to install TensorRT 10.5.0.18. | bool |
False |
precision |
The computational precision, such as fp32, fp16. | str |
fp32 |
enable_mkldnn |
Whether to enable MKL-DNN acceleration for inference. If MKL-DNN is unavailable or the model does not support it, acceleration will not be used even if this flag is set. | bool |
True |
mkldnn_cache_capacity |
MKL-DNN cache capacity. | int |
10 |
cpu_threads |
The number of threads used for inference on the CPU. | int |
8 |
paddlex_config |
Path to PaddleX pipeline configuration file. | str |
After running, the results will be printed to the terminal, as follows:
{'res': {'input_path': './seal_text_det.png', 'model_settings': {'use_doc_preprocessor': True, 'use_layout_detection': True}, 'doc_preprocessor_res': {'input_path': None, 'page_index': None, 'model_settings': {'use_doc_orientation_classify': False, 'use_doc_unwarping': False}, 'angle': -1}, 'layout_det_res': {'input_path': None, 'page_index': None, 'boxes': [{'cls_id': 16, 'label': 'seal', 'score': 0.975529670715332, 'coordinate': [6.191284, 0.16680908, 634.39325, 628.85345]}]}, 'seal_res_list': [{'input_path': None, 'page_index': None, 'model_settings': {'use_doc_preprocessor': False, 'use_textline_orientation': False}, 'dt_polys': [array([[320, 38],
...,
[315, 38]]), array([[461, 347],
...,
[456, 346]]), array([[439, 445],
...,
[434, 444]]), array([[158, 468],
...,
[154, 466]])], 'text_det_params': {'limit_side_len': 736, 'limit_type': 'min', 'thresh': 0.2, 'max_side_limit': 4000, 'box_thresh': 0.6, 'unclip_ratio': 0.5}, 'text_type': 'seal', 'textline_orientation_angles': array([-1, ..., -1]), 'text_rec_score_thresh': 0, 'rec_texts': ['天津君和缘商贸有限公司', '发票专用章', '吗繁物', '5263647368706'], 'rec_scores': array([0.99340463, ..., 0.9916274 ]), 'rec_polys': [array([[320, 38],
...,
[315, 38]]), array([[461, 347],
...,
[456, 346]]), array([[439, 445],
...,
[434, 444]]), array([[158, 468],
...,
[154, 466]])], 'rec_boxes': array([], dtype=float64)}]}}
save_path
, and the visualized result of seal OCR is as follows:
2.2 Python Script Integration¶
- The above command line is for quickly experiencing and viewing the effect. Generally, in a project, you often need to integrate through code. You can complete the quick inference of the pipeline with just a few lines of code. The inference code is as follows:
from paddleocr import TableStructureRecognition
model = TableStructureRecognition(model_name="SLANet")
output = model.predict(input="table_recognition.jpg", batch_size=1)
for res in output:
res.print(json_format=False)
res.save_to_json("./output/res.json")
After running, the result is:
{'res': {'input_path': 'table_recognition.jpg', 'page_index': None, 'bbox': [[42, 2, 390, 2, 388, 27, 40, 26], [11, 35, 89, 35, 87, 63, 11, 63], [113, 34, 192, 34, 186, 64, 109, 64], [219, 33, 399, 33, 393, 62, 212, 62], [413, 33, 544, 33, 544, 64, 407, 64], [12, 67, 98, 68, 96, 93, 12, 93], [115, 66, 205, 66, 200, 91, 111, 91], [234, 65, 390, 65, 385, 92, 227, 92], [414, 66, 537, 67, 537, 95, 409, 95], [7, 97, 106, 97, 104, 128, 7, 128], [113, 96, 206, 95, 201, 127, 109, 127], [236, 96, 386, 96, 381, 128, 230, 128], [413, 96, 534, 95, 533, 127, 408, 127]], 'structure': ['<html>', '<body>', '<table>', '<tr>', '<td', ' colspan="4"', '>', '</td>', '</tr>', '<tr>', '<td></td>', '<td></td>', '<td></td>', '<td></td>', '</tr>', '<tr>', '<td></td>', '<td></td>', '<td></td>', '<td></td>', '</tr>', '<tr>', '<td></td>', '<td></td>', '<td></td>', '<td></td>', '</tr>', '</table>', '</body>', '</html>'], 'structure_score': 0.99948007}}
Parameter meanings are as follows:
input_path
: The path of the input table image to be predictedpage_index
: If the input is a PDF file, indicates the page number of the PDF; otherwise, it isNone
boxes
: Predicted table cell information, a list consisting of the coordinates of predicted table cells. Notably, table cell predictions for the SLANeXt series models are invalidstructure
: Predicted table structure HTML expressions, a list consisting of predicted HTML keywords in orderstructure_score
: Confidence of the predicted table structure
Descriptions of related methods and parameters are as follows:
TableStructureRecognition
instantiates a table structure recognition model (usingSLANet
as an example). Details are as follows:
Parameter | Description | Type | Options | Default |
---|---|---|---|---|
doc_orientation_classify_model_name |
Name of the document orientation classification model. If set to None , the pipeline default model is used. |
str |
All model names | None |
doc_orientation_classify_model_dir |
Directory path of the document orientation classification model. If set to None , the official model will be downloaded. |
str |
None |
|
doc_unwarping_model_name |
Name of the document unwarping model. If set to None , the pipeline default model is used. |
str |
None |
|
doc_unwarping_model_dir |
Directory path of the document unwarping model. If set to None , the official model will be downloaded. |
str |
None |
|
layout_detection_model_name |
Name of the layout detection model. If set to None , the pipeline default model is used. |
str |
None |
|
layout_detection_model_dir |
Directory path of the layout detection model. If set to None , the official model will be downloaded. |
str |
None |
|
seal_text_detection_model_name |
Name of the seal text detection model. If set to None , the default model will be used. |
str |
||
seal_text_detection_model_dir |
Directory of the seal text detection model. If set to None , the official model will be downloaded. |
str |
||
text_recognition_model_name |
Name of the text recognition model. If set to None , the pipeline default model is used. |
str |
None |
|
text_recognition_model_dir |
Directory path of the text recognition model. If set to None , the official model will be downloaded. |
str |
None |
|
text_recognition_batch_size |
Batch size for the text recognition model. If set to None , the default batch size is 1 . |
int |
None |
|
use_doc_orientation_classify |
Whether to enable the document orientation classification module. If set to None , the default value is True . |
bool |
None |
|
use_doc_unwarping |
Whether to enable the document image unwarping module. If set to None , the default value is True . |
bool |
None |
|
use_layout_detection |
Whether to load and use the layout detection module. If set to None , the parameter will default to the value initialized in the pipeline, which is True . |
bool |
None |
|
layout_threshold |
Score threshold for the layout model.
|
float|dict |
None |
|
layout_nms |
Whether to use Non-Maximum Suppression (NMS) as post-processing for layout detection. If set to None , the parameter will default to the value initialized in the pipeline, which is set to True by default. |
bool |
None |
|
layout_unclip_ratio |
Expansion ratio for the bounding boxes from the layout detection model.
|
float|Tuple[float,float]|dict |
None |
|
layout_merge_bboxes_mode |
Filtering method for overlapping boxes in layout detection.
|
str|dict |
None |
|
seal_det_limit_side_len |
Image side length limit for seal text detection.
|
int |
None |
|
seal_det_limit_type |
Limit type for seal text detection image side length.
|
str |
None |
|
seal_det_thresh |
Pixel threshold for detection. Pixels with scores greater than this value in the probability map are considered text pixels.
|
float |
None |
|
seal_det_box_thresh |
Bounding box threshold. If the average score of all pixels inside a detection box exceeds this threshold, it is considered a text region.
|
float |
None |
|
seal_det_unclip_ratio |
Expansion ratio for seal text detection. The larger the value, the larger the expanded area.
|
float |
None |
|
seal_rec_score_thresh |
Score threshold for seal text recognition. Text results with scores above this threshold will be retained.
|
float |
None |
|
device |
Device used for inference. Supports specifying device ID:
|
str |
None |
|
enable_hpi |
Whether to enable high-performance inference. | bool |
False |
|
use_tensorrt |
Whether to use the Paddle Inference TensorRT subgraph engine. For Paddle with CUDA version 11.8, the compatible TensorRT version is 8.x (x>=6), and it is recommended to install TensorRT 8.6.1.6. For Paddle with CUDA version 12.6, the compatible TensorRT version is 10.x (x>=5), and it is recommended to install TensorRT 10.5.0.18. | bool |
False |
|
precision |
Computation precision, e.g., fp32, fp16. | str |
"fp32" |
|
enable_mkldnn |
Whether to enable MKL-DNN acceleration for inference. If MKL-DNN is unavailable or the model does not support it, acceleration will not be used even if this flag is set. | bool |
True |
|
mkldnn_cache_capacity |
MKL-DNN cache capacity. | int |
10 |
|
cpu_threads |
Number of threads used for inference on CPU. | int |
8 |
|
paddlex_config |
Path to the PaddleX pipeline configuration file. | str |
None |
-
Among them,
model_name
must be specified. Ifmodel_dir
is specified, the user's custom model is used. -
Call the
predict()
method of the table structure recognition model for inference prediction, which returns a result list. In addition, this module also provides thepredict_iter()
method. The two are completely consistent in parameter acceptance and result return. The difference is thatpredict_iter()
returns agenerator
, which can process and obtain prediction results step by step, suitable for handling large datasets or scenarios where you want to save memory. You can choose to use either method according to your actual needs. Thepredict()
method has parametersinput
andbatch_size
, described as follows:
Parameter | Parameter Description | Parameter Type | Default Value |
---|---|---|---|
input |
Input data to be predicted. Required. Supports multiple types:
|
Python Var|str|list |
|
use_doc_orientation_classify |
Whether to use the document orientation classification module during inference. | bool |
None |
use_doc_unwarping |
Whether to use the text image correction module during inference. | bool |
None |
use_layout_detection |
Whether to use the layout detection module during inference. | bool |
None |
layout_threshold |
Same as the parameter during instantiation. | float|dict |
None |
layout_nms |
Same as the parameter during instantiation. | bool |
None |
layout_unclip_ratio |
Same as the parameter during instantiation. | float|Tuple[float,float]|dict |
None |
layout_merge_bboxes_mode |
Same as the parameter during instantiation. | str|dict |
None |
seal_det_limit_side_len |
Same as the parameter during instantiation. | int |
None |
seal_det_limit_type |
Same as the parameter during instantiation. | str |
None |
seal_det_thresh |
Same as the parameter during instantiation. | float |
None |
seal_det_box_thresh |
Same as the parameter during instantiation. | float |
None |
seal_det_unclip_ratio |
Same as the parameter during instantiation. | float |
None |
seal_rec_score_thresh |
Same as the parameter during instantiation. | float |
None |
- For processing prediction results, the prediction result of each sample is the corresponding Result object, and supports printing and saving as a
json
file:
Method | Description | Parameter | Type | Parameter Description | Default |
---|---|---|---|---|---|
print() |
Print result to 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 for better readability, effective only 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 will retain the original characters, effective only when format_json is True . |
False |
||
save_to_json() |
Save result as json format file | save_path |
str |
The file path to save the results. When it is a directory, the saved file name will be consistent with the input file type. | None |
indent |
int |
Specify the indentation level to beautify the output JSON data for better readability, effective only 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 will retain the original characters, effective only when format_json is True . |
False |
||
save_to_img() |
Save results as an image file | save_path |
str |
The file path to save the results, supports directory or file path. | None |
- In addition, it also supports obtaining results through attributes, as follows:
Attribute | Description |
---|---|
json |
Get the prediction result in json format |
- The prediction results obtained through the
json
attribute are of dict type, with content consistent with what is saved by calling thesave_to_json()
method. - The prediction results returned by the
img
attribute are of dict type. The keys arelayout_det_res
,seal_res_region1
, andpreprocessed_img
, corresponding to threeImage.Image
objects: one for visualizing layout detection, one for visualizing seal text recognition results, and one for visualizing image preprocessing. If the image preprocessing sub-module is not used,preprocessed_img
will not be included in the dictionary. If the layout region detection module is not used,layout_det_res
will not be included.
3. Development Integration/Deployment¶
If the pipeline meets your requirements for inference speed and accuracy, you can proceed directly with development integration/deployment.
If you need to integrate the pipeline into your Python project, you can refer to the example code in 2.2 Python Script Method.
In addition, PaddleOCR also provides three other deployment methods, which are detailed as follows:
🚀 High-Performance Inference: In real-world production environments, many applications have stringent performance requirements for deployment strategies, especially in terms of response speed, to ensure efficient system operation and a smooth user experience. To address this, PaddleOCR offers high-performance inference capabilities aimed at deeply optimizing the performance of model inference and pre/post-processing, thereby significantly accelerating the end-to-end process. For detailed high-performance inference procedures, please refer to High-Performance Inference.
☁️ Service Deployment: Service deployment is a common form of deployment in real-world production environments. By encapsulating inference functionality into a service, clients can access these services via network requests to obtain inference results. For detailed production service deployment procedures, please refer to Serving.
Below are the API references for basic serving deployment and multi-language service invocation examples:
API Reference
For the main operations provided by the service:
- The HTTP request method is POST.
- The request body and response body are both JSON data (JSON objects).
- When the request is processed successfully, the response status code is
200
, and the attributes of the response body are as follows:
Name | Type | Description |
---|---|---|
logId |
string |
The UUID of the request. |
errorCode |
integer |
Error code. Fixed as 0 . |
errorMsg |
string |
Error message. Fixed as "Success" . |
result |
object |
The result of the operation. |
- When the request is not processed successfully, the attributes of the response body are as follows:
Name | Type | Description |
---|---|---|
logId |
string |
The UUID of the request. |
errorCode |
integer |
Error code. Same as the response status code. |
errorMsg |
string |
Error message. |
The main operations provided by the service are as follows:
infer
Obtain the seal text recognition result.
POST /seal-recognition
- The attributes of the request body are as follows:
Name | Type | Description | Required |
---|---|---|---|
file |
string |
The URL of an image or PDF file accessible by the server, or the Base64-encoded content of the file. By default, for PDF files exceeding 10 pages, only the content of the first 10 pages will be processed. To remove the page limit, please add the following configuration to the pipeline configuration file:
|
Yes |
fileType |
integer | null |
The type of file. 0 indicates a PDF file, 1 indicates an image file. If this attribute is not present in the request body, the file type will be inferred from the URL. |
No |
useDocOrientationClassify |
boolean | null |
Please refer to the description of the use_doc_orientation_classify parameter of the pipeline object's predict method. |
No |
useDocUnwarping |
boolean | null |
Please refer to the description of the use_doc_unwarping parameter of the pipeline object's predict method. |
No |
useLayoutDetection |
boolean | null |
Please refer to the description of the use_layout_detection parameter of the pipeline object's predict method. |
No |
layoutThreshold |
number | null |
Please refer to the description of the layout_threshold parameter of the pipeline object's predict method. |
No |
layoutNms |
boolean | null |
Please refer to the description of the layout_nms parameter of the pipeline object's predict method. |
No |
layoutUnclipRatio |
number | array | null |
Please refer to the description of the layout_unclip_ratio parameter of the pipeline object's predict method. |
No |
layoutMergeBboxesMode |
string | null |
Please refer to the description of the layout_merge_bboxes_mode parameter of the pipeline object's predict method. |
No |
sealDetLimitSideLen |
integer | null |
Please refer to the description of the seal_det_limit_side_len parameter of the pipeline object's predict method. |
No |
sealDetLimitType |
string | null |
Please refer to the description of the seal_det_limit_type parameter of the pipeline object's predict method. |
No |
sealDetThresh |
number | null |
Please refer to the description of the seal_det_thresh parameter of the pipeline object's predict method. |
No |
sealDetBoxThresh |
number | null |
Please refer to the description of the seal_det_box_thresh parameter of the pipeline object's predict method. |
No |
sealDetUnclipRatio |
number | null |
Please refer to the description of the seal_det_unclip_ratio parameter of the pipeline object's predict method. |
No |
sealRecScoreThresh |
number | null |
Please refer to the description of the seal_rec_score_thresh parameter of the pipeline object's predict method. |
No |
- When the request is processed successfully, the
result
in the response body has the following properties:
Name | Type | Meaning |
---|---|---|
sealRecResults |
object |
The seal text recognition result. The array length is 1 (for image input) or the actual number of document pages processed (for PDF input). For PDF input, each element in the array represents the result of each page actually processed in the PDF file. |
dataInfo |
object |
Information about the input data. |
Each element in sealRecResults
is an object
with the following properties:
Name | Type | Meaning |
---|---|---|
prunedResult |
object |
A simplified version of the res field in the JSON representation generated by the predict method of the production object, where the input_path and the page_index fields are removed. |
outputImages |
object | null |
See the description of the img attribute of the result of the pipeline prediction. The images are in JPEG format and encoded in Base64. |
inputImage |
string | null |
The input image. The image is in JPEG format and encoded in Base64. |
Multi-language Service Invocation Example
Python
import base64
import requests
API_URL = "http://localhost:8080/seal-recognition"
file_path = "./demo.jpg"
with open(file_path, "rb") as file:
file_bytes = file.read()
file_data = base64.b64encode(file_bytes).decode("ascii")
payload = {"file": file_data, "fileType": 1}
response = requests.post(API_URL, json=payload)
assert response.status_code == 200
result = response.json()["result"]
for i, res in enumerate(result["sealRecResults"]):
print(res["prunedResult"])
for img_name, img in res["outputImages"].items():
img_path = f"{img_name}_{i}.jpg"
with open(img_path, "wb") as f:
f.write(base64.b64decode(img))
print(f"Output image saved at {img_path}")
C++
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include "cpp-httplib/httplib.h" // https://github.com/Huiyicc/cpp-httplib
#include "nlohmann/json.hpp" // https://github.com/nlohmann/json
#include "base64.hpp" // https://github.com/tobiaslocker/base64
int main() {
httplib::Client client("localhost", 8080);
const std::string filePath = "./demo.jpg";
std::ifstream file(filePath, std::ios::binary | std::ios::ate);
if (!file) {
std::cerr << "Error opening file: " << filePath << std::endl;
return 1;
}
std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector buffer(size);
if (!file.read(buffer.data(), size)) {
std::cerr << "Error reading file." << std::endl;
return 1;
}
std::string bufferStr(buffer.data(), static_cast(size));
std::string encodedFile = base64::to_base64(bufferStr);
nlohmann::json jsonObj;
jsonObj["file"] = encodedFile;
jsonObj["fileType"] = 1;
auto response = client.Post("/seal-recognition", jsonObj.dump(), "application/json");
if (response && response->status == 200) {
nlohmann::json jsonResponse = nlohmann::json::parse(response->body);
auto result = jsonResponse["result"];
if (!result.is_object() || !result["sealRecResults"].is_array()) {
std::cerr << "Unexpected response format." << std::endl;
return 1;
}
for (size_t i = 0; i < result["sealRecResults"].size(); ++i) {
auto res = result["sealRecResults"][i];
if (res.contains("prunedResult")) {
std::cout << "Recognized seal result: " << res["prunedResult"].dump() << std::endl;
}
if (res.contains("outputImages") && res["outputImages"].is_object()) {
for (auto& [imgName, imgData] : res["outputImages"].items()) {
std::string outputPath = imgName + "_" + std::to_string(i) + ".jpg";
std::string decodedImage = base64::from_base64(imgData.get());
std::ofstream outFile(outputPath, std::ios::binary);
if (outFile.is_open()) {
outFile.write(decodedImage.c_str(), decodedImage.size());
outFile.close();
std::cout << "Saved image: " << outputPath << std::endl;
} else {
std::cerr << "Failed to write image: " << outputPath << std::endl;
}
}
}
}
} else {
std::cerr << "Request failed." << std::endl;
if (response) {
std::cerr << "HTTP status: " << response->status << std::endl;
std::cerr << "Response body: " << response->body << std::endl;
}
return 1;
}
return 0;
}
Java
import okhttp3.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Base64;
public class Main {
public static void main(String[] args) throws IOException {
String API_URL = "http://localhost:8080/seal-recognition";
String imagePath = "./demo.jpg";
File file = new File(imagePath);
byte[] fileContent = java.nio.file.Files.readAllBytes(file.toPath());
String base64Image = Base64.getEncoder().encodeToString(fileContent);
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode payload = objectMapper.createObjectNode();
payload.put("file", base64Image);
payload.put("fileType", 1);
OkHttpClient client = new OkHttpClient();
MediaType JSON = MediaType.get("application/json; charset=utf-8");
RequestBody body = RequestBody.create(JSON, payload.toString());
Request request = new Request.Builder()
.url(API_URL)
.post(body)
.build();
try (Response response = client.newCall(request).execute()) {
if (response.isSuccessful()) {
String responseBody = response.body().string();
JsonNode root = objectMapper.readTree(responseBody);
JsonNode result = root.get("result");
JsonNode sealRecResults = result.get("sealRecResults");
for (int i = 0; i < sealRecResults.size(); i++) {
JsonNode item = sealRecResults.get(i);
int finalI = i;
JsonNode prunedResult = item.get("prunedResult");
System.out.println("Pruned Result [" + i + "]: " + prunedResult.toString());
JsonNode outputImages = item.get("outputImages");
if (outputImages != null && outputImages.isObject()) {
outputImages.fieldNames().forEachRemaining(imgName -> {
try {
String imgBase64 = outputImages.get(imgName).asText();
byte[] imgBytes = Base64.getDecoder().decode(imgBase64);
String imgPath = imgName + "_" + finalI + ".jpg";
try (FileOutputStream fos = new FileOutputStream(imgPath)) {
fos.write(imgBytes);
System.out.println("Saved image: " + imgPath);
}
} catch (IOException e) {
System.err.println("Failed to save image: " + e.getMessage());
}
});
}
}
} else {
System.err.println("Request failed with HTTP code: " + response.code());
}
}
}
}
Go
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
func main() {
API_URL := "http://localhost:8080/seal-recognition"
filePath := "./demo.jpg"
fileBytes, err := ioutil.ReadFile(filePath)
if err != nil {
fmt.Printf("Error reading file: %v\n", err)
return
}
fileData := base64.StdEncoding.EncodeToString(fileBytes)
payload := map[string]interface{}{
"file": fileData,
"fileType": 1,
}
payloadBytes, err := json.Marshal(payload)
if err != nil {
fmt.Printf("Error marshaling payload: %v\n", err)
return
}
client := &http.Client{}
req, err := http.NewRequest("POST", API_URL, bytes.NewBuffer(payloadBytes))
if err != nil {
fmt.Printf("Error creating request: %v\n", err)
return
}
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
fmt.Printf("Error sending request: %v\n", err)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
fmt.Printf("Unexpected status code: %d\n", resp.StatusCode)
return
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Error reading response body: %v\n", err)
return
}
type SealResult struct {
PrunedResult map[string]interface{} `json:"prunedResult"`
OutputImages map[string]string `json:"outputImages"`
InputImage *string `json:"inputImage"`
}
type Response struct {
Result struct {
SealRecResults []SealResult `json:"sealRecResults"`
DataInfo interface{} `json:"dataInfo"`
} `json:"result"`
}
var respData Response
if err := json.Unmarshal(body, &respData); err != nil {
fmt.Printf("Error unmarshaling response: %v\n", err)
return
}
for i, res := range respData.Result.SealRecResults {
fmt.Printf("Pruned Result %d: %+v\n", i, res.PrunedResult)
for name, imgBase64 := range res.OutputImages {
imgBytes, err := base64.StdEncoding.DecodeString(imgBase64)
if err != nil {
fmt.Printf("Error decoding image %s: %v\n", name, err)
continue
}
filename := fmt.Sprintf("%s_%d.jpg", name, i)
if err := ioutil.WriteFile(filename, imgBytes, 0644); err != nil {
fmt.Printf("Error saving image %s: %v\n", filename, err)
continue
}
fmt.Printf("Output image saved at %s\n", filename)
}
}
}
C#
using System;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
class Program
{
static readonly string API_URL = "http://localhost:8080/seal-recognition";
static readonly string inputFilePath = "./demo.jpg";
static async Task Main(string[] args)
{
var httpClient = new HttpClient();
byte[] fileBytes = File.ReadAllBytes(inputFilePath);
string fileData = Convert.ToBase64String(fileBytes);
var payload = new JObject
{
{ "file", fileData },
{ "fileType", 1 }
};
var content = new StringContent(payload.ToString(), Encoding.UTF8, "application/json");
HttpResponseMessage response = await httpClient.PostAsync(API_URL, content);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
JObject jsonResponse = JObject.Parse(responseBody);
JArray sealRecResults = (JArray)jsonResponse["result"]["sealRecResults"];
for (int i = 0; i < sealRecResults.Count; i++)
{
var res = sealRecResults[i];
Console.WriteLine($"[{i}] prunedResult:\n{res["prunedResult"]}");
JObject outputImages = res["outputImages"] as JObject;
if (outputImages != null)
{
foreach (var img in outputImages)
{
string imgName = img.Key;
string base64Img = img.Value?.ToString();
if (!string.IsNullOrEmpty(base64Img))
{
string imgPath = $"{imgName}_{i}.jpg";
byte[] imageBytes = Convert.FromBase64String(base64Img);
File.WriteAllBytes(imgPath, imageBytes);
Console.WriteLine($"Output image saved at {imgPath}");
}
}
}
}
}
}
Node.js
const axios = require('axios');
const fs = require('fs');
const path = require('path');
const API_URL = 'http://localhost:8080/seal-recognition';
const imagePath = './demo.jpg';
function encodeImageToBase64(filePath) {
const bitmap = fs.readFileSync(filePath);
return Buffer.from(bitmap).toString('base64');
}
const payload = {
file: encodeImageToBase64(imagePath),
fileType: 1
};
axios.post(API_URL, payload)
.then((response) => {
const result = response.data["result"];
const sealRecResults = result["sealRecResults"];
sealRecResults.forEach((res, i) => {
console.log(`\n[${i}] prunedResult:\n`, res["prunedResult"]);
const outputImages = res["outputImages"];
if (outputImages) {
for (const [imgName, base64Img] of Object.entries(outputImages)) {
const imgBuffer = Buffer.from(base64Img, 'base64');
const fileName = `${imgName}_${i}.jpg`;
fs.writeFileSync(fileName, imgBuffer);
console.log(`Output image saved at ${fileName}`);
}
} else {
console.log(`[${i}] No outputImages found.`);
}
});
})
.catch((error) => {
console.error('Error occurred while calling the API:', error.message);
});
PHP
<?php
$API_URL = "http://localhost:8080/seal-recognition";
$image_path = "./demo.jpg";
$image_data = base64_encode(file_get_contents($image_path));
$payload = array("file" => $image_data, "fileType" => 1);
$ch = curl_init($API_URL);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response, true)["result"]["sealRecResults"];
foreach ($result as $i => $item) {
echo "[$i] prunedResult:\n";
print_r($item["prunedResult"]);
if (!empty($item["outputImages"])) {
foreach ($item["outputImages"] as $img_name => $base64_img) {
if (!empty($base64_img)) {
$output_path = "{$img_name}_{$i}.jpg";
file_put_contents($output_path, base64_decode($base64_img));
echo "Output image saved at $output_path\n";
}
}
} else {
echo "No outputImages found for item $i\n";
}
}
?>
4. Custom Development¶
If the default model weights provided by the seal text recognition pipeline do not meet your requirements in terms of accuracy or speed, you can try to fine-tune the existing models using your own domain-specific or application data to improve the recognition performance of the seal text recognition pipeline in your scenario.
4.1 Model Fine-Tuning¶
Since the seal text recognition pipeline consists of several modules, if the pipeline's performance does not meet expectations, the issue may arise from any one of these modules. You can analyze images with poor recognition results to identify which module is problematic and refer to the corresponding fine-tuning tutorial links in the table below for model fine-tuning.
Scenario | Fine-Tuning Module | Fine-Tuning Reference Link |
---|---|---|
Inaccurate or missing seal position detection | Layout Detection Module | Link |
Missing text detection | Text Detection Module | Link |
Inaccurate text content | Text Recognition Module | Link |
Inaccurate full-image rotation correction | Document Image Orientation Classification Module | Link |
Inaccurate image distortion correction | Text Image Correction Module | Not supported for fine-tuning |
4.2 Model Application¶
After you complete the fine-tuning training with a private dataset, you can obtain the local model weight files. You can then use the fine-tuned model weights by specifying the local model save path through parameters or by using a custom pipeline configuration file.
4.2.1 Specify Local Model Path via Parameters¶
When initializing the pipeline object, specify the local model path through parameters. Taking the usage of fine-tuned weights for a text detection model as an example, the demonstration is as follows:
Command line method:
# Single card training (default training method)
python3 tools/train.py -c configs/table/SLANet_plus.yml \
-o Global.pretrained_model=./SLANet_plus_pretrained.pdparams
Train.dataset.data_dir=./table_rec_dataset_examples \
Train.dataset.label_file_list='[./table_rec_dataset_examples/train.txt]' \
Eval.dataset.data_dir=./table_rec_dataset_examples \
Eval.dataset.label_file_list='[./table_rec_dataset_examples/val.txt]'
# Multi-card training, specify card numbers via --gpus parameter
python3 -m paddle.distributed.launch --gpus '0,1,2,3' tools/train.py \
-c configs/table/SLANet_plus.yml \
-o Global.pretrained_model=./SLANet_plus_pretrained.pdparams
-o Global.pretrained_model=./PP-OCRv5_server_det_pretrained.pdparams \
Train.dataset.data_dir=./table_rec_dataset_examples \
Train.dataset.label_file_list='[./table_rec_dataset_examples/train.txt]' \
Eval.dataset.data_dir=./table_rec_dataset_examples \
Eval.dataset.label_file_list='[./table_rec_dataset_examples/val.txt]'
4.3 Model Evaluation¶
You can evaluate the trained weights, such as output/xxx/xxx.pdparams
, using the following command:
# Note to set the path of pretrained_model to the local path. If you use the model saved by your own training, please modify the path and file name to {path/to/weights}/{model_name}.
# Demo test set evaluation
python3 tools/eval.py -c configs/table/SLANet_plus.yml -o \
Global.pretrained_model=output/xxx/xxx.pdparams
Eval.dataset.data_dir=./table_rec_dataset_examples \
Eval.dataset.label_file_list='[./table_rec_dataset_examples/val.txt]'
4.4 Model Export¶
python3 tools/export_model.py -c configs/table/SLANet_plus.yml -o \
Global.pretrained_model=output/xxx/xxx.pdparams \
Global.save_inference_dir="./SLANet_plus_infer/"
After exporting the model, the static graph model will be stored in ./SLANet_plus_infer/
in the current directory. In this directory, you will see the following files: