news 2026/9/10 1:52:14

PaddleOCR 3.x 快速上手指南:安装、命令行与 Python 推理实战

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
PaddleOCR 3.x 快速上手指南:安装、命令行与 Python 推理实战

PaddleOCR 3.x 快速上手指南:安装、命令行与 Python 推理实战

【免费下载链接】PaddleOCRTurn any PDF or image document into structured data for your AI. A powerful, lightweight OCR toolkit that bridges the gap between images/PDFs and LLMs. Supports 100+ languages.项目地址: https://gitcode.com/GitHub_Trending/pa/PaddleOCR

本文是 PaddleOCR 3.x 的官方快速入门指南(对应仓库 docs/quick_start.en.md),面向希望尽快把 OCR 能力跑起来的开发者。PaddleOCR 3.x 引入了统一推理引擎配置,可在 PaddlePaddle 与 Hugging Face Transformers 之间自由切换底层运行时;在此基础上,本文会带你完成环境安装、命令行推理与 Python 脚本调用三个环节,覆盖 PP-OCRv6 整图识别、文本检测、文本识别与 PP-StructureV3 文档结构化四条主线,并结合仓库源码说明engineuse_doc_orientation_classifyuse_doc_unwarpinguse_textline_orientation等关键参数在底层是如何生效的。读完本文,你将能够独立完成 PaddleOCR 3.x 的部署与二次开发。

一、理解 PaddleOCR 3.x 的统一推理引擎机制

PaddleOCR 3.x 在架构上最显著的变化是:推理引擎与业务 API 解耦。无论是完整 OCR 管线PaddleOCR、文档结构化管线PPStructureV3,还是单模型模块TextDetection/TextRecognition,都通过一个统一的engine参数选择底层运行时。

从源码看,该机制实现在 paddleocr/_common_args.py:

SUPPORTED_INFERENCE_ENGINE_LIST = [ "paddle", "paddle_static", "paddle_dynamic", "transformers", "onnxruntime", ]

即当前版本支持paddle(默认)、paddle_staticpaddle_dynamictransformersonnxruntime五种引擎。所有模型与管线的公共参数(deviceengineuse_tensorrtprecisionenable_mkldnncpu_threadsenable_cinn等)都会经过 parse_common_args 校验与归一化——例如引擎不在支持列表内会直接抛出ValueError。随后 prepare_common_init_args 会根据引擎类型与设备类型(CPU/GPU)自动组装engine_config:GPU 上可选择 Paddle Inference 的 TensorRT 子图(trt_fp32/trt_fp16),CPU 上则可开启 MKL-DNN 加速。

在命令行层面,CLI 入口 paddleocr/_cli.py 会注册全部管线与模型子命令;最终每个预测器的创建经由 paddleocr/_models/base.py 中的_create_paddlex_predictor调用 PaddleX 的create_predictor完成,因此你既可以通过--engine paddle使用 PaddlePaddle,也可以通过--engine transformers使用 Transformers。

二、环境安装

2.1 安装推理引擎

PaddleOCR 支持统一推理引擎配置,你可以按需选择底层运行时,目前支持 PaddlePaddle 与 Transformers。

选择一:PaddlePaddle

CPU 安装:

python -m pip install paddlepaddle==3.2.0 -i https://www.paddlepaddle.org.cn/packages/stable/cpu/

GPU 安装(以 Linux 平台 + CUDA 11.8 为例):

python -m pip install paddlepaddle-gpu==3.2.0 -i https://www.paddlepaddle.org.cn/packages/stable/cu118/

GPU 安装对 CUDA 版本有特定要求,其他平台请参照 PaddlePaddle 官方安装文档进行。注意:使用 PaddlePaddle 推理时,PaddleOCR 3.x 依赖 PaddlePaddle3.0及以上版本。

选择二:Transformers

使用 Transformers 引擎推理时,安装 Hugging Face Transformers:

python -m pip install "transformers>=5.8.0"

多数情况下,你还需要安装 Transformers 所依赖的底层推理框架,具体请参见 Transformers 官方安装文档。

2.2 安装paddleocr

安装 PaddleOCR 的全部功能:

python -m pip install "paddleocr[all]"

PaddleOCR 也支持按需安装特定功能(如仅装 OCR 或仅装文档解析相关依赖),详细说明见 PaddleOCR 安装文档。

提示:安装完成后可通过paddleocr -vpaddleocr --version查看版本信息,版本号来源于 paddleocr/_version.py 中的importlib.metadata读取。

