from rapid_doc.backend.pipeline.pipeline_analyze import doc_analyze as pipeline_doc_analyze
def doc_analyze(
pdf_bytes_list,
lang_list,
parse_method: str = 'auto',
formula_enable=True,
table_enable=True,
layout_config=None,
ocr_config=None,
formula_config=None,
table_config=None,
checkbox_config=None,
)在mineru参数基础上新增了layout_config、ocr_config、formula_config、table_config、checkbox_config参数
# 用于指定推理设备。支持cpu/cuda/cuda:0/npu等设备类型
os.environ['MINERU_DEVICE_MODE'] = "cpu"
# 模型文件存储目录。如果不设置会默认下载到rapid_doc项目里面
os.environ['RAPID_MODELS_DIR'] = r'D:\CodeProjects\doc\RapidAI\models'
# 是否启用图片方向矫正,开启后,可以自动识别并矫正 90°、270°的图片
os.environ['USE_DOC_ORIENTATION_CLASSIFY'] = "true" # 在安装完 rapid_doc 之后,卸载 cpu 版的 onnxruntime
pip uninstall onnxruntime
# 这里一定要确定onnxruntime-gpu与GPU对应
# 可参见https://onnxruntime.ai/docs/execution-providers/CUDA-ExecutionProvider.html#requirements
pip install onnxruntime-gpu# 在 Python 中指定 GPU(必须在导入 rapid_doc 之前设置)
import os
# 使用默认 GPU(cuda:0)
os.environ['MINERU_DEVICE_MODE'] = "cuda"
# 或指定 GPU 编号,例如使用第二块 GPU(cuda:1)
os.environ['MINERU_DEVICE_MODE'] = "cuda:1"
# paddleocr-vl模型配置
os.environ['PADDLEOCRVL_VERSION'] = "v1.5"
os.environ['PADDLEOCRVL_VL_REC_BACKEND'] = "vllm-server"
os.environ['PADDLEOCRVL_VL_VL_REC_SERVER_URL'] = "http://localhost:8118/v1"| 参数名 | 说明 | 默认值 | 备注 |
|---|---|---|---|
| model_type | 模型 | PP_DOCLAYOUTV2 | |
| layout_shape_mode | 版面框形状 | PP_DOCLAYOUTV2 | "rect" / "auto" |
| conf_thresh | 阈值 | 0.5(_S为0.2) | |
| batch_num | 批处理大小 | 1 | |
| model_dir_or_path | 模型路径 | None | |
| markdown_ignore_labels | 忽略的版面类型 | ["number","footnote","header","header_image","footer","footer_image","aside_text",] | |
| 示例: |
from rapid_doc.model.layout.rapid_layout_self import ModelType as LayoutModelType
layout_config = {
"model_type": LayoutModelType.PP_DOCLAYOUT_L,
"conf_thresh": 0.4,
"batch_num": 1,
"model_dir_or_path": "C:\ocr\models\ppmodel\layout\PP-DocLayout-L\pp_doclayout_l.onnx"
}在rapidocr配置基础上新增如下参数
| 参数名 | 说明 | 默认值 | 备注 |
|---|---|---|---|
| custom_model | 自定义模型 | ||
| engine_type | det和rec的推理引擎 | OPENVINO(cpu)、TORCH(gpu) | |
| use_det_mode | 文本检测框模式:auto(默认)、txt、ocr | auto | 1、txt只会从pypdfium2获取文本框,保留pdf中的图片 2、ocr只会从OCR-det获取文本框 3、auto先从pypdfium2获取文本框,提取不到再使用OCR-det提取 |
| Det.rec_batch_num | rec批处理大小 | 1 |
示例:
from rapidocr import EngineType as OCREngineType, OCRVersion, ModelType
ocr_config = {
# "Det.model_path": r"C:\ocr\models\ppmodel\ocr\v4\ch_PP-OCRv4_det_infer\openvino\ch_PP-OCRv4_det_infer.onnx",
# "Rec.model_path": r"C:\ocr\models\ppmodel\ocr\v4\ch_PP-OCRv4_rec_infer\openvino\ch_PP-OCRv4_rec_infer.onnx",
# "Rec.rec_batch_num": 1,
# "Det.ocr_version": OCRVersion.PPOCRV5,
# "Rec.ocr_version": OCRVersion.PPOCRV5,
# "Det.model_type": OCRModelType.SERVER,
# "Rec.model_type": OCRModelType.SERVER,
# 新增的自定义参数
# "engine_type": OCREngineType.TORCH, # 统一设置推理引擎
# "Det.rec_batch_num": 1, # Det批处理大小
# "use_det_mode": 'auto' # 文本检测框模式:auto(默认)、txt、ocr
}| 参数名 | 说明 | 默认值 | 备注 |
|---|---|---|---|
| custom_model | 自定义模型 | ||
| model_type | 模型 | PP_FORMULANET_PLUS_M | |
| engine_type | 推理引擎 | ONNXRUNTIME(cpu)、TORCH(gpu) | torch仅支持PP_FORMULANET_PLUS_M模型 |
| formula_level | 公式识别等级 | 0 | 公式识别等级,默认为0,全识别。1:仅识别行间公式,行内公式不识别 |
| batch_num | 批处理大小 | 1 | |
| model_dir_or_path | 模型路径 | None | 不设置自动下载 |
| dict_keys_path | yml字典路径 | None | torch推理使用,不设置自动下载 |
| 示例: |
from rapid_doc.model.formula.rapid_formula_self import ModelType as FormulaModelType
formula_config = {
"model_type": FormulaModelType.PP_FORMULANET_PLUS_M,
"formula_level": 1, # 公式识别等级,默认为0,全识别。1:仅识别行间公式,行内公式不识别
"batch_num": 1,
"model_dir_or_path": r"C:\ocr\models\ppmodel\formula\PP-FormulaNet_plus-S\pp_formulanet_plus_s.onnx",
}| 参数名 | 说明 | 默认值 | 备注 |
|---|---|---|---|
| custom_model | 自定义模型 | ||
| force_ocr | 表格文字是否强制使用ocr | False | 根据 parse_method 来判断是否需要ocr还是从pdf中直接提取文本 |
| skip_text_in_image | 是否跳过表格里图片中的文字 | True | 如表格单元格中嵌入的图片、图标、扫描底图等,里面的文字 |
| use_img2table | 是否优先使用img2table库提取表格 | False | 需要手动安装(pip install img2table),基于opencv识别准确度不如使用模型,但是速度很快,默认关闭 |
| model_type | 模型 | UNET_SLANET_PLUS | 有线表格使用unet,无线表格使用slanet_plus |
| engine_type | 推理引擎 | OPENVINO(cpu)、ONNXRUNTIME(gpu) | None |
| use_word_box | 使用单字坐标匹配单元格 | True | |
| use_compare_table | 启用表格结果比较(同时跑有线/无线并比对) | False | 如果开启,分类判断为有线表格时,也会用无线表格模型识别,然后比较两个模型选择有效单元格识别结果多的 |
| table_formula_enable | 表格内公式识别 | True | |
| table_image_enable | 表格内图片识别 | True | |
| extract_original_image | 是否提取表格内原始图片 | False | |
| model_dir_or_path | 模型地址 | None | 单个模型使用。如SLANET_PLUS、UNITABLE |
| cls.model_type | 表格分类模型 | Q_CLS | |
| cls.model_dir_or_path | 表格分类模型地址 | None | |
| unet.model_dir_or_path | UNET表格模型地址 | None | |
| unitable.model_dir_or_path | UNITABLE表格模型地址 | None | |
| slanet_plus.model_dir_or_path | SLANET_PLUS表格模型地址 | None | |
| wired_cell.model_dir_or_path | 有线单元格模型地址 | None | 配置SLANEXT时使用 |
| wireless_cell.model_dir_or_path | 无线单元格模型地址 | None | 配置SLANEXT时使用 |
| wired_table.model_dir_or_path | 有线表结构模型地址 | None | 配置SLANEXT时使用 |
| wireless_table.model_dir_or_path | 无线表结构模型地址 | None | 配置SLANEXT时使用 |
| 示例: |
from rapid_doc.model.table.rapid_table_self import ModelType as TableModelType, EngineType as TableEngineType
table_config = {
# "force_ocr": False, # 表格文字,是否强制使用ocr,默认 False 根据 parse_method 来判断是否需要ocr还是从pdf中直接提取文本
# 注:文字版pdf可以使用pypdfium2提取到表格内图片,扫描版或图片需要使用PP_DOCLAYOUT_PLUS_L/PP_DOCLAYOUTV2版面识别模型,才能识别到表格内的图片
# "skip_text_in_image": True, # 是否跳过表格里图片中的文字(如表格单元格中嵌入的图片、图标、扫描底图等)
# "use_img2table": False, # 是否优先使用img2table库提取表格,需要手动安装(pip install img2table),基于opencv识别准确度不如使用模型,但是速度很快,默认关闭
# "model_type": TableModelType.SLANETPLUS,
# "model_type": TableModelType.UNET_SLANET_PLUS, # (默认) 有线表格使用unet,无线表格使用slanet_plus
# "model_type": TableModelType.UNET_UNITABLE, # 有线表格使用unet,无线表格使用unitable
# "model_type": TableModelType.UNITABLE,
# "model_dir_or_path": "", #单个模型使用。如SLANET_PLUS、UNITABLE
# "use_compare_table": False, # 启用表格结果比较(同时跑有线/无线并比对),默认 False
# "table_formula_enable": False, # 表格内公式识别
# "table_image_enable": False, # 表格内图片识别
# "extract_original_image": False # 是否提取表格内原始图片,默认 False
# "cls.model_type": TableModelType.Q_CLS, # 表格分类模型
# "cls.model_dir_or_path": "", # 表格分类模型地址
# "unet.model_dir_or_path": "", # UNET表格模型地址
# "unitable.model_dir_or_path": "", # UNITABLE表格模型地址
# "slanet_plus.model_dir_or_path": "", # SLANET_PLUS表格模型地址
# "engine_type": TableEngineType.ONNXRUNTIME, # 统一设置推理引擎
}| 参数名 | 说明 | 默认值 | 备注 |
|---|---|---|---|
| checkbox_enable | 是否识别复选框 | False | 基于opencv,有可能会误检 |
| 示例: |
checkbox_config = {
"checkbox_enable": True, # 是否识别复选框,默认不识别,基于opencv,有可能会误检
}(版面识别元素为图片的配置)
| 参数名 | 说明 | 默认值 | 备注 |
|---|---|---|---|
| extract_original_image | 是否提取原始图片 | False | 使用 pypdfium2 提取原始图片。截图可能导致清晰度降低和边界丢失,默认关闭 |
| extract_original_image_iou_thresh | 原始图片和版面识别的图片,bbox重叠度 | 0.9 | |
| 示例: |
from rapid_doc.backend.pipeline.model_json_to_middle_json import result_to_middle_json as pipeline_result_to_middle_json
image_config = {
"extract_original_image": True, # 是否提取原始图片(使用 pypdfium2 提取原始图片。截图可能导致清晰度降低和边界丢失,默认关闭)
"extract_original_image_iou_thresh": 0.5, # 原始图片和版面识别的图片,bbox重叠度,默认0.9
}
middle_json = pipeline_result_to_middle_json(model_list, images_list, pdf_dict, image_writer, _lang, _ocr_enable, p_formula_enable, ocr_config=ocr_config, image_config=image_config)