PaddleOCR/StyleText/README.md

195 lines
6.7 KiB
Markdown
Raw Normal View History

2020-12-16 00:00:00 +08:00
English | [简体中文](README_ch.md)
2020-12-15 15:38:01 +08:00
2020-12-16 00:44:38 +08:00
## Style Text
2020-12-15 23:34:07 +08:00
### Contents
- [1. Introduction](#Introduction)
- [2. Preparation](#Preparation)
2020-12-16 00:44:38 +08:00
- [3. Quick Start](#Quick_Start)
- [4. Applications](#Applications)
2020-12-15 23:34:07 +08:00
- [5. Code Structure](#Code_structure)
2020-12-15 15:38:01 +08:00
2020-12-15 23:34:07 +08:00
<a name="Introduction"></a>
### Introduction
2020-12-15 15:38:01 +08:00
2020-12-15 23:34:07 +08:00
<div align="center">
<img src="doc/images/3.png" width="800">
</div>
<div align="center">
2020-12-16 00:44:38 +08:00
<img src="doc/images/9.png" width="600">
2020-12-15 23:34:07 +08:00
</div>
The Style-Text data synthesis tool is a tool based on Baidu's self-developed text editing algorithm "Editing Text in the Wild" [https://arxiv.org/abs/1908.03047](https://arxiv.org/abs/1908.03047).
Different from the commonly used GAN-based data synthesis tools, the main framework of Style-Text includes:
* (1) Text foreground style transfer module.
* (2) Background extraction module.
* (3) Fusion module.
2020-12-16 00:00:00 +08:00
After these three steps, you can quickly realize the image text style transfer. The following figure is some results of the data synthesis tool.
2020-12-15 23:34:07 +08:00
<div align="center">
2020-12-16 00:44:38 +08:00
<img src="doc/images/10.png" width="1000">
2020-12-15 23:34:07 +08:00
</div>
<a name="Preparation"></a>
2020-12-15 15:38:01 +08:00
#### Preparation
2020-12-15 21:53:06 +08:00
1. Please refer the [QUICK INSTALLATION](../doc/doc_en/installation_en.md) to install PaddlePaddle. Python3 environment is strongly recommended.
2020-12-15 15:38:01 +08:00
2. Download the pretrained models and unzip:
```bash
2020-12-15 23:34:07 +08:00
cd StyleText
wget https://paddleocr.bj.bcebos.com/dygraph_v2.0/style_text/style_text_models.zip
2020-12-15 15:38:01 +08:00
unzip style_text_models.zip
```
2020-12-15 23:34:07 +08:00
If you save the model in another location, please modify the address of the model file in `configs/config.yml`, and you need to modify these three configurations at the same time:
2020-12-15 15:38:01 +08:00
```
bg_generator:
pretrain: style_text_rec/bg_generator
...
text_generator:
pretrain: style_text_models/text_generator
...
fusion_generator:
pretrain: style_text_models/fusion_generator
```
2020-12-16 00:44:38 +08:00
<a name="Quick_Start"></a>
### Quick Start
2020-12-15 15:38:01 +08:00
2020-12-15 23:34:07 +08:00
#### Synthesis single image
2020-12-15 15:38:01 +08:00
2020-12-16 00:44:38 +08:00
1. You can run `tools/synth_image` and generate the demo image, which is saved in the current folder.
2020-12-15 15:38:01 +08:00
2020-12-15 23:34:07 +08:00
```python
2020-12-16 00:44:38 +08:00
python3 -m tools.synth_image -c configs/config.yml --style_image examples/style_images/2.jpg --text_corpus PaddleOCR --language en
2020-12-15 15:38:01 +08:00
```
2020-12-16 00:44:38 +08:00
* Note: The language options is correspond to the corpus. Currently, the tool only supports English, Simplified Chinese and Korean.
For example, enter the following image and corpus `PaddleOCR`.
<div align="center">
<img src="examples/style_images/2.jpg" width="300">
</div>
The result `fake_fusion.jpg` will be generated.
<div align="center">
<img src="doc/images/4.jpg" width="300">
</div>
What's more, the medium result `fake_bg.jpg` will also be saved, which is the background output.
<div align="center">
<img src="doc/images/7.jpg" width="300">
</div>
`fake_text.jpg` * `fake_text.jpg` is the generated image with the same font style as `Style Input`.
2020-12-15 15:38:01 +08:00
2020-12-16 00:44:38 +08:00
<div align="center">
<img src="doc/images/8.jpg" width="300">
</div>
2020-12-15 15:38:01 +08:00
2020-12-15 23:34:07 +08:00
#### Batch synthesis
2020-12-15 15:38:01 +08:00
2020-12-16 00:44:38 +08:00
In actual application scenarios, it is often necessary to synthesize pictures in batches and add them to the training set. StyleText can use a batch of style pictures and corpus to synthesize data in batches. The synthesis process is as follows:
2020-12-15 15:38:01 +08:00
1. The referenced dataset can be specifed in `configs/dataset_config.yml`:
2020-12-15 23:34:07 +08:00
2020-12-16 00:44:38 +08:00
* `Global`
* `output_dir:`Output synthesis data path.
* `StyleSampler`
* `image_home`style images' folder.
* `label_file`Style images' file list. If label is provided, then it is the label file path.
* `with_label`Whether the `label_file` is label file list.
* `CorpusGenerator`
* `method`Method of CorpusGeneratorsupports `FileCorpus` and `EnNumCorpus`. If `EnNumCorpus` is usedNo other configuration is neededotherwise you need to set `corpus_file` and `language`.
* `language`Language of the corpus.
* `corpus_file`: Filepath of the corpus.
2020-12-15 15:38:01 +08:00
2020-12-16 00:00:00 +08:00
We provide a general dataset containing Chinese, English and Korean (50,000 images in all) for your trial ([download link](https://paddleocr.bj.bcebos.com/dygraph_v2.0/style_text/chkoen_5w.tar)), some examples are given below :
2020-12-15 23:34:07 +08:00
<div align="center">
<img src="doc/images/5.png" width="800">
</div>
2020-12-15 15:38:01 +08:00
2. You can run the following command to start synthesis task:
``` bash
python -m tools.synth_dataset.py -c configs/dataset_config.yml
```
2020-12-15 23:34:07 +08:00
2020-12-16 00:44:38 +08:00
<a name="Applications"></a>
### Applications
2020-12-15 23:34:07 +08:00
We take two scenes as examples, which are metal surface English number recognition and general Korean recognition, to illustrate practical cases of using StyleText to synthesize data to improve text recognition. The following figure shows some examples of real scene images and composite images:
<div align="center">
2020-12-16 00:44:38 +08:00
<img src="doc/images/11.png" width="800">
2020-12-15 23:34:07 +08:00
</div>
After adding the above synthetic data for training, the accuracy of the recognition model is improved, which is shown in the following table:
| Scenario | Characters | Raw Data | Test Data | Only Use Raw Data</br>Recognition Accuracy | New Synthetic Data | Simultaneous Use of Synthetic Data</br>Recognition Accuracy | Index Improvement |
| -------- | ---------- | -------- | -------- | ----------- --------------- | ------------ | --------------------- -| -------- |
| Metal surface | English and numbers | 2203 | 650 | 0.5938 | 20000 | 0.7546 | 16% |
| Random background | Korean | 5631 | 1230 | 0.3012 | 100000 | 0.5057 | 20% |
<a name="Code_structure"></a>
### Code Structure
2020-12-16 00:44:38 +08:00
2020-12-15 23:34:07 +08:00
```
style_text_rec
2020-12-16 00:44:38 +08:00
|-- arch // Network module files.
2020-12-15 23:34:07 +08:00
| |-- base_module.py
| |-- decoder.py
| |-- encoder.py
| |-- spectral_norm.py
| `-- style_text_rec.py
2020-12-16 00:44:38 +08:00
|-- configs // Config files.
2020-12-15 23:34:07 +08:00
| |-- config.yml
| `-- dataset_config.yml
2020-12-16 00:44:38 +08:00
|-- engine // Synthesis engines.
| |-- corpus_generators.py // Sample corpus from file or generate random corpus.
| |-- predictors.py // Predict using network.
| |-- style_samplers.py // Sample style images.
| |-- synthesisers.py // Manage other engines to synthesis images.
| |-- text_drawers.py // Generate standard input text images.
| `-- writers.py // Write synthesis images and labels into files.
|-- examples // Example files.
2020-12-15 23:34:07 +08:00
| |-- corpus
| | `-- example.txt
| |-- image_list.txt
| `-- style_images
| |-- 1.jpg
| `-- 2.jpg
2020-12-16 00:44:38 +08:00
|-- fonts // Font files.
2020-12-15 23:34:07 +08:00
| |-- ch_standard.ttf
| |-- en_standard.ttf
| `-- ko_standard.ttf
2020-12-16 00:44:38 +08:00
|-- tools // Program entrance.
2020-12-15 23:34:07 +08:00
| |-- __init__.py
2020-12-16 00:44:38 +08:00
| |-- synth_dataset.py // Synthesis dataset.
| `-- synth_image.py // Synthesis image.
`-- utils // Module of basic functions.
2020-12-15 23:34:07 +08:00
|-- config.py
|-- load_params.py
|-- logging.py
|-- math_functions.py
`-- sys_funcs.py
```