三、命令行快速体验

paddleocr安装后会注册同名 CLI 命令,支持ocrtext_detectiontext_recognitionpp_structurev3等子命令(完整注册逻辑见 paddleocr/_cli.py)。以下示例均以仓库演示图(如general_ocr_002.png)为输入。

3.1 PP-OCRv6 整图 OCR

PaddlePaddle 引擎:

paddleocr ocr -i ./general_ocr_002.png \ --use_doc_orientation_classify False \ --use_doc_unwarping False \ --use_textline_orientation False \ --engine paddle

Transformers 引擎:

paddleocr ocr -i ./general_ocr_002.png \ --use_doc_orientation_classify False \ --use_doc_unwarping False \ --use_textline_orientation False \ --engine transformers

参数说明(均对应 paddleocr/_pipelines/ocr.py 中的 CLI 定义):

参数含义类型/默认行为
-i, --input输入图片路径或 URL必填
--engine推理引擎,取值为paddle/paddle_static/paddle_dynamic/transformers/onnxruntime默认由 PaddleX 自动选择
--use_doc_orientation_classify是否启用文档方向分类(0°/90°/180°/270°)布尔值,True/False
--use_doc_unwarping是否启用文档图像矫正(去弯曲)布尔值,True/False
--use_textline_orientation是否启用文本行方向分类(解决单行文字倒置问题)布尔值,True/False
--device推理设备,如cpugpugpu:0默认 GPU 0 可用时用 GPU,否则 CPU
--save_path结果保存目录可选

从源码看,use_doc_orientation_classifyuse_doc_unwarping二者任一为True时,use_doc_preprocessor会被自动置为True(见 paddleocr/_pipelines/ocr.py),即进入"文档预处理"子管线。

3.2 PP-OCRv6 文本检测模块

PaddlePaddle 引擎:

paddleocr text_detection -i ./general_ocr_001.png --engine paddle

Transformers 引擎:

paddleocr text_detection -i ./general_ocr_001.png --engine transformers

该子命令默认加载PP-OCRv6_medium_det检测模型(见 paddleocr/_models/text_detection.py),并额外支持--limit_side_len--limit_type--thresh--box_thresh--unclip_ratio--input_shape等检测参数(定义见 paddleocr/_models/_text_detection.py)。

3.3 PP-OCRv6 文本识别模块

PaddlePaddle 引擎:

paddleocr text_recognition -i ./general_ocr_rec_001.png --engine paddle

Transformers 引擎:

paddleocr text_recognition -i ./general_ocr_rec_001.png --engine transformers

该子命令默认加载PP-OCRv6_medium_rec识别模型(见 paddleocr/_models/text_recognition.py)。

3.4 PP-StructureV3 文档结构化

PaddlePaddle 引擎:

paddleocr pp_structurev3 -i ./pp_structure_v3_demo.png \ --use_doc_orientation_classify False \ --use_doc_unwarping False \ --engine paddle

Transformers 引擎(当前部分模型仍在适配中,必须关闭公式识别,并替换无线表格结构识别模型):

paddleocr pp_structurev3 -i ./pp_structure_v3_demo.png \ --engine transformers \ --use_formula_recognition False \ --wireless_table_structure_recognition_model_name SLANeXt_wireless

pp_structurev3子命令的参数极为丰富,除了上表列出的通用参数外,还包括布局检测(--layout_threshold--layout_nms--layout_unclip_ratio)、表格识别(--wired_table_structure_recognition_model_name--wireless_table_structure_recognition_model_name)、印章识别(--use_seal_recognition--seal_det_thresh等)、图表识别(--use_chart_recognition)、公式识别(--use_formula_recognition)、区域检测(--use_region_detection)等开关与模型配置,完整定义见 paddleocr/_pipelines/pp_structurev3.py。

四、Python 脚本使用

CLI 之外,PaddleOCR 3.x 提供面向对象 Python API。所有核心类均从 paddleocr/init.py 导出,包括PaddleOCRPPStructureV3TextDetectionTextRecognition等。每个预测对象都遵循统一的predict模式:返回结果列表,每个结果支持print()打印、save_to_img()保存可视化图、save_to_json()保存 JSON。

4.1 PP-OCRv6 整图 OCR

PaddlePaddle 引擎:

from paddleocr import PaddleOCR ocr = PaddleOCR( use_doc_orientation_classify=False, use_doc_unwarping=False, use_textline_orientation=False, engine="paddle", ) result = ocr.predict("./general_ocr_002.png") for res in result: res.print() res.save_to_img("output") res.save_to_json("output")

