PaddleOCR/ppstructure/recovery/README.md

214 lines
10 KiB
Markdown
Raw Normal View History

2022-05-07 16:55:20 +08:00
English | [简体中文](README_ch.md)
2022-08-26 11:02:57 +08:00
# Layout Recovery
- [1. Introduction](#1)
- [2. Install](#2)
- [2.1 Install PaddlePaddle](#2.1)
2022-05-09 16:17:40 +08:00
- [2.2 Install PaddleOCR](#2.2)
2022-10-17 10:38:12 +08:00
- [3. Quick Start using standard PDF parse](#3)
- [4. Quick Start using image format PDF parse ](#4)
2022-10-12 21:28:48 +08:00
- [4.1 Download models](#4.1)
- [4.2 Layout recovery](#4.2)
- [5. More](#5)
2022-05-07 16:55:20 +08:00
<a name="1"></a>
2022-08-22 11:48:18 +08:00
## 1. Introduction
2022-05-07 16:55:20 +08:00
2022-10-12 21:28:48 +08:00
The layout recovery module is used to restore the image or pdf to an
editable Word file consistent with the original image layout.
2022-05-07 16:55:20 +08:00
2022-10-17 10:38:12 +08:00
Two layout recovery methods are provided, you can choose by PDF format:
2022-10-12 21:28:48 +08:00
2022-10-17 10:38:12 +08:00
- **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.
2022-10-12 21:28:48 +08:00
2022-10-17 10:38:12 +08:00
- **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.
2022-10-12 21:28:48 +08:00
The input formats and application scenarios of the two methods are as follows:
| method | input formats | application scenarios/problem |
| :-----: | :----------: | :----------------------------------------------------------: |
2022-10-17 10:38:12 +08:00
| 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 |
2022-10-12 21:28:48 +08:00
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:
2022-05-07 16:55:20 +08:00
<div align="center">
2022-08-22 16:41:42 +08:00
<img src="../docs/recovery/recovery.jpg" width = "700" />
2022-05-07 16:55:20 +08:00
</div>
2022-08-22 16:41:42 +08:00
2022-08-23 23:28:49 +08:00
<div align="center">
<img src="../docs/recovery/recovery_ch.jpg" width = "800" />
</div>
2022-10-12 21:28:48 +08:00
2022-05-07 16:55:20 +08:00
<a name="2"></a>
2022-05-09 16:17:40 +08:00
## 2. Install
<a name="2.1"></a>
2022-08-26 11:02:57 +08:00
### 2.1 Install PaddlePaddle
2022-05-09 16:17:40 +08:00
```bash
python3 -m pip install --upgrade pip
2022-08-23 23:28:49 +08:00
# If you have cuda9 or cuda10 installed on your machine, please run the following command to install
2022-08-22 11:48:18 +08:00
python3 -m pip install "paddlepaddle-gpu" -i https://mirror.baidu.com/pypi/simple
2022-05-09 16:17:40 +08:00
# CPU installation
2022-08-22 11:48:18 +08:00
python3 -m pip install "paddlepaddle" -i https://mirror.baidu.com/pypi/simple
2022-05-09 16:17:40 +08:00
````
2022-08-22 11:48:18 +08:00
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).
2022-05-09 16:17:40 +08:00
<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.
````
2022-10-12 21:28:48 +08:00
- **(2) Install recovery `requirements`**
2022-10-14 18:45:39 +08:00
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.
2022-05-09 16:17:40 +08:00
2022-10-12 21:28:48 +08:00
Install all the libraries by running the following command:
2022-08-23 23:28:49 +08:00
2022-05-09 16:17:40 +08:00
```bash
python3 -m pip install -r ppstructure/recovery/requirements.txt
````
2022-10-14 18:45:39 +08:00
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
```
2022-05-09 16:17:40 +08:00
<a name="3"></a>
2022-10-17 10:38:12 +08:00
## 3. Quick Start using standard PDF parse
2022-10-12 21:28:48 +08:00
`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>
2022-10-17 10:38:12 +08:00
## 4. Quick Start using image format PDF parse
2022-05-07 16:55:20 +08:00
2022-08-23 23:28:49 +08:00
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](../docs/quickstart_en.md) for details.
2022-08-25 14:44:54 +08:00
```bash
paddleocr --image_dir=ppstructure/docs/table/1.png --type=structure --recovery=true --lang='en'
```
2022-08-23 23:28:49 +08:00
2022-10-12 21:28:48 +08:00
<a name="4.1"></a>
### 4.1 Download models
2022-08-22 11:48:18 +08:00
If input is English document, download English models:
2022-08-22 16:41:42 +08:00
```bash
2022-05-07 16:55:20 +08:00
cd PaddleOCR/ppstructure
# download model
mkdir inference && cd inference
# Download the detection model of the ultra-lightweight English PP-OCRv3 model and unzip it
[Cherry-pick] Cherry-pick from release/2.6 (#11092) * Update recognition_en.md (#10059) ic15_dict.txt only have 36 digits * Update ocr_rec.h (#9469) It is enough to include preprocess_op.h, we do not need to include ocr_cls.h. * 补充num_classes注释说明 (#10073) ser_vi_layoutxlm_xfund_zh.yml中的Architecture.Backbone.num_classes所赋值会设置给Loss.num_classes, 由于采用BIO标注,假设字典中包含n个字段(包含other)时,则类别数为2n-1;假设字典中包含n个字段(不含other)时,则类别数为2n+1。 * Update algorithm_overview_en.md (#9747) Fix links to super-resolution algorithm docs * 改进文档`deploy/hubserving/readme.md`和`doc/doc_ch/models_list.md` (#9110) * Update readme.md * Update readme.md * Update readme.md * Update models_list.md * trim trailling spaces @ `deploy/hubserving/readme_en.md` * `s/shell/bash/` @ `deploy/hubserving/readme_en.md` * Update `deploy/hubserving/readme_en.md` to sync with `deploy/hubserving/readme.md` * Update deploy/hubserving/readme_en.md to sync with `deploy/hubserving/readme.md` * Update deploy/hubserving/readme_en.md to sync with `deploy/hubserving/readme.md` * Update `doc/doc_en/models_list_en.md` to sync with `doc/doc_ch/models_list_en.md` * using Grammarly to weak `deploy/hubserving/readme_en.md` * using Grammarly to tweak `doc/doc_en/models_list_en.md` * `ocr_system` module will return with values of field `confidence` * Update README_CN.md * 修复测试服务中图片转Base64的引用地址错误。 (#8334) * Update application.md * [Doc] Fix 404 link. (#10318) * Update PP-OCRv3_det_train.md * Update knowledge_distillation.md * Update config.md * Fix fitz camelCase deprecation and .PDF not being recognized as pdf file (#10181) * Fix fitz camelCase deprecation and .PDF not being recognized as pdf file * refactor get_image_file_list function * Update customize.md (#10325) * Update FAQ.md (#10345) * Update FAQ.md (#10349) * Don't break overall processing on a bad image (#10216) * Add preprocessing common to OCR tasks (#10217) Add preprocessing to options * [MLU] add mlu device for infer (#10249) * Create newfeature.md * Update newfeature.md * remove unused imported module, so can avoid PyInstaller packaged binary's start-time not found module error. (#10502) * CV套件建设专项活动 - 文字识别返回单字识别坐标 (#10515) * modification of return word box * update_implements * Update rec_postprocess.py * Update utility.py * Update README_ch.md * revert README_ch.md update * Fixed Layout recovery README file (#10493) Co-authored-by: Shubham Chambhare <shubhamchambhare@zoop.one> * update_doc * bugfix --------- Co-authored-by: ChuongLoc <89434232+ChuongLoc@users.noreply.github.com> Co-authored-by: Wang Xin <xinwang614@gmail.com> Co-authored-by: tanjh <dtdhinjapan@gmail.com> Co-authored-by: Louis Maddox <lmmx@users.noreply.github.com> Co-authored-by: n0099 <n@n0099.net> Co-authored-by: zhenliang li <37922155+shouyong@users.noreply.github.com> Co-authored-by: itasli <ilyas.tasli@outlook.fr> Co-authored-by: UserUnknownFactor <63057995+UserUnknownFactor@users.noreply.github.com> Co-authored-by: PeiyuLau <135964669+PeiyuLau@users.noreply.github.com> Co-authored-by: kerneltravel <kjpioo2006@gmail.com> Co-authored-by: ToddBear <43341135+ToddBear@users.noreply.github.com> Co-authored-by: Ligoml <39876205+Ligoml@users.noreply.github.com> Co-authored-by: Shubham Chambhare <59397280+Shubham654@users.noreply.github.com> Co-authored-by: Shubham Chambhare <shubhamchambhare@zoop.one> Co-authored-by: andyj <87074272+andyjpaddle@users.noreply.github.com>
2023-10-18 17:37:23 +08:00
wget https://paddleocr.bj.bcebos.com/PP-OCRv3/english/en_PP-OCRv3_det_infer.tar && tar xf en_PP-OCRv3_det_infer.tar
2022-05-07 16:55:20 +08:00
# Download the recognition model of the ultra-lightweight English PP-OCRv3 model and unzip it
2022-08-22 11:48:18 +08:00
wget https://paddleocr.bj.bcebos.com/PP-OCRv3/english/en_PP-OCRv3_rec_infer.tar && tar xf en_PP-OCRv3_rec_infer.tar
2022-05-07 16:55:20 +08:00
# Download the ultra-lightweight English table inch model and unzip it
2022-08-23 23:28:49 +08:00
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
# Download the layout model of publaynet dataset and unzip it
2022-08-23 23:28:49 +08:00
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
2022-05-07 16:55:20 +08:00
cd ..
2022-08-22 11:48:18 +08:00
```
If input is Chinese documentdownload Chinese models:
2022-10-12 21:28:48 +08:00
[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-版面分析模型)
2022-08-22 11:48:18 +08:00
2022-10-12 21:28:48 +08:00
<a name="4.2"></a>
### 4.2 Layout recovery
2022-08-22 11:48:18 +08:00
```bash
python3 predict_system.py \
--image_dir=./docs/table/1.png \
--det_model_dir=inference/en_PP-OCRv3_det_infer \
2022-08-22 11:48:18 +08:00
--rec_model_dir=inference/en_PP-OCRv3_rec_infer \
--rec_char_dict_path=../ppocr/utils/en_dict.txt \
2022-08-22 11:48:18 +08:00
--table_model_dir=inference/en_ppstructure_mobile_v2.0_SLANet_infer \
--table_char_dict_path=../ppocr/utils/dict/table_structure_dict.txt \
2022-08-22 11:48:18 +08:00
--layout_model_dir=inference/picodet_lcnet_x1_0_fgd_layout_infer \
--layout_dict_path=../ppocr/utils/dict/layout_dict/layout_publaynet_dict.txt \
--vis_font_path=../doc/fonts/simfang.ttf \
--recovery=True \
2022-08-22 11:48:18 +08:00
--output=../output/
2022-05-07 16:55:20 +08:00
```
2022-08-22 11:48:18 +08:00
After running, the docx of each picture will be saved in the directory specified by the output field
Field
2022-10-12 21:28:48 +08:00
- image_dirtest file can be picture, picture directory, pdf file, pdf file directory
2022-08-22 11:48:18 +08:00
- det_model_dirOCR detection model path
- rec_model_dirOCR recognition model path
- rec_char_dict_pathOCR 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_dirtabel recognition model path
- table_char_dict_pathtabel recognition dict path. If the Chinese model is used, no need to change
- layout_model_dirlayout analysis model path
- layout_dict_pathlayout analysis dict path. If the Chinese model is used, change to "../ppocr/utils/dict/layout_dict/layout_cdla_dict.txt"
- recoverywhether to enable layout of recovery, default False
- outputsave the recovery result path
2022-08-23 23:28:49 +08:00
2022-10-12 21:28:48 +08:00
<a name="5"></a>
2022-08-23 23:28:49 +08:00
2022-10-12 21:28:48 +08:00
## 5. More
2022-08-23 23:28:49 +08:00
2022-08-25 14:44:54 +08:00
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).
2022-08-23 23:28:49 +08:00
2022-08-25 14:44:54 +08:00
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).
2022-08-23 23:28:49 +08:00
2022-08-25 14:44:54 +08:00
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)
2022-08-23 23:28:49 +08:00
2022-08-25 14:44:54 +08:00
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)