Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

新增了对字体与背景颜色的支持 #66

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
__pycache__/
*.pyc
.DS_Store
.devcontainer/

data/fonts/
data/corpus/**
Expand Down
9 changes: 9 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM python:3.6

COPY . /the/workdir/text_render
WORKDIR /the/workdir/text_render

RUN apt-get update && apt-get install libgl1-mesa-glx -y
RUN pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt

CMD ["python3", "main.py"]
3 changes: 2 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
MIT License

Copyright (c) 2018 Qing
Original work Copyright (c) 2018 Qing
Modified work Copyright 2019 Tang

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
91 changes: 73 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
# Text Renderer
Generate text images for training deep learning OCR model (e.g. [CRNN](https://github.com/bgshih/crnn)).
# 项目简介
基于原[text_render](https://github.com/Sanster/text_renderer) 根据自己的需求做了一些修改;
主要增加了对于色彩的支持(包含文字色彩与背景色彩图);
Support both latin and non-latin text.

# Setup
- Ubuntu 16.04
- python 3.5+

Install dependencies:
```
pip3 install -r requirements.txt

Python3 main.py
```

# Demo
By default, simply run `python3 main.py` will generate 20 text images
and a labels.txt file in `output/default/`.

![example1.jpg](./imgs/example1.jpg)
![example2.jpg](./imgs/example2.jpg)
![example1.jpg](./imgs/example1_c.jpg)
![example2.jpg](./imgs/example2_c.jpg)

![example3.jpg](./imgs/example3.jpg)
![example4.jpg](./imgs/example4.jpg)
![example3.jpg](./imgs/example3_c.jpg)
![example4.jpg](./imgs/example4_c.jpg)

# Use your own data to generate image
1. Please run `python3 main.py --help` to see all optional arguments and their meanings.
And put your own data in corresponding folder.

2. Config text effects and fraction in `configs/default.yaml` file(or create a
new config file and use it by `--config_file` option), here are some examples:
# 文字变换特效
编辑 `configs/default.yaml` 来实现文字弯曲,模糊等效果,详见原[text_render](https://github.com/Sanster/text_renderer);
新增表格线颜色控制,字体颜色控制,添加冗余文字

|Effect name|Image|
|------------|----|
Expand All @@ -40,12 +36,70 @@ new config file and use it by `--config_file` option), here are some examples:
|Random char space small|![random char space small](./imgs/effects/random_space_small.jpg)|
|Middle line|![middle line](./imgs/effects/line_middle.jpg)|
|Table line|![table line](./imgs/effects/line_table.jpg)|
|Table line(offset)|![table line offset](./imgs/effects/line_table_offset.jpg)|
|Under line|![under line](./imgs/effects/line_under.jpg)|
|Emboss|![emboss](./imgs/effects/emboss.jpg)|
|Reverse color|![reverse color](./imgs/effects/reverse.jpg)|
|Blur|![blur](./imgs/effects/blur.jpg)|
|font_color|![font_color](./imgs/effects/colored.jpg)|
|line_color|![line_color](./imgs/effects/table.jpg)|
|extra_words|![extra_words](./imgs/effects/extra.jpg)|
|texture|![texture](./imgs/effects/texture.jpg)|
|second_font|![second](./imgs/effects/second_font.jpg)

颜色配置说明:
程序会随机拾取下方所配置颜色种类(按照fraction中的概率,所有颜色fraction相加需要等于1),选中颜色种类后,rgb由l_boundary与h_boundary中取一个随机数确定; font_color 同理
```
line_color:
black:
fraction: 0.3
l_boundary: 0,0,0
h_boundary: 64,64,64
gray:
fraction: 0.7
l_boundary: 77,77,77
h_boundary: 230,230,230
```
冗余文字说明:
如开启选项,程序会按照设定的总概率随机选取一段独立的文字(颜色,字体及字号大小均采用正文的设置),生成的文字长度在1到最长文本长度之间随机。 top 与 bottom 中的 fraction 决定了冗余文字生成在上方或下方的概率;
*注:由于冗余文字尽量生成在上方与下方,所以可能生成在图片之外,所以最终出现的概率会小于设定值;
```
extra_words:
enable: true
fraction: 0.05
top:
fraction: 0.5
bottom:
fraction: 0.5
```
文字纹理说明:
开启选项后,会在文本上生成perling噪音的侵蚀效果(速度较慢)。可以调节octaves,persistence,scale参数调整纹理的效果(改变似乎不是非常明显)
```
texture:
enable: true
fraction: 0.05

cloud:
enable: true
fraction: 1
octaves: 2
persistence: 0.4
scale : 2000
```

第二字体说明:
开启选项后,会在文本会在文本行上出现第二种字体,可以设置字体、颜色、大小是否与主字体相同。change_rate决定了在文本行中第二字体出现的概率
```
second_font:
enable: true
fraction: 0.5
change_rate: 0.3
font_change: true
font_size_change: true
font_color_change: true

```

3. Run `main.py` file.

# Strict mode
For no-latin language(e.g Chinese), it's very common that some fonts only support
Expand Down Expand Up @@ -91,4 +145,5 @@ You can see how perspectiveTransform works and all bounding/rotated boxes.


# Todo
See https://github.com/Sanster/text_renderer/projects/1
- 增加同一张图中不同字体、大小与颜色的设置;
- 增加文字侵蚀,打印不完整的效果;
115 changes: 95 additions & 20 deletions configs/default.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,50 @@
# Small font_size will make text looks like blured/prydown
font_size:
min: 14
max: 23
min: 30
max: 42

# choose font_color range
# color boundary is in R,G,B format
font_color:
black:
fraction: 0.3
l_boundary: 0,0,0
h_boundary: 64,64,64
gray:
fraction: 0.4
l_boundary: 77,77,77
h_boundary: 230,230,230
blue:
fraction: 0.2
l_boundary: 0,0,150
h_boundary: 60,60,255
brown:
fraction: 0.1
l_boundary: 139,70,19
h_boundary: 160,82,43

# second font in one line
# choose what's different from first font

second_font:
enable: false
fraction: 0.5
change_rate: 0.3
font_change: false
font_size_change: true
font_color_change: true

# choose line_color range
# color boundary is in R,G,B format
line_color:
black:
fraction: 0.3
l_boundary: 0,0,0
h_boundary: 64,64,64
gray:
fraction: 0.7
l_boundary: 77,77,77
h_boundary: 230,230,230

# By default, text is drawed by Pillow with (https://stackoverflow.com/questions/43828955/measuring-width-of-text-python-pil)
# If `random_space` is enabled, some text will be drawed char by char with a random space
Expand All @@ -11,27 +54,36 @@ random_space:
min: -0.1 # -0.1 will make chars very close or even overlapped
max: 0.1

# draw extra random words on top or bottom of the img
extra_words:
enable: true
fraction: 0.05
top:
fraction: 0.5
bottom:
fraction: 0.5

# Do remap with sin()
# Currently this process is very slow!
curve:
enable: false
enable: true
fraction: 0.3
period: 360 # degree, sin 函数的周期
min: 1 # sin 函数的幅值范围
max: 5

# random crop text height
crop:
enable: false
enable: true
fraction: 0.5

# top and bottom will applied equally
top:
min: 5
max: 10 # in pixel, this value should small than img_height
max: 15 # in pixel, this value should small than img_height
bottom:
min: 5
max: 10 # in pixel, this value should small than img_height
max: 15 # in pixel, this value should small than img_height

# Use image in bg_dir as background for text
img_bg:
Expand Down Expand Up @@ -68,17 +120,39 @@ perspective_transform:

blur:
enable: true
fraction: 0.03
fraction: 0.1

# If an image is applied blur, it will not be applied prydown
prydown:
gauss:
enable: true
fraction: 0.25

norm:
enable: true
fraction: 0.25

motion:
enable: true
fraction: 0.25

prydown:
enable: true
fraction: 0.25
max_scale: 1.5

texture:
enable: true
fraction: 0.03
max_scale: 1.5 # Image will first resize to 1.5x, and than resize to 1x
fraction: 0.05

cloud:
enable: true
fraction: 1
octaves: 2
persistence: 0.4
scale : 2000

noise:
enable: true
fraction: 0.3
fraction: 0.03

gauss:
enable: true
Expand All @@ -97,20 +171,21 @@ noise:
fraction: 0.25

line:
enable: false
fraction: 0.05
enable: true
fraction: 0.1

under_line:
enable: false
fraction: 0.2
enable: true
fraction: 0.1

table_line:
enable: false
fraction: 0.3
enable: true
fraction: 0.8
random_offset: true # line will cross chars

middle_line:
enable: false
fraction: 0.5
enable: true
fraction: 0.1

# These operates are applied on the final output image,
# so actually it can also be applied in training process as an data augmentation method.
Expand Down
9 changes: 1 addition & 8 deletions data/corpus/The Hitchhiker's Guide to the Galaxy.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
  在银河系西螺旋臂的末端那片未曾标明的寂静虚空中,悬挂着—颗不被人注意的小小的黄色太阳。距离它大约9800万英里的轨道上,运行着一颗完全无足轻重的蓝绿色小行星。这颗行星上由猿演化而来的生命形式原始得令人吃惊,他们居然还认为电子表是一项非常了不起的设想。
  这颗行星上存在着——或者说曾经存在——一个问题,那就是:大部分的居民在大部分的时间里都感到不开心。针对这一问题曾提出过各种各样的解决方案,但其中大部分都是紧紧围绕那些绿色小纸片的运动来着手的——这很奇怪,因为实际上并不是这些绿色小纸片不开心。
  于是,问题依旧困扰着这颗星球。所有的人都感到不自在,其中大部分简直可以说是凄凄惨惨,甚至包括那些拥有电子表的人。
  不过这与我们这里要讲的故事无关。
  我们这个故事是关于一场可怕而又愚蠢的灾难,以及它的一系列后果。
  同时这个故事也是关于一本书的,书名叫做《银河系漫游指南》,这不是一本地球上的书,它从来没有在地球上出版过,并且一直到那场可怕的灾难发牛之前,没有任何地球人见过或者听说过它。
  然而,这绝对是一本非凡的书。
  事实上,它可能是小熊星座那些伟大的出版公司所出版过的最非凡的书了——当然,这些公司也没有任何—个地球人听说过。但是,在银河系东部外缘的许多更加开放的文明里,《银河系漫游指南》已经取代伟大的《银河系百科全书》成为所有知识和智慧的标准。这是因为,尽管显得冗长,并且包含许多虚假或者至少是不够准确的信息,但它却在两个极其重要的方面超越了那部更加陈旧和呆板的著作。
  第一,它稍微便宜一点儿;第二,在它的封面上以大而友善的字体写着“不要恐慌”这句话。
  不过,这个关于那个可怕而又愚蠢的星期四的故事,这个关于那次灾难的奇特后果的故事,这个关于这些后果如何无可逃避地与这本非凡的书交织在一起的故事,它的开始却非常简单。
  它的开始与一所房子有关。
  0123456789¥20.642018-06-01130km公里2020年10月01日
3 changes: 3 additions & 0 deletions data/corpus_line/corpus_line.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
2018-01-12
2018年12月21日
12.834
Binary file added data/fonts/chn/img_font/-.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/fonts/chn/img_font/..png
Binary file not shown.
Binary file added data/fonts/chn/img_font/0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/fonts/chn/img_font/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/fonts/chn/img_font/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/fonts/chn/img_font/3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/fonts/chn/img_font/4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/fonts/chn/img_font/5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/fonts/chn/img_font/6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/fonts/chn/img_font/7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/fonts/chn/img_font/8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/fonts/chn/img_font/9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/fonts/chn/img_font/k.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/fonts/chn/img_font/m.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/fonts/chn/img_font/¥.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/fonts/chn/img_font/元.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/fonts/chn/img_font/公.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/fonts/chn/img_font/年.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/fonts/chn/img_font/日.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/fonts/chn/img_font/月.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/fonts/chn/img_font/里.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions data/fonts_list/img_font.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
./data/fonts/chn/img_font
8 changes: 8 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: "3"

services:
text_render:
build: .
volumes:
- ./data/:/the/workdir/text_render/data/
- ./output/:/the/workdir/text_render/output/
15 changes: 9 additions & 6 deletions help_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
# See parse_args for supported arguments
configs = [
dict(tag='test1',
num_img=10,
img_width=150),
dict(tag='test2',
num_img=20,
debug=False,
num_img=50,
img_width=150,
fonts_by_image=True,
corpus_dir='./data/corpus_line',
corpus_mode='list',
corpus_dir='./data/list_corpus')
fonts_list='./data/fonts_list/img_font.txt',
strict=True),
dict(tag='test2',
num_img=50,
strict=True)
]


Expand Down
Binary file added imgs/effects/colored.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imgs/effects/extra.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imgs/effects/line_table_offset.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imgs/effects/second_font.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imgs/effects/table.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imgs/effects/texture.jpg
Binary file removed imgs/example1.jpg
Diff not rendered.
Binary file added imgs/example1_c.jpg
Binary file removed imgs/example2.jpg
Diff not rendered.
Binary file added imgs/example2_c.jpg
Binary file removed imgs/example3.jpg
Diff not rendered.
Binary file added imgs/example3_c.jpg
Binary file removed imgs/example4.jpg
Diff not rendered.
Binary file added imgs/example4_c.jpg
Loading