Transformers 引擎:

from paddleocr import PaddleOCR ocr = PaddleOCR( use_doc_orientation_classify=False, use_doc_unwarping=False, use_textline_orientation=False, engine="transformers", ) result = ocr.predict("./general_ocr_002.png") for res in result: res.print() res.save_to_img("output") res.save_to_json("output")

示例输出(节选):

{'res': {'input_path': './general_ocr_002.png', 'page_index': None, 'model_settings': {'use_doc_preprocessor': True, 'use_textline_orientation': False}, 'doc_preprocessor_res': {'input_path': None, 'page_index': None, 'model_settings': {'use_doc_orientation_classify': False, 'use_doc_unwarping': False}, 'angle': -1}, 'dt_polys': array([[[ 1, 4], ..., [ 1, 33]], ..., [[ 99, 455], ..., [ 99, 480]]], dtype=int16), 'text_det_params': {'limit_side_len': 960, 'limit_type': 'max', 'thresh': 0.3, 'max_side_limit': 4000, 'box_thresh': 0.6, 'unclip_ratio': 1.5}, 'text_type': 'general', 'textline_orientation_angles': array([-1, ..., -1]), 'text_rec_score_thresh': 0.0, 'rec_texts': ['www.997788.com', '登机牌', 'BOARDING PASS', '舱位CLASS', '序号 SERIAL NO.', '座位号', 'SEAT NO', '航班FLIGHT', '日期', 'DATE', 'MU 2379', '03DEC', 'W', '035', '', '始发地', 'FROM', '登机口', 'GATE', '登机时间BDT', '目的地TO', '福州', 'TAIYUAN', 'G11', 'FUZHOU', '身份识别IDNO.', '姓名NAME', 'ZHANGQIWEI', '票号TKTNO.', '张祺伟', '票价FARE', 'ETKT7813699238489/1', '登机口于起飞前10分钟关闭 GATESCL0SE10MINUTESBEFOREDEPARTURETIME'], 'rec_scores': array([0.99684608, ..., 0.97179604]), 'rec_polys': array([[[ 1, 4], ..., [ 1, 33]], ..., [[ 99, 455], ..., [ 99, 480]]], dtype=int16), 'rec_boxes': array([[ 1, ..., 33], ..., [ 99, ..., 480]], dtype=int16)}}

结果字段解读:

字段含义
doc_preprocessor_res文档预处理子结果,angle为方向分类输出的旋转角度(-1 表示无旋转)
dt_polys文本检测多边形坐标(N×4×2,int16)
text_det_params检测阶段的生效参数快照(limit_side_lenthreshbox_threshunclip_ratio等)
rec_texts识别出的文本行列表
rec_scores每行文本的置信度
rec_polys/rec_boxes每行文本的多边形/矩形框坐标

4.2 PP-OCRv6 文本检测模块

PaddlePaddle 引擎:

from paddleocr import TextDetection model = TextDetection(engine="paddle") output = model.predict("general_ocr_001.png") for res in output: res.print() res.save_to_img(save_path="./output/") res.save_to_json(save_path="./output/res.json")

Transformers 引擎:

from paddleocr import TextDetection model = TextDetection(engine="transformers") output = model.predict("general_ocr_001.png") for res in output: res.print() res.save_to_img(save_path="./output/") res.save_to_json(save_path="./output/res.json")

示例输出:

{'res': {'input_path': 'general_ocr_001.png', 'page_index': None, 'dt_polys': array([[[ 77, 551], ..., [ 78, 587]], ..., [[ 34, 408], ..., [ 36, 456]]], dtype=int16), 'dt_scores': [0.8562385635646694, 0.8818259002228059, 0.8406072284043453, 0.8855339313157491]}}

检测结果包含dt_polys(文本框多边形)与dt_scores(各框置信度)。

4.3 PP-OCRv6 文本识别模块

PaddlePaddle 引擎:

from paddleocr import TextRecognition model = TextRecognition(engine="paddle") output = model.predict(input="general_ocr_rec_001.png") for res in output: res.print() res.save_to_img(save_path="./output/") res.save_to_json(save_path="./output/res.json")

Transformers 引擎:

from paddleocr import TextRecognition model = TextRecognition(engine="transformers") output = model.predict(input="general_ocr_rec_001.png") for res in output: res.print() res.save_to_img(save_path="./output/") res.save_to_json(save_path="./output/res.json")

