Layout Recovery¶
1. Introduction¶
The layout recovery module is used to restore the image or pdf to an editable Word file consistent with the original image layout.
Two layout recovery methods are provided, you can choose by PDF format:
-
Standard PDF parse(the input is standard PDF): Python based PDF to word library pdf2docx is optimized, the method extracts data from PDF with PyMuPDF, then parse layout with rule, finally, generate docx with python-docx.
-
Image format PDF parse(the input can be standard PDF or image format PDF): Layout recovery combines layout analysis、table recognition to better recover images, tables, titles, etc. supports input files in PDF and document image formats in Chinese and English.
The input formats and application scenarios of the two methods are as follows:
method | input formats | application scenarios/problem |
---|---|---|
Standard PDF parse | Advantages: Better recovery for non-paper documents, each page remains on the same page after restoration Disadvantages: English characters in some Chinese documents are garbled, some contents are still beyond the current page, the whole page content is restored to the table format, and the recovery effect of some pictures is not good |
|
Image format PDF parse( | pdf、picture | Advantages: More suitable for paper document content recovery, OCR recognition effect is more good Disadvantages: Currently, the recovery is based on rules, the effect of content typesetting (spacing, fonts, etc.) need to be further improved, and the effect of layout recovery depends on layout analysis |
The following figure shows the effect of restoring the layout of documents by using PDF parse:
The following figures show the effect of restoring the layout of English and Chinese documents by using OCR technique:
2. Install¶
2.1 Install PaddlePaddle¶
3. Quick Start using standard PDF parse¶
use_pdf2docx_api
use PDF parse for layout recovery, The whl package is also provided for quick use, follow the above code, for more infomation please refer to quickstart for details.
Command line:
4. Quick Start using image format PDF parse¶
Through layout analysis, we divided the image/PDF documents into regions, located the key regions, such as text, table, picture, etc., and recorded the location, category, and regional pixel value information of each region. Different regions are processed separately, where:
-
OCR detection and recognition is performed in the text area, and the coordinates of the OCR detection box and the text content information are added on the basis of the previous information
-
The table area identifies tables and records html and text information of tables
- Save the image directly
We can restore the test picture through the layout information, OCR detection and recognition structure, table information, and saved pictures.
The whl package is also provided for quick use, follow the above code, for more infomation please refer to quickstart for details.
4.1 Download models¶
If input is English document, download English models:
If input is Chinese document,download Chinese models: Chinese and English ultra-lightweight PP-OCRv3 model
4.2 Layout recovery¶
After running, the docx of each picture will be saved in the directory specified by the output field
Field:
- image_dir:test file, can be picture, picture directory, pdf file, pdf file directory
- det_model_dir:OCR detection model path
- rec_model_dir:OCR recognition model path
- rec_char_dict_path:OCR recognition dict path. If the Chinese model is used, change to "../ppocr/utils/ppocr_keys_v1.txt". And if you trained the model on your own dataset, change to the trained dictionary
- table_model_dir:tabel recognition model path
- table_char_dict_path:tabel recognition dict path. If the Chinese model is used, no need to change
- layout_model_dir:layout analysis model path
- layout_dict_path:layout analysis dict path. If the Chinese model is used, change to "../ppocr/utils/dict/layout_dict/layout_cdla_dict.txt"
- recovery:whether to enable layout of recovery, default False
- output:save the recovery result path
5. More¶
For training, evaluation and inference tutorial for text detection models, please refer to text detection doc.
For training, evaluation and inference tutorial for text recognition models, please refer to text recognition doc.
For training, evaluation and inference tutorial for layout analysis models, please refer to layout analysis doc
For training, evaluation and inference tutorial for table recognition models, please refer to table recognition doc