File size: 36,366 Bytes
8207382 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 | ---
comments: true
---
# 文本检测模块使用教程
## 一、概述
文本检测模块是OCR(光学字符识别)系统中的关键组成部分,负责在图像中定位和标记出包含文本的区域。该模块的性能直接影响到整个OCR系统的准确性和效率。文本检测模块通常会输出文本区域的边界框(Bounding Boxes),这些边界框将作为输入传递给文本识别模块进行后续处理。
## 二、支持模型列表
> 推理耗时仅包含模型推理耗时,不包含前后处理耗时。表格中的“常规模式”耗时对应本地 <code>paddle_static</code> 推理引擎。
<table>
<thead>
<tr>
<th>模型</th><th>模型下载链接</th>
<th>检测Hmean(%)</th>
<th>GPU推理耗时(ms)<br/>[常规模式 / 高性能模式]</th>
<th>CPU推理耗时(ms)<br/>[常规模式 / 高性能模式]</th>
<th>模型存储大小(MB)</th>
<th>介绍</th>
</tr>
</thead>
<tbody>
<tr>
<td>PP-OCRv6_medium_det</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/PP-OCRv6_medium_det_infer.tar">推理模型</a>/<a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_pretrained_model/PP-OCRv6_medium_det_pretrained.pdparams">训练模型</a></td>
<td>86.2*</td>
<td>- / -</td>
<td>- / -</td>
<td>59.4</td>
<td>PP-OCRv6 的中等规模文本检测模型,基于 PPLCNetV4 + RepLKFPN,精度最高,适合服务端部署</td>
</tr>
<tr>
<td>PP-OCRv6_small_det</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/PP-OCRv6_small_det_infer.tar">推理模型</a>/<a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_pretrained_model/PP-OCRv6_small_det_pretrained.pdparams">训练模型</a></td>
<td>84.1*</td>
<td>- / -</td>
<td>- / -</td>
<td>9.6</td>
<td>PP-OCRv6 的小型文本检测模型,兼顾精度与效率,适合移动端部署</td>
</tr>
<tr>
<td>PP-OCRv6_tiny_det</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/PP-OCRv6_tiny_det_infer.tar">推理模型</a>/<a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_pretrained_model/PP-OCRv6_tiny_det_pretrained.pdparams">训练模型</a></td>
<td>80.6*</td>
<td>- / -</td>
<td>- / -</td>
<td>1.9</td>
<td>PP-OCRv6 的超轻量文本检测模型(0.43M 参数),适合端侧/IoT 场景</td>
</tr>
<tr>
<td>PP-OCRv5_server_det</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/PP-OCRv5_server_det_infer.tar">推理模型</a>/<a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_pretrained_model/PP-OCRv5_server_det_pretrained.pdparams">训练模型</a></td>
<td>83.8</td>
<td>89.55 / 70.19</td>
<td>383.15 / 383.15</td>
<td>84.3</td>
<td>PP-OCRv5 的服务端文本检测模型,精度更高,适合在性能较好的服务器上部署</td>
</tr>
<tr>
<td>PP-OCRv5_mobile_det</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/PP-OCRv5_mobile_det_infer.tar">推理模型</a>/<a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_pretrained_model/PP-OCRv5_mobile_det_pretrained.pdparams">训练模型</a></td>
<td>79.0</td>
<td>10.67 / 6.36</td>
<td>57.77 / 28.15</td>
<td>4.7</td>
<td>PP-OCRv5 的移动端文本检测模型,效率更高,适合在端侧设备部署</td>
</tr>
<tr>
<td>PP-OCRv4_server_det</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/PP-OCRv4_server_det_infer.tar">推理模型</a>/<a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_pretrained_model/PP-OCRv4_server_det_pretrained.pdparams">训练模型</a></td>
<td>69.2</td>
<td>127.82 / 98.87</td>
<td>585.95 / 489.77</td>
<td>109</td>
<td>PP-OCRv4 的服务端文本检测模型,精度更高,适合在性能较好的服务器上部署</td>
</tr>
<tr>
<td>PP-OCRv4_mobile_det</td>
<td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/PP-OCRv4_mobile_det_infer.tar">推理模型</a>/<a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_pretrained_model/PP-OCRv4_mobile_det_pretrained.pdparams">训练模型</a></td>
<td>63.8</td>
<td>9.87 / 4.17</td>
<td>56.60 / 20.79</td>
<td>4.7</td>
<td>PP-OCRv4 的移动端文本检测模型,效率更高,适合在端侧设备部署</td>
</tr>
</tbody>
</table>
> *注:PP-OCRv6 指标基于内部多场景评估集测得,PP-OCRv5/v4 指标基于通用评估集测得,两者评估集不同,指标不可直接对比。
<strong>测试环境说明:</strong>
<ul>
<li><b>性能测试环境</b>
<ul>
<li><strong>测试数据集:</strong>PaddleOCR3.0 全新构建多语种(包含中、繁、英、日),覆盖街景、网图、文档、手写、模糊、旋转、扭曲等多个场景的文本检测数据集,包含2677 张图片。</li>
<li><strong>硬件配置:</strong>
<ul>
<li>GPU:NVIDIA Tesla T4</li>
<li>CPU:Intel Xeon Gold 6271C @ 2.60GHz</li>
</ul>
</li>
<li><strong>软件环境:</strong>
<ul>
<li>Ubuntu 20.04 / CUDA 11.8 / cuDNN 8.9 / TensorRT 8.6.1.6</li>
<li>paddlepaddle-gpu 3.0.0 / paddleocr 3.0.3</li>
</ul>
</li>
</ul>
</li>
<li><b>推理模式说明</b></li>
</ul>
<table border="1">
<thead>
<tr>
<th>模式</th>
<th>GPU配置</th>
<th>CPU配置</th>
<th>加速技术组合</th>
</tr>
</thead>
<tbody>
<tr>
<td>常规模式</td>
<td>FP32精度 / 无TRT加速</td>
<td>FP32精度 / 8线程</td>
<td>PaddleInference</td>
</tr>
<tr>
<td>高性能模式</td>
<td>选择先验精度类型和加速策略的最优组合</td>
<td>FP32精度 / 8线程</td>
<td>选择先验最优后端(Paddle/OpenVINO/TRT等)</td>
</tr>
</tbody>
</table>
## 三、快速开始
> ❗ 在快速开始前,请先安装 PaddleOCR 的 wheel 包,详细请参考 [安装教程](../installation.md)。
### 3.1 环境准备
#### 3.1.1 基础安装
```bash
# 安装基础版本(仅包含文本检测功能)
pip install paddleocr
# 安装完整版本(包含所有功能)
pip install "paddleocr[all]"
```
#### 3.1.2 环境验证
```python
# 验证安装是否成功
import paddleocr
print(f"PaddleOCR版本: {paddleocr.__version__}")
# 验证GPU是否可用
import paddle
print(f"Paddle版本: {paddle.__version__}")
print(f"GPU可用: {paddle.is_compiled_with_cuda()}")
print(f"GPU数量: {paddle.device.cuda.device_count()}")
```
### 3.2 命令行快速体验
使用一行命令即可快速体验:
```bash
# 使用默认模型进行文本检测
paddleocr text_detection -i https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/general_ocr_001.png
# 指定模型
paddleocr text_detection -i general_ocr_001.png --model_name PP-OCRv6_small_det
```
上述示例默认使用 <code>paddle_static</code> 推理引擎,请先按照[飞桨框架安装](../paddlepaddle_installation.md)完成 PaddlePaddle 安装。
如果选择 `transformers` 作为推理引擎,请确保已配置 Transformers 环境,然后执行如下命令:
```bash
# 使用 transformers 引擎进行推理
paddleocr text_detection -i https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/general_ocr_001.png \
--engine transformers
```
如果选择 `onnxruntime` 作为推理引擎,请确保已配置 ONNX Runtime 环境,然后执行如下命令:
```bash
# 使用 onnxruntime 引擎进行推理
paddleocr text_detection -i https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/general_ocr_001.png \
--engine onnxruntime
```
在大多数场景下,默认的 `paddle_static` 推理引擎通常具备更好的推理性能,建议优先使用。
<b>注:</b>PaddleOCR 官方模型默认从 HuggingFace 获取,如运行环境访问 HuggingFace 不便,可通过环境变量修改模型源为 BOS:`PADDLE_PDX_MODEL_SOURCE="BOS"`,未来将支持更多主流模型源;
### 3.3 Python API 使用
您也可以将文本检测的模块中的模型推理集成到您的项目中。运行以下代码前,请您下载[示例图片](https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/general_ocr_001.png)到本地。
```python
from paddleocr import TextDetection
model = TextDetection()
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")
```
上述示例默认使用 <code>paddle_static</code> 推理引擎,请先按照[飞桨框架安装](../paddlepaddle_installation.md)完成 PaddlePaddle 安装。
如果选择 `transformers` 作为推理引擎,请确保已配置 Transformers 环境,然后执行如下代码:
```python
from paddleocr import TextDetection
model = TextDetection(engine="transformers")
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")
```
如果选择 `onnxruntime` 作为推理引擎,请确保已配置 ONNX Runtime 环境,然后执行如下代码:
```python
from paddleocr import TextDetection
model = TextDetection(engine="onnxruntime")
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")
```
在大多数场景下,默认的 `paddle_static` 推理引擎通常具备更好的推理性能,建议优先使用。
训练后的模型如果想使用 `paddle_dynamic` 或 `transformers` 引擎,请参考后文 [推理引擎](#五推理引擎) 中的 [权重转换](#52-权重转换) 部分将模型由 `pdparams` 格式通过 PaddleX 转换为 `safetensors` 格式。
运行后,得到的结果为:
```bash
{'res': {'input_path': 'general_ocr_001.png', 'page_index': None, 'dt_polys': array([[[ 75, 549],
...,
[ 77, 586]],
...,
[[ 31, 406],
...,
[ 34, 455]]], dtype=int16), 'dt_scores': [0.873949039891189, 0.8948166013613552, 0.8842595305917041, 0.876953790920377]}}
```
运行结果参数含义如下:
<ul>
<li><code>input_path</code>:表示输入待预测图像的路径</li>
<li><code>page_index</code>:如果输入是PDF文件,则表示当前是PDF的第几页,否则为 <code>None</code></li>
<li><code>dt_polys</code>:表示预测的文本检测框,其中每个文本检测框包含一个四边形的四个顶点。其中每个顶点都是一个列表,分别表示该顶点的x坐标和y坐标</li>
<li><code>dt_scores</code>:表示预测的文本检测框的置信度</li>
</ul>
可视化图片如下:
<img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/refs/heads/main/images/modules/text_det/general_ocr_001_res.png"/>
相关方法、参数等说明如下:
* <code>TextDetection</code>类实例化文本检测模型,具体说明如下:
<table>
<thead>
<tr>
<th>参数</th>
<th>参数说明</th>
<th>参数类型</th>
<th>默认值</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>model_name</code></td>
<td><b>含义:</b>模型名称。<br/>
<b>说明:</b>
如果设置为<code>None</code>,则使用<code>PP-OCRv6_medium_det</code>。</td>
<td><code>str|None</code></td>
<td><code>None</code></td>
</tr>
<tr>
<td><code>model_dir</code></td>
<td><b>含义:</b>模型存储路径。</td>
<td><code>str|None</code></td>
<td><code>None</code></td>
</tr>
<tr>
<td><code>device</code></td>
<td><b>含义:</b>用于推理的设备。<br/>
<b>说明:</b>
<b>例如:</b><code>"cpu"</code>、<code>"gpu"</code>、<code>"npu"</code>、<code>"gpu:0"</code>、<code>"gpu:0,1"</code>。<br/>
如指定多个设备,将进行并行推理。<br/>
默认情况下,优先使用 GPU 0;若不可用则使用 CPU。
</td>
<td><code>str|None</code></td>
<td><code>None</code></td>
</tr>
<tr>
<td><code>engine</code></td>
<td><b>含义:</b>推理引擎。<br><b>说明:</b>支持 <code>None</code>(默认值)、<code>paddle</code>、<code>paddle_static</code>、<code>paddle_dynamic</code>、<code>transformers</code>、<code>onnxruntime</code>。保持为默认值 <code>None</code> 时,本地推理默认使用 <code>paddle_static</code> 引擎。详细说明、取值、兼容性规则与示例请参见 <a href="../inference_deployment/local_inference/inference_engine.md">推理引擎与配置说明</a>。</td>
<td><code>str|None</code></td>
<td><code>None</code></td>
</tr>
<tr>
<td><code>engine_config</code></td>
<td><b>含义:</b>推理引擎配置。<br><b>说明:</b>推荐与 <code>engine</code> 搭配使用。详细字段、兼容性规则与示例请参见 <a href="../inference_deployment/local_inference/inference_engine.md">推理引擎与配置说明</a>。</td>
<td><code>dict|None</code></td>
<td><code>None</code></td>
</tr>
<tr>
<td><code>enable_hpi</code></td>
<td><b>含义:</b>是否启用高性能推理。</td>
<td><code>bool</code></td>
<td><code>False</code></td>
</tr>
<tr>
<td><code>use_tensorrt</code></td>
<td><b>含义:</b>是否启用 Paddle Inference 的 TensorRT 子图引擎。<br/>
<b>说明:</b>
如果模型不支持通过 TensorRT 加速,即使设置了此标志,也不会使用加速。<br/>
对于 CUDA 11.8 版本的飞桨,兼容的 TensorRT 版本为 8.x(x>=6),建议安装 TensorRT 8.6.1.6。<br/>
</td>
<td><code>bool</code></td>
<td><code>False</code></td>
</tr>
<tr>
<td><code>precision</code></td>
<td><b>含义:</b>当使用 Paddle Inference 的 TensorRT 子图引擎时设置的计算精度。<br/>
<b>说明:</b>
<b>可选项:</b><code>"fp32"</code>、<code>"fp16"</code>。</td>
<td><code>str</code></td>
<td><code>"fp32"</code></td>
</tr>
<tr>
<td><code>enable_mkldnn</code></td>
<td>
<b>含义:</b>是否启用 MKL-DNN 加速推理。<br/>
<b>说明:</b>
如果 MKL-DNN 不可用或模型不支持通过 MKL-DNN 加速,即使设置了此标志,也不会使用加速。<br/>
</td>
<td><code>bool</code></td>
<td><code>True</code></td>
</tr>
<tr>
<td><code>mkldnn_cache_capacity</code></td>
<td>
<b>含义:</b>MKL-DNN 缓存容量。
</td>
<td><code>int</code></td>
<td><code>10</code></td>
</tr>
<tr>
<td><code>cpu_threads</code></td>
<td><b>含义:</b>在 CPU 上推理时使用的线程数量。</td>
<td><code>int</code></td>
<td><code>10</code></td>
</tr>
<tr>
<td><code>limit_side_len</code></td>
<td><b>含义:</b>检测的图像边长限制:<code>int</code> 表示边长限制数值。<br/>
<b>说明:</b>
如果设置为<code>None</code>,将使用模型默认配置。</td>
<td><code>int|None</code></td>
<td><code>None</code></td>
</tr>
<tr>
<td><code>limit_type</code></td>
<td><b>含义:</b>检测的图像边长限制,检测的边长限制类型。<br/>
<b>说明:</b>
<code>"min"</code> 表示保证图像最短边不小于det_limit_side_len,<code>"max"</code>表示保证图像最长边不大于limit_side_len。如果设置为<code>None</code>,将使用模型默认配置。</td>
<td><code>str|None</code></td>
<td><code>None</code></td>
</tr>
<tr>
<td><code>max_side_limit</code></td>
<td><b>含义:</b>检测的图像边长最大值限制:<code>int</code> 限制输入检测模型的图片最长边。<br/>
<b>说明:</b>
如果设置为 <code>None</code>,将使用模型默认配置。</td>
<td><code>int|None</code></td>
<td><code>None</code></td>
</tr>
<tr>
<td><code>thresh</code></td>
<td><b>含义:</b>像素得分阈值。输出概率图中得分大于该阈值的像素点被认为是文本像素。<br/>
<b>说明:</b>
如果设置为<code>None</code>,将使用模型默认配置。</td>
<td><code>float|None</code></td>
<td><code>None</code></td>
</tr>
<tr>
<td><code>box_thresh</code></td>
<td><b>含义:</b>检测结果边框内,所有像素点的平均得分大于该阈值时,该结果会被认为是文字区域。<br/>
<b>说明:</b>
如果设置为<code>None</code>,将使用模型默认配置。</td>
<td><code>float|None</code></td>
<td><code>None</code></td>
</tr>
<tr>
<td><code>unclip_ratio</code></td>
<td><b>含义:</b>Vatti clipping算法的扩张系数,使用该方法对文字区域进行扩张。<br/>
<b>说明:</b>
如果设置为<code>None</code>,将使用模型默认配置。</td>
<td><code>float|None</code></td>
<td><code>None</code></td>
</tr>
<tr>
<td><code>input_shape</code></td>
<td><b>含义:</b>模型输入图像尺寸,格式为 <code>(C, H, W)</code>。</td>
<td><code>tuple|None</code></td>
<td><code>None</code></td>
</tr>
</tbody>
</table>
* 调用文本检测模型的 <code>predict()</code> 方法进行推理预测,该方法会返回一个结果列表。另外,本模块还提供了 <code>predict_iter()</code> 方法。两者在参数接受和结果返回方面是完全一致的,区别在于 <code>predict_iter()</code> 返回的是一个 <code>generator</code>,能够逐步处理和获取预测结果,适合处理大型数据集或希望节省内存的场景。可以根据实际需求选择使用这两种方法中的任意一种。<code>predict()</code> 方法参数有 <code>input</code> 、<code>batch_size</code>、 <code>limit_side_len</code>、 <code>limit_type</code>、 <code>thresh</code>、 <code>box_thresh</code>、 <code>max_candidates</code>、<code>unclip_ratio</code>,具体说明如下:
<table>
<thead>
<tr>
<th>参数</th>
<th>参数说明</th>
<th>参数类型</th>
<th>默认值</th>
</tr>
</thead>
<tr>
<td><code>input</code></td>
<td><b>含义:</b>待预测数据,支持多种输入类型,必填。<br/>
<b>说明:</b>
<ul>
<li><b>Python Var</b>:如 <code>numpy.ndarray</code> 表示的图像数据</li>
<li><b>str</b>:如图像文件或者PDF文件的本地路径:<code>/root/data/img.jpg</code>;<b>如URL链接</b>,如图像文件或PDF文件的网络URL:<a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/general_ocr_rec_001.png">示例</a>;<b>如本地目录</b>,该目录下需包含待预测图像,如本地路径:<code>/root/data/</code>(当前不支持目录中包含PDF文件的预测,PDF文件需要指定到具体文件路径)</li>
<li><b>list</b>:列表元素需为上述类型数据,如<code>[numpy.ndarray, numpy.ndarray]</code>,<code>["/root/data/img1.jpg", "/root/data/img2.jpg"]</code>,<code>["/root/data1", "/root/data2"]</code></li>
</ul>
</td>
<td><code>Python Var|str|list</code></td>
<td></td>
</tr>
<tr>
<td><code>batch_size</code></td>
<td><b>含义:</b>批大小<br/>
<b>说明:</b>
可设置为任意正整数。</td>
<td><code>int</code></td>
<td>1</td>
</tr>
<tr>
<td><code>limit_side_len</code></td>
<td><b>含义:</b>参数含义与实例化参数基本相同。<br/>
<b>说明:</b>
设置为<code>None</code>表示使用实例化参数,否则该参数优先级更高。</td>
<td><code>int|None</code></td>
<td><code>None</code></td>
</tr>
<tr>
<td><code>limit_type</code></td>
<td><b>含义:</b>参数含义与实例化参数基本相同。<br/>
<b>说明:</b>
设置为<code>None</code>表示使用实例化参数,否则该参数优先级更高。</td>
<td><code>str|None</code></td>
<td><code>None</code></td>
</tr>
<tr>
<td><code>thresh</code></td>
<td><b>含义:</b>参数含义与实例化参数基本相同。<br/>
<b>说明:</b>
设置为<code>None</code>表示使用实例化参数,否则该参数优先级更高。</td>
<td><code>float|None</code></td>
<td><code>None</code></td>
</tr>
<tr>
<td><code>box_thresh</code></td>
<td><b>含义:</b>参数含义与实例化参数基本相同。<br/>
<b>说明:</b>
设置为<code>None</code>表示使用实例化参数,否则该参数优先级更高。</td>
<td><code>float|None</code></td>
<td><code>None</code></td>
</tr>
<tr>
<td><code>unclip_ratio</code></td>
<td><b>含义:</b>参数含义与实例化参数基本相同。<br/>
<b>说明:</b>
设置为<code>None</code>表示使用实例化参数,否则该参数优先级更高。</td>
<td><code>float|None</code></td>
<td><code>None</code></td>
</tr>
</tbody>
</table>
* 对预测结果进行处理,每个样本的预测结果均为对应的Result对象,且支持打印、保存为图片、保存为<code>json</code>文件的操作:
<table>
<thead>
<tr>
<th>方法</th>
<th>方法说明</th>
<th>参数</th>
<th>参数类型</th>
<th>参数说明</th>
<th>默认值</th>
</tr>
</thead>
<tr>
<td rowspan="3"><code>print()</code></td>
<td rowspan="3">打印结果到终端</td>
<td><code>format_json</code></td>
<td><code>bool</code></td>
<td>是否对输出内容进行使用 <code>JSON</code> 缩进格式化</td>
<td><code>True</code></td>
</tr>
<tr>
<td><code>indent</code></td>
<td><code>int</code></td>
<td>指定缩进级别,以美化输出的 <code>JSON</code> 数据,使其更具可读性,仅当 <code>format_json</code> 为 <code>True</code> 时有效</td>
<td>4</td>
</tr>
<tr>
<td><code>ensure_ascii</code></td>
<td><code>bool</code></td>
<td>控制是否将非 <code>ASCII</code> 字符转义为 <code>Unicode</code>。设置为 <code>True</code> 时,所有非 <code>ASCII</code> 字符将被转义;<code>False</code> 则保留原始字符,仅当<code>format_json</code>为<code>True</code>时有效</td>
<td><code>False</code></td>
</tr>
<tr>
<td rowspan="3"><code>save_to_json()</code></td>
<td rowspan="3">将结果保存为json格式的文件</td>
<td><code>save_path</code></td>
<td><code>str</code></td>
<td>保存的文件路径,当为目录时,保存文件命名与输入文件类型命名一致</td>
<td>无</td>
</tr>
<tr>
<td><code>indent</code></td>
<td><code>int</code></td>
<td>指定缩进级别,以美化输出的 <code>JSON</code> 数据,使其更具可读性,仅当 <code>format_json</code> 为 <code>True</code> 时有效</td>
<td>4</td>
</tr>
<tr>
<td><code>ensure_ascii</code></td>
<td><code>bool</code></td>
<td>控制是否将非 <code>ASCII</code> 字符转义为 <code>Unicode</code>。设置为 <code>True</code> 时,所有非 <code>ASCII</code> 字符将被转义;<code>False</code> 则保留原始字符,仅当<code>format_json</code>为<code>True</code>时有效</td>
<td><code>False</code></td>
</tr>
<tr>
<td><code>save_to_img()</code></td>
<td>将结果保存为图像格式的文件</td>
<td><code>save_path</code></td>
<td><code>str</code></td>
<td>保存的文件路径,当为目录时,保存文件命名与输入文件类型命名一致</td>
<td>无</td>
</tr>
</table>
* 此外,也支持通过属性获取带结果的可视化图像和预测结果,具体如下:
<table>
<thead>
<tr>
<th>属性</th>
<th>属性说明</th>
</tr>
</thead>
<tr>
<td rowspan="1"><code>json</code></td>
<td rowspan="1">获取预测的<code>json</code>格式的结果</td>
</tr>
<tr>
<td rowspan="1"><code>img</code></td>
<td rowspan="1">获取格式为<code>dict</code>的可视化图像</td>
</tr>
</table>
## 四、二次开发
如果以上模型在您的场景上效果仍然不理想,您可以尝试以下步骤进行二次开发,此处以训练 `PP-OCRv5_server_det` 举例,其他模型替换对应配置文件即可。首先,您需要准备文本检测的数据集,可以参考[文本检测 Demo 数据](https://paddle-model-ecology.bj.bcebos.com/paddlex/data/ocr_det_dataset_examples.tar)的格式准备,准备好后,即可按照以下步骤进行模型训练和导出,导出后,可以将模型快速集成到上述 API 中。此处以文本检测 Demo 数据示例。在训练模型之前,请确保已经按照[安装文档](../installation.md)安装了 PaddleOCR 所需要的依赖。
### 4.1 数据集、预训练模型准备
#### 4.1.1 准备数据集
```shell
# 下载示例数据集
wget https://paddle-model-ecology.bj.bcebos.com/paddlex/data/ocr_det_dataset_examples.tar
tar -xf ocr_det_dataset_examples.tar
```
#### 4.1.2 下载预训练模型
```shell
# 下载 PP-OCRv5_server_det 预训练模型
wget https://paddle-model-ecology.bj.bcebos.com/paddlex/official_pretrained_model/PP-OCRv5_server_det_pretrained.pdparams
```
### 4.2 模型训练
PaddleOCR 对代码进行了模块化,训练 `PP-OCRv5_server_det` 识别模型时需要使用 `PP-OCRv5_server_det` 的[配置文件](https://github.com/PaddlePaddle/PaddleOCR/blob/{{PADDLEOCR_GITHUB_REF}}/configs/det/PP-OCRv5/PP-OCRv5_server_det.yml)。
训练命令如下:
```bash
#单卡训练 (默认训练方式)
python3 tools/train.py -c configs/det/PP-OCRv5/PP-OCRv5_server_det.yml \
-o Global.pretrained_model=./PP-OCRv5_server_det_pretrained.pdparams \
Train.dataset.data_dir=./ocr_det_dataset_examples \
Train.dataset.label_file_list='[./ocr_det_dataset_examples/train.txt]' \
Eval.dataset.data_dir=./ocr_det_dataset_examples \
Eval.dataset.label_file_list='[./ocr_det_dataset_examples/val.txt]'
#多卡训练,通过--gpus参数指定卡号
python3 -m paddle.distributed.launch --gpus '0,1,2,3' tools/train.py \
-c configs/det/PP-OCRv5/PP-OCRv5_server_det.yml \
-o Global.pretrained_model=./PP-OCRv5_server_det_pretrained.pdparams \
Train.dataset.data_dir=./ocr_det_dataset_examples \
Train.dataset.label_file_list='[./ocr_det_dataset_examples/train.txt]' \
Eval.dataset.data_dir=./ocr_det_dataset_examples \
Eval.dataset.label_file_list='[./ocr_det_dataset_examples/val.txt]'
```
### 4.3 模型评估
您可以评估已经训练好的权重,如,`output/PP-OCRv5_server_det/best_accuracy.pdprams`,使用如下命令进行评估:
```bash
# 注意将pretrained_model的路径设置为本地路径。若使用自行训练保存的模型,请注意修改路径和文件名为{path/to/weights}/{model_name}。
# demo 测试集评估
python3 tools/eval.py -c configs/det/PP-OCRv5/PP-OCRv5_server_det.yml \
-o Global.pretrained_model=output/PP-OCRv5_server_det/best_accuracy.pdparams \
Eval.dataset.data_dir=./ocr_det_dataset_examples \
Eval.dataset.label_file_list='[./ocr_det_dataset_examples/val.txt]'
```
### 4.4 模型导出
```bash
python3 tools/export_model.py -c configs/det/PP-OCRv5/PP-OCRv5_server_det.yml -o \
Global.pretrained_model=output/PP-OCRv5_server_det/best_accuracy.pdparams \
Global.save_inference_dir="./PP-OCRv5_server_det_infer/"
```
导出模型后,静态图模型会存放于当前目录的`./PP-OCRv5_server_det_infer/`中,在该目录下,您将看到如下文件:
```
./PP-OCRv5_server_det_infer/
├── inference.json
├── inference.pdiparams
├── inference.yml
```
至此,二次开发完成,该静态图模型可以直接集成到 PaddleOCR 的 API 中。
训练后的模型如果想使用 `paddle_dynamic` 或 `transformers` 引擎,请参考后文 [推理引擎](#五推理引擎) 中的 [权重转换](#52-权重转换) 部分将模型由 `pdparams` 格式通过 PaddleX 转换为 `safetensors` 格式。
## 五、推理引擎 {#五推理引擎}
关于推理引擎的详细说明、取值、兼容性规则与示例请参见 <a href="../inference_deployment/local_inference/inference_engine.md">推理引擎与配置说明</a>。
### 5.1 速度数据
<table border="1">
<thead>
<tr>
<th>model</th>
<th>engine</th>
<th>Preprocessing (ms)</th>
<th>Inference (ms)</th>
<th>PostProcessing (ms)</th>
<th>End-to-End (ms)</th>
</tr>
</thead>
<tbody>
<tr>
<td rowspan="4">PP-OCRv5_mobile_det</td>
<td>paddle_static</td>
<td>11.43</td>
<td>13.80</td>
<td>2.15</td>
<td>27.58</td>
</tr>
<tr>
<td>paddle_dynamic</td>
<td>11.70</td>
<td>48.36</td>
<td>2.47</td>
<td>62.71</td>
</tr>
<tr>
<td>transformers</td>
<td>14.05</td>
<td>18.45</td>
<td>3.98</td>
<td>37.54</td>
</tr>
<tr>
<td>onnxruntime</td>
<td>9.98</td>
<td>5.70</td>
<td>2.04</td>
<td>17.90</td>
</tr>
<tr>
<td rowspan="4">PP-OCRv5_server_det</td>
<td>paddle_static</td>
<td>13.24</td>
<td>26.91</td>
<td>2.63</td>
<td>43.05</td>
</tr>
<tr>
<td>paddle_dynamic</td>
<td>11.82</td>
<td>45.56</td>
<td>2.52</td>
<td>60.10</td>
</tr>
<tr>
<td>transformers</td>
<td>14.56</td>
<td>13.76</td>
<td>7.44</td>
<td>36.76</td>
</tr>
<tr>
<td>onnxruntime</td>
<td>10.01</td>
<td>13.76</td>
<td>1.92</td>
<td>25.86</td>
</tr>
<tr>
<td rowspan="4">PP-OCRv6_medium_det</td>
<td>paddle_static</td>
<td>13.89</td>
<td>16.02</td>
<td>2.49</td>
<td>33.14</td>
</tr>
<tr>
<td>paddle_dynamic</td>
<td>11.42</td>
<td>26.23</td>
<td>2.30</td>
<td>40.10</td>
</tr>
<tr>
<td>transformers</td>
<td>11.40</td>
<td>8.57</td>
<td>8.35</td>
<td>29.57</td>
</tr>
<tr>
<td>onnxruntime</td>
<td>10.80</td>
<td>13.06</td>
<td>2.19</td>
<td>26.18</td>
</tr>
<tr>
<td rowspan="4">PP-OCRv6_small_det</td>
<td>paddle_static</td>
<td>10.91</td>
<td>10.97</td>
<td>2.41</td>
<td>24.45</td>
</tr>
<tr>
<td>paddle_dynamic</td>
<td>11.56</td>
<td>22.17</td>
<td>2.66</td>
<td>36.55</td>
</tr>
<tr>
<td>transformers</td>
<td>11.70</td>
<td>7.34</td>
<td>3.87</td>
<td>23.89</td>
</tr>
<tr>
<td>onnxruntime</td>
<td>11.32</td>
<td>7.46</td>
<td>2.54</td>
<td>21.49</td>
</tr>
<tr>
<td rowspan="4">PP-OCRv6_tiny_det</td>
<td>paddle_static</td>
<td>11.14</td>
<td>10.71</td>
<td>2.84</td>
<td>24.85</td>
</tr>
<tr>
<td>paddle_dynamic</td>
<td>11.52</td>
<td>21.70</td>
<td>2.94</td>
<td>36.31</td>
</tr>
<tr>
<td>transformers</td>
<td>10.90</td>
<td>6.99</td>
<td>4.13</td>
<td>23.00</td>
</tr>
<tr>
<td>onnxruntime</td>
<td>11.19</td>
<td>6.35</td>
<td>2.79</td>
<td>20.49</td>
</tr>
</tbody>
</table>
<strong>测试环境说明:</strong>
<ul>
<li><strong>测试数据:</strong><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/general_ocr_001.jpg">示例图片</a></li>
<li><strong>硬件配置:</strong>
<ul>
<li>GPU:NVIDIA A100 40G</li>
<li>CPU:Intel(R) Xeon(R) Gold 6248 CPU @ 2.50GHz</li>
</ul>
</li>
<li><strong>软件环境:</strong>
<ul>
<li>Ubuntu 22.04 / CUDA 12.6 / cuDNN 9.5</li>
<li>paddlepaddle-gpu 3.2.1 / paddleocr 3.5 / transformers 5.4.0 / torch 2.10 / onnxruntime-gpu 1.23.2</li>
</ul>
</li>
</ul>
### 5.2 权重转换 {#52-权重转换}
使用推理引擎时,系统会自动下载官方预训练模型。若需使用自训练模型配合 `paddle_dynamic` 或 `transformers` 引擎,请参考 [PaddleX 文本检测模块权重转换](https://paddlepaddle.github.io/PaddleX/latest/module_usage/tutorials/ocr_modules/text_detection.html#442) 部分,将 `pdparams` 格式通过 PaddleX 转换为 `safetensors` 格式,即可无缝集成到 PaddleOCR 的 API 中进行推理。若需使用自训练模型配合`onnxruntime`引擎,请参考[PaddleX 获取 ONNX 模型](https://paddlepaddle.github.io/PaddleX/latest/pipeline_deploy/paddle2onnx.html)获取onnx模型,即可无缝集成到 PaddleOCR 的 API 中进行推理。
## 六、常见问题与解决方案
### 6.1 性能优化问题
#### Q: GPU推理速度慢怎么办?
**A**: 可以通过以下方式优化:
(1)启用高性能推理:设置`enable_hpi=True`,自动选择最优加速策略
(2)启用TensorRT加速:设置`use_tensorrt=True`,需要CUDA 11.8+和TensorRT 8.6+
(3)使用半精度:设置`precision="fp16"`,可以显著提升速度
(4)调整批处理大小:根据显存大小设置合适的`batch_size`
(5)使用轻量模型:在精度要求不高时使用 `PP-OCRv6_small`/`PP-OCRv6_tiny` 等轻量级模型
#### Q: GPU内存不足(CUDA out of memory)怎么办?
**A**: 可以通过以下方式解决:
(1)减小批处理大小:将`batch_size`设置为1
(2)减小图像尺寸:设置`det_limit_side_len=640`
(3)启用内存优化:设置`enable_memory_optim=True`
(4)限制GPU内存使用:设置`gpu_mem=200`
(5)使用轻量模型:切换到 `PP-OCRv6_small`/`PP-OCRv6_tiny` 等轻量级模型
### 6.2 检测精度问题
#### Q: 检测框不准确或漏检怎么办?
**A**: 可以通过以下方式优化:
(1)调整检测参数:
```python
model = TextDetection(
thresh=0.3, # 降低像素阈值
box_thresh=0.5, # 降低检测框阈值
unclip_ratio=2.0, # 增大扩张系数
limit_side_len=1216 # 增大图像尺寸
)
```
(2)使用更精确的后处理模式:设置`det_db_score_mode="slow"`
(3)启用膨胀处理:设置`use_dilation=True`
### 6.3 模型选择建议
#### Q: 如何选择合适的模型?
**A**: 根据应用场景选择:
- 服务器高精度场景:使用 `PP-OCRv6_medium_det`,精度最高
- 移动端部署:使用 `PP-OCRv6_small_det`,兼顾精度与效率
- 端侧/IoT:使用 `PP-OCRv6_tiny_det`,模型最小
- 实时处理:使用 `PP-OCRv6_small_det` 或 `PP-OCRv6_tiny_det`,推理更快
### 6.4 参数调优建议
#### Q: 如何调优检测参数?
**A**: 通过参数`limit_type`和`limit_side_len`来对图片的尺寸进行限制,`limit_type`可选参数为[`max`, `min`],`limit_side_len` 为正整数,一般设置为 32 的倍数,比如 960。
如果输入图形分辨率不大,建议使用`limit_type=min` 和 `limit_side_len=960` 节省计算资源的同时能获得最佳检测效果。如果输入图片的分辨率比较大,而且想使用更大的分辨率预测,可以设置 `limit_side_len` 为想要的值,比如 1216。
#### Q: 不同场景的参数配置建议?
**A**:
- **高精度配置**:`limit_side_len=1216`, `thresh=0.3`, `box_thresh=0.5`, `unclip_ratio=1.5`
- **高速度配置**:`limit_side_len=640`, `thresh=0.5`, `box_thresh=0.7`, `unclip_ratio=1.2`
- **平衡配置**:`limit_side_len=960`, `thresh=0.4`, `box_thresh=0.6`, `unclip_ratio=1.5`
### 6.5 错误处理
#### Q: 模型加载失败怎么办?
**A**:
(1)检查模型路径是否正确
(2)确保模型文件完整(inference.pdmodel, inference.pdiparams, inference.json)
(3)设置模型下载源:`os.environ['PADDLE_PDX_MODEL_SOURCE'] = 'BOS'`
(4)使用本地模型:指定`model_dir`参数
#### Q: 输入数据格式错误怎么办?
**A**:
(1)检查图像文件是否存在
(2)确保图像格式正确(支持PNG、JPG、JPEG)
(3)验证图像尺寸(最小10x10像素)
(4)检查图像是否为3通道格式
|