示例输出:

{'res': {'input_path': 'general_ocr_rec_001.png', 'page_index': None, 'rec_text': '绿洲仕格维花园公寓', 'rec_score': 0.990813672542572}}

4.4 PP-StructureV3 文档结构化

PaddlePaddle 引擎:

from paddleocr import PPStructureV3 pipeline = PPStructureV3( use_doc_orientation_classify=False, use_doc_unwarping=False, engine="paddle", ) output = pipeline.predict( input="./pp_structure_v3_demo.png") for res in output: res.print() res.save_to_json(save_path="output") res.save_to_markdown(save_path="output")

Transformers 引擎(当前部分模型仍在适配中,需关闭公式识别并替换无线表格结构识别模型):

from paddleocr import PPStructureV3 pipeline = PPStructureV3( use_doc_orientation_classify=False, use_doc_unwarping=False, use_formula_recognition=False, wireless_table_structure_recognition_model_name="SLANeXt_wireless", engine="transformers", ) output = pipeline.predict(input="./pp_structure_v3_demo.png") for res in output: res.print() res.save_to_json(save_path="output") res.save_to_markdown(save_path="output")

PaddlePaddle 推理示例输出(节选):

{'res': {'input_path': './pp_structure_v3_demo.png', 'page_index': None, 'model_settings': {'use_doc_preprocessor': False, 'use_seal_recognition': True, 'use_table_recognition': True, 'use_formula_recognition': True, 'use_chart_recognition': False, 'use_region_detection': True}, 'layout_det_res': {'input_path': None, 'page_index': None, 'boxes': [ {'cls_id': 1, 'label': 'image', 'score': 0.9864752888679504, 'coordinate': [774.821, 201.05177, 1502.1008, 685.7733]}, {'cls_id': 2, 'label': 'text', 'score': 0.9859225749969482, 'coordinate': [769.8655, 776.2446, 1121.5986, 1058.417]}, ... {'cls_id': 0, 'label': 'paragraph_title', 'score': 0.9476125240325928, 'coordinate': [28.159409, 456.7627, 339.5631, 514.9665]}, {'cls_id': 10, 'label': 'doc_title', 'score': 0.9376171827316284, 'coordinate': [133.77905, 36.8844, 1379.6667, 123.46869]}, ... ]}, 'overall_ocr_res': {'input_path': None, 'page_index': None, 'model_settings': {'use_doc_preprocessor': False, 'use_textline_orientation': False}, 'dt_polys': array([[[ 129, 42], ..., [ 129, 140]], ..., [[1156, 1330], ..., [1156, 1351]]], dtype=int16), 'text_det_params': {'limit_side_len': 736, 'limit_type': 'min', 'thresh': 0.3, 'max_side_limit': 4000, 'box_thresh': 0.6, 'unclip_ratio': 1.5}, 'text_type': 'general', 'textline_orientation_angles': array([-1, ..., -1]), 'text_rec_score_thresh': 0.0, 'rec_texts': ['助力双方交往', '搭建友谊桥梁', ...], 'rec_scores': array([0.99113536, ..., 0.95110035]), 'rec_polys': array([[[ 129, 42], ..., [ 129, 140]], ..., [[1156, 1330], ..., [1156, 1351]]], dtype=int16), 'rec_boxes': array([[ 129, ..., 140], ..., [1156, ..., 1351]], dtype=int16)}}}

PP-StructureV3 的结果分两层:layout_det_res输出版面分析结果(cls_id对应paragraph_titletextimagefigure_titledoc_title等区域类别,coordinate为 [x1, y1, x2, y2] 坐标),overall_ocr_res输出整页 OCR 结果。此外,相比 OCR 管线,它还额外支持save_to_markdown(),可直接产出结构化 Markdown,便于喂给 LLM 或知识库。

五、深入源码:模型选择与关键参数

5.1 默认模型与语言选择

PaddleOCR在未显式指定检测/识别模型时,会根据langocr_version自动选择默认模型(见 paddleocr/_pipelines/ocr.py):

  • 不指定任何参数时,默认使用PP-OCRv6 中量级模型对PP-OCRv6_medium_det+PP-OCRv6_medium_rec
  • 支持ocr_versionPP-OCRv3/PP-OCRv4/PP-OCRv5/PP-OCRv6,版本与语言不匹配(如某语言尚无对应版本模型)时会抛出明确错误;
  • 支持 100+ 语言的lang参数:PP-OCRv6 原生覆盖chchinese_chtenjapan及全部拉丁语系;其余语系(韩语、泰语、希腊语、西里尔、阿拉伯、天城文等)会自动回退到 PP-OCRv5 / PP-OCRv3 模型;
  • 一旦显式传入text_detection_model_name/text_recognition_model_name(或对应model_dir),langocr_version会被忽略并给出警告。

