-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
effect_layout_example.py
232 lines (192 loc) · 5.88 KB
/
effect_layout_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# All Effect/Layout example config
# 1. Run effect_layout_example.py, generate images in effect_layout_image
# 2. Update README.md
import inspect
import os
from dataclasses import dataclass
from pathlib import Path
import numpy as np
from text_renderer.effect import *
from text_renderer.corpus import *
from text_renderer.config import (
RenderCfg,
NormPerspectiveTransformCfg,
GeneratorCfg,
SimpleTextColorCfg,
TextColorCfg,
FixedTextColorCfg,
FixedPerspectiveTransformCfg,
)
from text_renderer.effect.curve import Curve
from text_renderer.layout import SameLineLayout, ExtraTextLineLayout
CURRENT_DIR = Path(os.path.abspath(os.path.dirname(__file__)))
BG_DIR = CURRENT_DIR / "bg"
font_cfg = dict(
font_dir=CURRENT_DIR / "font",
font_size=(30, 31),
)
small_font_cfg = dict(
font_dir=CURRENT_DIR / "font",
font_size=(20, 21),
)
def base_cfg(name: str):
return GeneratorCfg(
num_image=5,
save_dir=CURRENT_DIR / "effect_layout_image" / name,
render_cfg=RenderCfg(
bg_dir=BG_DIR,
corpus=EnumCorpus(
EnumCorpusCfg(
items=["Hello! 你好!"],
text_color_cfg=FixedTextColorCfg(),
**font_cfg,
),
),
),
)
def dropout_rand():
cfg = base_cfg(inspect.currentframe().f_code.co_name)
cfg.render_cfg.corpus_effects = Effects(DropoutRand(p=1, dropout_p=(0.3, 0.5)))
return cfg
def dropout_horizontal():
cfg = base_cfg(inspect.currentframe().f_code.co_name)
cfg.render_cfg.corpus_effects = Effects(
DropoutHorizontal(p=1, num_line=2, thickness=3)
)
return cfg
def dropout_vertical():
cfg = base_cfg(inspect.currentframe().f_code.co_name)
cfg.render_cfg.corpus_effects = Effects(DropoutVertical(p=1, num_line=15))
return cfg
def line():
poses = [
"top",
"bottom",
"left",
"right",
"top_left",
"top_right",
"bottom_left",
"bottom_right",
"horizontal_middle",
"vertical_middle",
]
cfgs = []
for i, pos in enumerate(poses):
pos_p = [0] * len(poses)
pos_p[i] = 1
cfg = base_cfg(f"{inspect.currentframe().f_code.co_name}_{pos}")
cfg.render_cfg.corpus_effects = Effects(
Line(p=1, thickness=(3, 4), line_pos_p=pos_p)
)
cfgs.append(cfg)
return cfgs
def padding():
cfg = base_cfg(inspect.currentframe().f_code.co_name)
cfg.render_cfg.corpus_effects = Effects(
Padding(p=1, w_ratio=[0.2, 0.21], h_ratio=[0.7, 0.71], center=True)
)
return cfg
def same_line_layout_different_font_size():
cfg = base_cfg(inspect.currentframe().f_code.co_name)
cfg.render_cfg.layout = SameLineLayout(h_spacing=(0.9, 0.91))
cfg.render_cfg.corpus = [
EnumCorpus(
EnumCorpusCfg(
items=["Hello "],
text_color_cfg=FixedTextColorCfg(),
**font_cfg,
),
),
EnumCorpus(
EnumCorpusCfg(
items=[" World!"],
text_color_cfg=FixedTextColorCfg(),
**small_font_cfg,
),
),
]
return cfg
def extra_text_line_layout():
cfg = base_cfg(inspect.currentframe().f_code.co_name)
cfg.render_cfg.layout = ExtraTextLineLayout(bottom_prob=1.0)
cfg.render_cfg.corpus = [
EnumCorpus(
EnumCorpusCfg(
items=["Hello world"],
text_color_cfg=FixedTextColorCfg(),
**font_cfg,
),
),
EnumCorpus(
EnumCorpusCfg(
items=["THIS IS AN EXTRA TEXT LINE!"],
text_color_cfg=FixedTextColorCfg(),
**font_cfg,
),
),
]
return cfg
def color_image():
cfg = base_cfg(inspect.currentframe().f_code.co_name)
cfg.render_cfg.gray = False
return cfg
def perspective_transform():
cfg = base_cfg(inspect.currentframe().f_code.co_name)
cfg.render_cfg.perspective_transform = FixedPerspectiveTransformCfg(30, 30, 1.5)
return cfg
def compact_char_spacing():
cfg = base_cfg(inspect.currentframe().f_code.co_name)
cfg.render_cfg.corpus.cfg.char_spacing = -0.3
return cfg
def large_char_spacing():
cfg = base_cfg(inspect.currentframe().f_code.co_name)
cfg.render_cfg.corpus.cfg.char_spacing = 0.5
return cfg
def curve():
cfg = base_cfg(inspect.currentframe().f_code.co_name)
cfg.render_cfg.corpus_effects = Effects(
[
Padding(p=1, w_ratio=[0.2, 0.21], h_ratio=[0.7, 0.71], center=True),
Curve(p=1, period=180, amplitude=(4, 5)),
]
)
return cfg
def vertical_text():
cfg = base_cfg(inspect.currentframe().f_code.co_name)
cfg.render_cfg.corpus.cfg.horizontal = False
# cfg.render_cfg.corpus.cfg.char_spacing = 0.1
return cfg
def bg_and_text_mask():
cfg = base_cfg(inspect.currentframe().f_code.co_name)
cfg.render_cfg.perspective_transform = FixedPerspectiveTransformCfg(30, 30, 1.5)
cfg.render_cfg.return_bg_and_mask = True
cfg.render_cfg.height = 48
return cfg
def emboss():
import imgaug.augmenters as iaa
cfg = base_cfg(inspect.currentframe().f_code.co_name)
cfg.render_cfg.height = 48
cfg.render_cfg.corpus_effects = Effects(
[
Padding(p=1, w_ratio=[0.2, 0.21], h_ratio=[0.7, 0.71], center=True),
ImgAugEffect(aug=iaa.Emboss(alpha=(0.9, 1.0), strength=(1.5, 1.6))),
]
)
return cfg
configs = [
# bg_and_text_mask()
emboss()
# vertical_text()
# extra_text_line_layout()
# char_spacing_compact(),
# char_spacing_large(),
# *line(),
# perspective_transform(),
# color_image(),
# dropout_rand(),
# dropout_horizontal(),
# dropout_vertical(),
# padding(),
# same_line_layout_different_font_size(),
]