paddle的版面分析的环境搭建及使用

时间:2024-03-12 07:50:47

一、什么是版面分析

版面分析技术,主要是对图片形式的文档进行版面分析,将文档划分为文字、标题、表格、图片以及列表5类区域,如下图所示:

二、应用场景

2.1 合同比对

2.2 文本类型划分

2.3 通用文档的还原

版面分析技术可将以word、pdf、图片等非结构化文档存储的信息还原为半结构化的电子数据,是非结构化文档信息利用首要任务。

三、paddle的版面分析 

3.1 快速体验 

 参考网址链接

https://github.com/PaddlePaddle/PaddleOCR/blob/release/2.7/ppstructure/docs/quickstart.md

环境搭建:

1)安装paddlepaddle

python3 -m pip install paddlepaddle-gpu -i https://mirror.baidu.com/pypi/simple【GPU版本】

python3 -m pip install paddlepaddle -i https://mirror.baidu.com/pypi/simple【CPU版本】

2) 安装 paddleocr,推荐使用2.6版本
pip3 install "paddleocr>=2.6.0.3"

# 安装 图像方向分类依赖包paddleclas(如不需要图像方向分类功能,可跳过)
pip3 install paddleclas>=2.4.3

 运行命令:

1)图像方向分类+版面分析+表格识别

paddleocr --image_dir=ppstructure/docs/table/1.png --type=structure --image_orientation=true

2) 版面分析+表格识别

paddleocr --image_dir=ppstructure/docs/table/1.png --type=structure

3)版面分析

paddleocr --image_dir=ppstructure/docs/table/1.png --type=structure --table=false --ocr=false

4)表格识别

paddleocr --image_dir=ppstructure/docs/table/table.jpg --type=structure --layout=false

5)版面恢复(作用将PDF的文档转换成通用(word)文档)

仅仅处理pdf文档

paddleocr --image_dir=ppstructure/recovery/UnrealText.pdf --type=structure --recovery=true --use_pdf2docx_api=true

3.2 源码程序实现

1) 安装PaddlePaddle

环境搭建

python3 -m pip install --upgrade pip

# GPU安装
python3 -m pip install "paddlepaddle-gpu>=2.3" -i https://mirror.baidu.com/pypi/simple

# CPU安装
python3 -m pip install "paddlepaddle>=2.3" -i https://mirror.baidu.com/pypi/simple

2)安装PaddleDetection

git clone https://github.com/PaddlePaddle/PaddleDetection.git

cd PaddleDetection
python3 -m pip install -r requirements.txt

需要自己训练数据集的,可以参考链接的攻略:

PaddleOCR/ppstructure/layout/README_ch.md at release/2.7 · PaddlePaddle/PaddleOCR · GitHub

预训练模型或者预测模型下载的官方网址为:

 PaddleOCR/ppstructure/docs/models_list.md at release/2.7 · PaddlePaddle/PaddleOCR · GitHub 

 

使用源码测试的命令,更换weights模型路径:

python3 tools/infer.py \
    -c configs/picodet/legacy_model/application/layout_analysis/picodet_lcnet_x1_0_layout.yml \
    --slim_config configs/picodet/legacy_model/application/layout_analysis/picodet_lcnet_x2_5_layout.yml \
    -o weights='output/picodet_lcnet_x2_5_layout/best_model.pdparams' \
    --infer_img='docs/images/layout.jpg' \
    --output_dir=output_dir/ \
    --draw_threshold=0.5

直接使用推理模型进行预测:

python3 deploy/python/infer.py \
    --model_dir=output_inference/picodet_lcnet_x1_0_layout/ \
    --image_file=docs/images/layout.jpg \
    --device=GPU

 备注:一些推理模型下载后,没有对应的yaml文件,会导致执行上面的指令出错,所以在运行上面的命令的时候,将下载的模型看看是否有对应的yaml文件。