版面分析指的是对图片形式的文档进行区域划分,定位其中的关键区域,如文字、标题、表格、图片等。
在上图中,最上面有图片区域,中间是标题和表格区域,下面是文字区域。
命令行使用
paddleocr --image_dir=ppstructure/docs/table/1.png --type=structure --table=false --ocr=false
Python代码使用
import os import cv2 from paddleocr import PPStructure,save_structure_res if __name__ == '__main__': table_engine = PPStructure(table=False, ocr=False, show_log=True) save_folder = './output' img_path = 'ppstructure/docs/table/1.png' img = cv2.imread(img_path) result = table_engine(img) save_structure_res(result, save_folder, os.path.basename(img_path).split('.')[0]) for line in result: img = line.pop('img') print(line) while True: cv2.imshow('img', img) key = cv2.waitKey() if key & 0xFF == ord('q'): break cv2.destroyAllWindows()
运行结果
{'type': 'text', 'bbox': [11, 729, 407, 847], 'res': '', 'img_idx': 0}
{'type': 'text', 'bbox': [442, 754, 837, 847], 'res': '', 'img_idx': 0}
{'type': 'title', 'bbox': [443, 705, 559, 719], 'res': '', 'img_idx': 0}
{'type': 'figure', 'bbox': [10, 1, 841, 294], 'res': '', 'img_idx': 0}
{'type': 'figure_caption', 'bbox': [70, 317, 707, 357], 'res': '', 'img_idx': 0}
{'type': 'figure_caption', 'bbox': [160, 317, 797, 335], 'res': '', 'img_idx': 0}
{'type': 'table', 'bbox': [453, 359, 822, 664], 'res': '', 'img_idx': 0}
{'type': 'table', 'bbox': [12, 360, 410, 716], 'res': '', 'img_idx': 0}
{'type': 'table_caption', 'bbox': [494, 343, 785, 356], 'res': '', 'img_idx': 0}
{'type': 'table_caption', 'bbox': [69, 318, 706, 357], 'res': '', 'img_idx': 0}
'text', 'bbox': [11, 729, 407, 847]
'text', 'bbox': [442, 754, 837, 847]
'title', 'bbox': [443, 705, 559, 719]
'figure', 'bbox': [10, 1, 841, 294]
'figure_caption', 'bbox': [70, 317, 707, 357]
'figure_caption', 'bbox': [160, 317, 797, 335]
'table', 'bbox': [453, 359, 822, 664]
'table', 'bbox': [12, 360, 410, 716]
'table_caption', 'bbox': [494, 343, 785, 356]
'table_caption', 'bbox': [69, 318, 706, 357]
从运行的结果来看,它是将原始图像拆成了图像、图像标题、表格、表格标题、文字和文字标题六个分类。
模型训练
下载PaddleDection框架代码
PaddleDetection: PaddleDetection的目的是为工业界和学术界提供丰富、易用的目标检测模型 (gitee.com)
下载,解压,进入PaddleDection主目录,安装需要的Python库
pip install -r .\requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
cocotools安装错误的话可以使用如下命令安装
git clone https://github.com/pdollar/coco.git
cd coco/PythonAPI
python setup.py build_ext --inplace
python setup.py build_ext install
|