单模型模块同理:TextDetection默认PP-OCRv6_medium_detTextRecognition默认PP-OCRv6_medium_rec

5.2 旧版参数兼容

为兼容 PaddleOCR 2.x 的调用习惯,PaddleOCR内置了废弃参数映射表(见 paddleocr/_pipelines/ocr.py),如det_model_dirtext_detection_model_dirrec_model_dirtext_recognition_model_diruse_angle_clsuse_textline_orientation等。传入旧参数会触发弃用警告并自动映射,但新旧参数同时传入会报错(二者互斥)。

5.3 公共推理参数

所有模型与管线均支持以下公共参数(定义见 paddleocr/_common_args.py),可用于性能与精度调优:

参数说明默认值
--devicecpu/gpu/npu/gpu:0有 GPU 用 GPU 0,否则 CPU
--use_tensorrt是否启用 Paddle Inference 的 TensorRT 子图加速视模型支持情况
--precisionTensorRT 精度(fp32/fp16默认值见 paddleocr/_constants.py
--enable_mkldnnCPU 上启用 MKL-DNN 加速默认关闭
--cpu_threadsCPU 推理线程数默认值见 paddleocr/_constants.py
--enable_cinn是否启用 CINN 编译器默认关闭
--enable_hpi是否启用高性能推理(HPI)默认关闭

CLI 中的布尔参数(如--use_doc_orientation_classify)经由 paddleocr/_utils/cli.py 的str2bool解析,接受true/yes/t/y/1等取值。

六、常见注意事项

  1. 引擎依赖不可混用--engine transformers要求已安装transformers>=5.8.0及其底层推理框架;--engine paddle要求 PaddlePaddle ≥ 3.0。引擎缺失时,预测器创建会因依赖检查失败而报错(见 paddleocr/_models/base.py 的错误处理逻辑)。
  2. PP-StructureV3 的 Transformers 适配尚未完全:使用engine="transformers"时,需关闭公式识别(use_formula_recognition=False)并将无线表格结构识别模型替换为SLANeXt_wireless,否则可能因模型未适配而失败。
  3. 文档预处理是独立子管线use_doc_orientation_classifyuse_doc_unwarping任一开启即会触发use_doc_preprocessor=True,增加前处理耗时;对方向正常的单页图片,官方示例通常显式关闭以追求速度。
  4. 结果持久化:单模型结果支持save_to_img/save_to_json;PP-StructureV3 额外支持save_to_markdown,适合直接作为 RAG/LLM 输入。

至此,你已经掌握了 PaddleOCR 3.x 的安装、CLI 与 Python API 用法,以及关键参数的底层行为。更完整的模型清单、按需安装与性能调优指南,可继续阅读 PaddleOCR 安装文档 与仓库中的 模型列表 等资料。

【免费下载链接】PaddleOCRTurn any PDF or image document into structured data for your AI. A powerful, lightweight OCR toolkit that bridges the gap between images/PDFs and LLMs. Supports 100+ languages.项目地址: https://gitcode.com/GitHub_Trending/pa/PaddleOCR

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/9/10 1:51:52

需求侧响应下配电网供电能力综合评估的Matlab复现与工程实践

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

作者头像 李华
网站建设 2026/9/10 1:51:30

聆听艺术是什么?

开篇语:随着国内美育政策持续落地,家庭对于儿童艺术素养培育的重视程度不断提升,少儿声乐培训赛道迎来持续扩容。根据行业调研数据显示,国内少儿艺术教育整体市场规模保持稳步增长态势,少儿声乐作为美育细分赛道&#…

作者头像 李华
网站建设 2026/9/10 1:47:33

IFA 2026:Potensic重磅押注Atom 3无人机

轻便型无人机持续主导消费市场,而Potensic正借助IFA 2026向外界展示,为何其最新机型值得列入买家的候选清单。这家无人机制造商连续第三年参展这场年度柏林科技展会,将聚焦点集中放在了Atom 3航拍无人机上,展示其面向旅行者、户外…

作者头像 李华