---
comments: true
---
# 公式识别产线使用教程
## 1. 公式识别产线介绍
公式识别是一种自动从文档或图像中识别和提取LaTeX公式内容及其结构的技术,广泛应用于数学、物理、计算机科学等领域的文档编辑和数据分析。通过使用计算机视觉和机器学习算法,公式识别能够将复杂的数学公式信息转换为可编辑的LaTeX格式,方便用户进一步处理和分析数据。
通用公式识别产线中包含版面区域检测模块和公式识别模块。
如您更考虑模型精度,请选择精度较高的模型,如您更考虑模型推理速度,请选择推理速度较快的模型,如您更考虑模型存储大小,请选择存储大小较小的模型。
版面区域检测模块模型:
| 模型 | 模型下载链接 | mAP(%) | GPU推理耗时(ms) | CPU推理耗时(ms) | 模型存储大小(M) |
|---|---|---|---|---|---|
| RT-DETR-H_layout_17cls | 推理模型/训练模型 | 92.6 | 115.126 | 3827.25 | 470.2M |
注:以上精度指标的评估集是 PaddleX 自建的版面区域检测数据集,包含 1w 张图片。以上所有模型 GPU 推理耗时基于 NVIDIA Tesla T4 机器,精度类型为 FP32, CPU 推理速度基于 Intel(R) Xeon(R) Gold 5117 CPU @ 2.00GHz,线程数为8,精度类型为 FP32。
公式识别模块模型:
| 模型 | 模型下载链接 | BLEU score | normed edit distance | ExpRate (%) | GPU推理耗时(ms) | CPU推理耗时(ms) | 模型存储大小 |
|---|---|---|---|---|---|---|---|
| LaTeX_OCR_rec | 推理模型/训练模型 | 0.8821 | 0.0823 | 40.01 | - | - | 89.7 M |
注:以上精度指标测量自 LaTeX-OCR公式识别测试集。以上所有模型 GPU 推理耗时基于 NVIDIA Tesla T4 机器,精度类型为 FP32, CPU 推理速度基于 Intel(R) Xeon(R) Gold 5117 CPU @ 2.00GHz,线程数为8,精度类型为 FP32。
## 2. 快速开始 PaddleX 支持在本地使用命令行或 Python 体验公式识别产线的效果。 在本地使用公式识别产线前,请确保您已经按照[PaddleX本地安装教程](../../../installation/installation.md)完成了PaddleX的wheel包安装。 ### 2.1 命令行方式体验 一行命令即可快速体验公式识别产线效果,使用 [测试文件](https://paddle-model-ecology.bj.bcebos.com/paddlex/demo_image/general_formula_recognition.png),并将 `--input` 替换为本地路径,进行预测 ```bash paddlex --pipeline formula_recognition --input general_formula_recognition.png --device gpu:0 ``` 参数说明: ``` --pipeline:产线名称,此处为公式识别产线 --input:待处理的输入图片的本地路径或URL --device 使用的GPU序号(例如gpu:0表示使用第0块GPU,gpu:1,2表示使用第1、2块GPU),也可选择使用CPU(--device cpu) ``` 在执行上述 Python 脚本时,加载的是默认的公式识别产线配置文件,若您需要自定义配置文件,可执行如下命令获取:paddlex --get_pipeline_config formula_recognition
执行后,公式识别产线配置文件将被保存在当前路径。若您希望自定义保存位置,可执行如下命令(假设自定义保存位置为 ./my_path ):
paddlex --get_pipeline_config formula_recognition --save_path ./my_path
获取产线配置文件后,可将 --pipeline 替换为配置文件保存路径,即可使配置文件生效。例如,若配置文件保存路径为 ./formula_recognition.yaml,只需执行:
paddlex --pipeline ./formula_recognition.yaml --input general_formula_recognition.png --device gpu:0
其中,--model、--device 等参数无需指定,将使用配置文件中的参数。若依然指定了参数,将以指定的参数为准。
{'input_path': 'general_formula_recognition.png', 'layout_result': {'input_path': 'general_formula_recognition.png', 'boxes': [{'cls_id': 3, 'label': 'number', 'score': 0.7580855488777161, 'coordinate': [1028.3635, 205.46213, 1038.953, 222.99033]}, {'cls_id': 0, 'label': 'paragraph_title', 'score': 0.8882032632827759, 'coordinate': [272.75305, 204.50894, 433.7473, 226.17996]}, {'cls_id': 2, 'label': 'text', 'score': 0.9685840606689453, 'coordinate': [272.75928, 282.17773, 1041.9316, 374.44687]}, {'cls_id': 2, 'label': 'text', 'score': 0.9559416770935059, 'coordinate': [272.39056, 385.54114, 1044.1521, 443.8598]}, {'cls_id': 2, 'label': 'text', 'score': 0.9610629081726074, 'coordinate': [272.40817, 467.2738, 1045.1033, 563.4855]}, {'cls_id': 7, 'label': 'formula', 'score': 0.8916195034980774, 'coordinate': [503.45743, 594.6236, 1040.6804, 619.73895]}, {'cls_id': 2, 'label': 'text', 'score': 0.973675549030304, 'coordinate': [272.32007, 648.8599, 1040.8702, 775.15686]}, {'cls_id': 7, 'label': 'formula', 'score': 0.9038916230201721, 'coordinate': [554.2307, 803.5825, 1040.4657, 855.3159]}, {'cls_id': 2, 'label': 'text', 'score': 0.9025381803512573, 'coordinate': [272.535, 875.1402, 573.1086, 898.3587]}, {'cls_id': 2, 'label': 'text', 'score': 0.8336610794067383, 'coordinate': [317.48013, 909.60864, 966.8498, 933.7868]}, {'cls_id': 2, 'label': 'text', 'score': 0.8779091238975525, 'coordinate': [19.704018, 653.322, 72.433235, 1215.1992]}, {'cls_id': 2, 'label': 'text', 'score': 0.8832409977912903, 'coordinate': [272.13028, 958.50806, 1039.7928, 1019.476]}, {'cls_id': 7, 'label': 'formula', 'score': 0.9088466167449951, 'coordinate': [517.1226, 1042.3978, 1040.2208, 1095.7457]}, {'cls_id': 2, 'label': 'text', 'score': 0.9587949514389038, 'coordinate': [272.03336, 1112.9269, 1041.0201, 1206.8417]}, {'cls_id': 2, 'label': 'text', 'score': 0.8885666131973267, 'coordinate': [271.7495, 1231.8752, 710.44495, 1255.7981]}, {'cls_id': 7, 'label': 'formula', 'score': 0.8907185196876526, 'coordinate': [581.2295, 1287.4525, 1039.8014, 1312.772]}, {'cls_id': 2, 'label': 'text', 'score': 0.9559596180915833, 'coordinate': [273.1827, 1341.421, 1041.0299, 1401.7255]}, {'cls_id': 2, 'label': 'text', 'score': 0.875311553478241, 'coordinate': [272.8338, 1427.3711, 789.7108, 1451.1359]}, {'cls_id': 7, 'label': 'formula', 'score': 0.9152213931083679, 'coordinate': [524.9582, 1474.8136, 1041.6333, 1530.7142]}, {'cls_id': 2, 'label': 'text', 'score': 0.9584835767745972, 'coordinate': [272.81665, 1549.524, 1042.9962, 1608.7157]}]}, 'ocr_result': {}, 'table_result': [], 'dt_polys': [array([[ 503.45743, 594.6236 ],
[1040.6804 , 594.6236 ],
[1040.6804 , 619.73895],
[ 503.45743, 619.73895]], dtype=float32), array([[ 554.2307, 803.5825],
[1040.4657, 803.5825],
[1040.4657, 855.3159],
[ 554.2307, 855.3159]], dtype=float32), array([[ 517.1226, 1042.3978],
[1040.2208, 1042.3978],
[1040.2208, 1095.7457],
[ 517.1226, 1095.7457]], dtype=float32), array([[ 581.2295, 1287.4525],
[1039.8014, 1287.4525],
[1039.8014, 1312.772 ],
[ 581.2295, 1312.772 ]], dtype=float32), array([[ 524.9582, 1474.8136],
[1041.6333, 1474.8136],
[1041.6333, 1530.7142],
[ 524.9582, 1530.7142]], dtype=float32)], 'rec_formula': ['F({\bf x})=C(F_{1}(x_{1}),\cdot\cdot\cdot,F_{N}(x_{N})).\qquad\qquad\qquad(1)', 'p(\mathbf{x})=c(\mathbf{u})\prod_{i}p(x_{i}).\qquad\qquad\qquad\qquad\qquad\quad\quad~~\quad~~~~~~~~~~~~~~~(2)', 'H_{c}({\bf x})=-\int_{{\bf{u}}}c({\bf{u}})\log c({\bf{u}})d{\bf{u}}.~~~~~~~~~~~~~~~~~~~~~(3)', 'I({\bf x})=-H_{c}({\bf x}).\qquad\qquad\qquad\qquad(4)', 'H({\bf x})=\sum_{i}H(x_{i})+H_{c}({\bf x}).\eqno\qquad\qquad\qquad(5)']}
其中,dt_polys为检测到的公式区域坐标, rec_formula为检测到的公式。
可视化图片默认不进行保存,您可以通过 `--save_path` 自定义保存路径,随后所有结果将被保存在指定路径下。公式识别可视化需要单独配置环境,请您参考[2.3 公式识别产线可视化](#23-公式识别产线可视化) 对LaTeX渲染引擎进行安装。
### 2.2 Python脚本方式集成
几行代码即可完成产线的快速推理,以公式识别产线为例:
```python
from paddlex import create_pipeline
pipeline = create_pipeline(pipeline="formula_recognition")
output = pipeline.predict("general_formula_recognition.png")
for res in output:
res.print()
```
> ❗ Python脚本运行得到的结果与命令行方式相同。
在上述 Python 脚本中,执行了如下几个步骤:
(1)实例化 `create_pipeline` 实例化 公式识别产线对象:具体参数说明如下:
| 参数 | 参数说明 | 参数类型 | 默认值 |
|---|---|---|---|
pipeline |
产线名称或是产线配置文件路径。如为产线名称,则必须为 PaddleX 所支持的产线。 | str |
无 |
device |
产线模型推理设备。支持:“gpu”,“cpu”。 | str |
gpu |
use_hpip |
是否启用高性能推理,仅当该产线支持高性能推理时可用。 | bool |
False |
| 参数类型 | 参数说明 |
|---|---|
| Python Var | 支持直接传入Python变量,如numpy.ndarray表示的图像数据。 |
| str | 支持传入待预测数据文件路径,如图像文件的本地路径:/root/data/img.jpg。 |
| str | 支持传入待预测数据文件URL,如图像文件的网络URL:示例。 |
| str | 支持传入本地目录,该目录下需包含待预测数据文件,如本地路径:/root/data/。 |
| dict | 支持传入字典类型,字典的key需与具体任务对应,如图像分类任务对应\"img\",字典的val支持上述类型数据,例如:{\"img\": \"/root/data1\"}。 |
| list | 支持传入列表,列表元素需为上述类型数据,如[numpy.ndarray, numpy.ndarray],[\"/root/data/img1.jpg\", \"/root/data/img2.jpg\"],[\"/root/data1\", \"/root/data2\"],[{\"img\": \"/root/data1\"}, {\"img\": \"/root/data2/img.jpg\"}]。 |
| 方法 | 说明 | 方法参数 |
|---|---|---|
| 打印结果到终端 | - format_json:bool类型,是否对输出内容进行使用json缩进格式化,默认为True;- indent:int类型,json格式化设置,仅当format_json为True时有效,默认为4;- ensure_ascii:bool类型,json格式化设置,仅当format_json为True时有效,默认为False; |
|
| save_to_json | 将结果保存为json格式的文件 | - save_path:str类型,保存的文件路径,当为目录时,保存文件命名与输入文件类型命名一致;- indent:int类型,json格式化设置,默认为4;- ensure_ascii:bool类型,json格式化设置,默认为False; |
| save_to_img | 将结果保存为图像格式的文件 | - save_path:str类型,保存的文件路径,当为目录时,保存文件命名与输入文件类型命名一致; |
对于服务提供的主要操作:
200,响应体的属性如下:| 名称 | 类型 | 含义 |
|---|---|---|
logId |
string |
请求的UUID。 |
errorCode |
integer |
错误码。固定为0。 |
errorMsg |
string |
错误说明。固定为"Success"。 |
result |
object |
操作结果。 |
| 名称 | 类型 | 含义 |
|---|---|---|
logId |
string |
请求的UUID。 |
errorCode |
integer |
错误码。与响应状态码相同。 |
errorMsg |
string |
错误说明。 |
服务提供的主要操作如下:
infer获取图像公式识别结果。
POST /formula-recognition
| 名称 | 类型 | 含义 | 是否必填 |
|---|---|---|---|
file |
string |
服务可访问的图像文件或PDF文件的URL,或上述类型文件内容的Base64编码结果。对于超过10页的PDF文件,只有前10页的内容会被使用。 | 是 |
fileType |
integer |
文件类型。0表示PDF文件,1表示图像文件。若请求体无此属性,则服务将尝试根据URL自动推断文件类型。 |
否 |
inferenceParams |
object |
推理参数。 | 否 |
inferenceParams的属性如下:
| 名称 | 类型 | 含义 | 是否必填 |
|---|---|---|---|
maxLongSide |
integer |
推理时,若文本检测模型的输入图像较长边的长度大于maxLongSide,则将对图像进行缩放,使其较长边的长度等于maxLongSide。 |
否 |
result具有如下属性:| 名称 | 类型 | 含义 |
|---|---|---|
formulaRecResults |
object |
公式识别结果。数组长度为1(对于图像输入)或文档页数与10中的较小者(对于PDF输入)。对于PDF输入,数组中的每个元素依次表示PDF文件中每一页的处理结果。 |
dataInfo |
object |
输入数据信息。 |
formulaRecResults中的每个元素为一个object,具有如下属性:
| 名称 | 类型 | 含义 |
|---|---|---|
formulas |
array |
公式位置和内容。 |
inputImage |
string |
输入图像。图像为JPEG格式,使用Base64编码。 |
layoutImage |
string |
版面区域检测结果图。图像为JPEG格式,使用Base64编码。 |
ocrImage |
string |
OCR结果图。图像为JPEG格式,使用Base64编码。 |
formulas中的每个元素为一个object,具有如下属性:
| 名称 | 类型 | 含义 |
|---|---|---|
poly |
array |
公式位置。数组中元素依次为包围文本的多边形的顶点坐标。 |
latex |
string |
公式内容。 |
import base64
import requests
API_URL = "http://localhost:8080/formula-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["formulaRecResults"]):
print("Detected formulas:")
print(res["formulas"])
layout_img_path = f"layout_{i}.jpg"
with open(layout_img_path, "wb") as f:
f.write(base64.b64decode(res["layoutImage"]))
print(f"Output image saved at {layout_img_path}")