README.md 10.0 KB
Newer Older
A
an1018 已提交
1 2
English | [简体中文](README_ch.md)

M
MissPenguin 已提交
3 4 5 6 7
# Layout Recovery

- [1. Introduction](#1)
- [2. Install](#2)
    - [2.1 Install PaddlePaddle](#2.1)
A
an1018 已提交
8
    - [2.2 Install PaddleOCR](#2.2)
A
an1018 已提交
9 10
- [3. Quick Start using standard PDF parse](#3)
- [4. Quick Start using image format PDF parse ](#4)
A
an1018 已提交
11 12 13
    - [4.1 Download models](#4.1)
    - [4.2 Layout recovery](#4.2)
- [5. More](#5)
A
an1018 已提交
14 15 16

<a name="1"></a>

A
an1018 已提交
17
## 1. Introduction
A
an1018 已提交
18

A
an1018 已提交
19 20
The layout recovery module is used to restore the image or pdf to an
editable Word file consistent with the original image layout.
A
an1018 已提交
21

A
an1018 已提交
22
Two layout recovery methods are provided, you can choose by PDF format:
A
an1018 已提交
23

A
an1018 已提交
24
- **Standard PDF parse(the input is standard PDF)**: Python based PDF to word library [pdf2docx] (https://github.com/dothinking/pdf2docx) is optimized, the method extracts data from PDF with PyMuPDF, then parse layout with rule, finally, generate docx with python-docx.
A
an1018 已提交
25

A
an1018 已提交
26
- **Image format PDF parse(the input can be standard PDF or image format PDF)**: Layout recovery combines [layout analysis](../layout/README.md)[table recognition](../table/README.md) to better recover images, tables, titles, etc. supports input files in PDF and document image formats in Chinese and English.
A
an1018 已提交
27 28 29 30 31

The input formats and application scenarios of the two methods are as follows:

|  method   | input formats |                      application scenarios/problem                       |
| :-----: | :----------: | :----------------------------------------------------------: |
A
an1018 已提交
32 33
| Standard PDF parse |     pdf      | Advantages: Better recovery for non-paper documents, each page remains on the same page after restoration<br>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<br>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 |
A
an1018 已提交
34 35 36 37 38 39 40 41

The following figure shows the effect of restoring the layout of documents by using PDF parse:

<div align="center">
<img src="https://user-images.githubusercontent.com/19808900/195319853-045123c9-f542-4596-b4e4-6081708dfc56.png"  width = "700" />
</div>

The following figures show the effect of restoring the layout of English and Chinese documents by using OCR technique:
A
an1018 已提交
42 43

<div align="center">
A
an1018 已提交
44
<img src="../docs/recovery/recovery.jpg"  width = "700" />
A
an1018 已提交
45
</div>
A
an1018 已提交
46

A
an1018 已提交
47 48 49
<div align="center">
<img src="../docs/recovery/recovery_ch.jpg"  width = "800" />
</div>
A
an1018 已提交
50 51


A
an1018 已提交
52 53
<a name="2"></a>

A
an1018 已提交
54 55 56 57
## 2. Install

<a name="2.1"></a>

M
MissPenguin 已提交
58
### 2.1 Install PaddlePaddle
A
an1018 已提交
59 60 61 62

```bash
python3 -m pip install --upgrade pip

A
an1018 已提交
63
# If you have cuda9 or cuda10 installed on your machine, please run the following command to install
A
an1018 已提交
64
python3 -m pip install "paddlepaddle-gpu" -i https://mirror.baidu.com/pypi/simple
A
an1018 已提交
65 66

# CPU installation
A
an1018 已提交
67
python3 -m pip install "paddlepaddle" -i https://mirror.baidu.com/pypi/simple
A
an1018 已提交
68 69
````

A
an1018 已提交
70
For more requirements, please refer to the instructions in [Installation Documentation](https://www.paddlepaddle.org.cn/en/install/quick?docurl=/documentation/docs/en/install/pip/macos-pip_en.html).
A
an1018 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

<a name="2.2"></a>

### 2.2 Install PaddleOCR

- **(1) Download source code**

```bash
[Recommended] git clone https://github.com/PaddlePaddle/PaddleOCR

# If the pull cannot be successful due to network problems, you can also choose to use the hosting on the code cloud:
git clone https://gitee.com/paddlepaddle/PaddleOCR

# Note: Code cloud hosting code may not be able to synchronize the update of this github project in real time, there is a delay of 3 to 5 days, please use the recommended method first.
````

A
an1018 已提交
87 88
- **(2) Install recovery `requirements`**

A
an1018 已提交
89
The layout restoration is exported as docx files, so python-docx API need to be installed, and PyMuPDF api([requires Python >= 3.7](https://pypi.org/project/PyMuPDF/)) need to be installed to process the input files in pdf format.
A
an1018 已提交
90

A
an1018 已提交
91
Install all the libraries by running the following command:
A
an1018 已提交
92

A
an1018 已提交
93 94 95 96
```bash
python3 -m pip install -r ppstructure/recovery/requirements.txt
````

A
an1018 已提交
97 98 99 100 101 102 103
 And if using pdf parse method, we need to install pdf2docx api.

```bash
wget https://paddleocr.bj.bcebos.com/whl/pdf2docx-0.0.0-py3-none-any.whl
pip3 install pdf2docx-0.0.0-py3-none-any.whl
```

A
an1018 已提交
104 105
<a name="3"></a>

A
an1018 已提交
106
## 3. Quick Start using standard PDF parse
A
an1018 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

`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](../docs/quickstart_en.md) for details.

```bash
# install paddleocr
pip3 install "paddleocr>=2.6"
paddleocr --image_dir=ppstructure/recovery/UnrealText.pdf --type=structure --recovery=true --use_pdf2docx_api=true
```

Command line:

```bash
python3 predict_system.py \
    --image_dir=ppstructure/recovery/UnrealText.pdf \
    --recovery=True \
    --use_pdf2docx_api=True \
    --output=../output/
```

<a name="4"></a>
A
an1018 已提交
127
## 4. Quick Start using image format PDF parse
A
an1018 已提交
128

A
an1018 已提交
129 130 131 132 133 134 135 136 137
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.

U
user1018 已提交
138
The whl package is also provided  for quick use, follow the above code, for more infomation please refer to [quickstart](../docs/quickstart_en.md) for details.
A
an1018 已提交
139

U
user1018 已提交
140 141 142
```bash
paddleocr --image_dir=ppstructure/docs/table/1.png --type=structure --recovery=true --lang='en'
```
A
an1018 已提交
143

A
an1018 已提交
144 145
<a name="4.1"></a>
### 4.1 Download models
A
an1018 已提交
146 147 148

If input is English document, download English models:

A
an1018 已提交
149
```bash
A
an1018 已提交
150 151 152 153 154
cd PaddleOCR/ppstructure

# download model
mkdir inference && cd inference
# Download the detection model of the ultra-lightweight English PP-OCRv3 model and unzip it
A
an1018 已提交
155
https://paddleocr.bj.bcebos.com/PP-OCRv3/english/en_PP-OCRv3_det_infer.tar && tar xf en_PP-OCRv3_det_infer.tar
A
an1018 已提交
156
# Download the recognition model of the ultra-lightweight English PP-OCRv3 model and unzip it
A
an1018 已提交
157
wget https://paddleocr.bj.bcebos.com/PP-OCRv3/english/en_PP-OCRv3_rec_infer.tar && tar xf en_PP-OCRv3_rec_infer.tar
A
an1018 已提交
158
# Download the ultra-lightweight English table inch model and unzip it
A
an1018 已提交
159 160
wget https://paddleocr.bj.bcebos.com/ppstructure/models/slanet/en_ppstructure_mobile_v2.0_SLANet_infer.tar
tar xf en_ppstructure_mobile_v2.0_SLANet_infer.tar
U
user1018 已提交
161
# Download the layout model of publaynet dataset and unzip it
A
an1018 已提交
162 163
wget https://paddleocr.bj.bcebos.com/ppstructure/models/layout/picodet_lcnet_x1_0_fgd_layout_infer.tar
tar xf picodet_lcnet_x1_0_fgd_layout_infer.tar
A
an1018 已提交
164
cd ..
A
an1018 已提交
165 166
```
If input is Chinese document,download Chinese models:
A
an1018 已提交
167
[Chinese and English ultra-lightweight PP-OCRv3 model](https://github.com/PaddlePaddle/PaddleOCR/blob/dygraph/README.md#pp-ocr-series-model-listupdate-on-september-8th)、[table recognition model](https://github.com/PaddlePaddle/PaddleOCR/blob/dygraph/ppstructure/docs/models_list.md#22-表格识别模型)、[layout analysis model](https://github.com/PaddlePaddle/PaddleOCR/blob/dygraph/ppstructure/docs/models_list.md#1-版面分析模型)
A
an1018 已提交
168

A
an1018 已提交
169 170
<a name="4.2"></a>
### 4.2 Layout recovery
A
an1018 已提交
171 172 173


```bash
U
user1018 已提交
174 175 176
python3 predict_system.py \
    --image_dir=./docs/table/1.png \
    --det_model_dir=inference/en_PP-OCRv3_det_infer \
A
an1018 已提交
177
    --rec_model_dir=inference/en_PP-OCRv3_rec_infer \
U
user1018 已提交
178
    --rec_char_dict_path=../ppocr/utils/en_dict.txt \
A
an1018 已提交
179
    --table_model_dir=inference/en_ppstructure_mobile_v2.0_SLANet_infer \
U
user1018 已提交
180
    --table_char_dict_path=../ppocr/utils/dict/table_structure_dict.txt \
A
an1018 已提交
181
    --layout_model_dir=inference/picodet_lcnet_x1_0_fgd_layout_infer \
U
user1018 已提交
182 183 184
    --layout_dict_path=../ppocr/utils/dict/layout_dict/layout_publaynet_dict.txt \
    --vis_font_path=../doc/fonts/simfang.ttf \
    --recovery=True \
A
an1018 已提交
185
    --output=../output/
A
an1018 已提交
186 187
```

A
an1018 已提交
188 189 190 191
After running, the docx of each picture will be saved in the directory specified by the output field

Field:

A
an1018 已提交
192
- image_dir:test file, can be picture, picture directory, pdf file, pdf file directory
A
an1018 已提交
193 194 195 196 197 198 199 200 201
- 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
A
an1018 已提交
202

A
an1018 已提交
203
<a name="5"></a>
A
an1018 已提交
204

A
an1018 已提交
205
## 5. More
A
an1018 已提交
206

A
an1018 已提交
207
For training, evaluation and inference tutorial for text detection models, please refer to [text detection doc](https://github.com/PaddlePaddle/PaddleOCR/blob/dygraph/doc/doc_en/detection_en.md).
A
an1018 已提交
208

A
an1018 已提交
209
For training, evaluation and inference tutorial for text recognition models, please refer to [text recognition doc](https://github.com/PaddlePaddle/PaddleOCR/blob/dygraph/doc/doc_en/recognition_en.md).
A
an1018 已提交
210

A
an1018 已提交
211
For training, evaluation and inference tutorial for layout analysis models, please refer to [layout analysis doc](https://github.com/PaddlePaddle/PaddleOCR/blob/dygraph/ppstructure/layout/README.md)
A
an1018 已提交
212

A
an1018 已提交
213
For training, evaluation and inference tutorial for table recognition models, please refer to [table recognition doc](https://github.com/PaddlePaddle/PaddleOCR/blob/dygraph/ppstructure/table/README.md)