-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathexample.py
214 lines (189 loc) · 5.81 KB
/
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
import inspect
import os
from pathlib import Path
import imgaug.augmenters as iaa
from text_renderer.effect import *
from text_renderer.corpus import *
from text_renderer.config import (
RenderCfg,
NormPerspectiveTransformCfg,
GeneratorCfg,
FixedTextColorCfg,
)
from text_renderer.layout.same_line import SameLineLayout
from text_renderer.layout.extra_text_line import ExtraTextLineLayout
CURRENT_DIR = Path(os.path.abspath(os.path.dirname(__file__)))
OUT_DIR = CURRENT_DIR / "output"
DATA_DIR = CURRENT_DIR
BG_DIR = DATA_DIR / "bg"
CHAR_DIR = DATA_DIR / "char"
FONT_DIR = DATA_DIR / "font"
FONT_LIST_DIR = DATA_DIR / "font_list"
TEXT_DIR = DATA_DIR / "text"
font_cfg = dict(
font_dir=FONT_DIR,
font_list_file=FONT_LIST_DIR / "font_list.txt",
font_size=(30, 31),
)
perspective_transform = NormPerspectiveTransformCfg(20, 20, 1.5)
def get_char_corpus():
return CharCorpus(
CharCorpusCfg(
text_paths=[TEXT_DIR / "chn_text.txt", TEXT_DIR / "eng_text.txt"],
filter_by_chars=True,
chars_file=CHAR_DIR / "chn.txt",
length=(5, 10),
char_spacing=(-0.3, 1.3),
**font_cfg
),
)
def base_cfg(
name: str, corpus, corpus_effects=None, layout_effects=None, layout=None, gray=True
):
return GeneratorCfg(
num_image=50,
save_dir=OUT_DIR / name,
render_cfg=RenderCfg(
bg_dir=BG_DIR,
perspective_transform=perspective_transform,
gray=gray,
layout_effects=layout_effects,
layout=layout,
corpus=corpus,
corpus_effects=corpus_effects,
),
)
def chn_data():
return base_cfg(
inspect.currentframe().f_code.co_name,
corpus=get_char_corpus(),
corpus_effects=Effects(
[
Line(0.5, color_cfg=FixedTextColorCfg()),
OneOf([DropoutRand(), DropoutVertical()]),
]
),
)
def enum_data():
return base_cfg(
inspect.currentframe().f_code.co_name,
corpus=EnumCorpus(
EnumCorpusCfg(
text_paths=[TEXT_DIR / "enum_text.txt"],
filter_by_chars=True,
chars_file=CHAR_DIR / "chn.txt",
**font_cfg
),
),
)
def rand_data():
return base_cfg(
inspect.currentframe().f_code.co_name,
corpus=RandCorpus(
RandCorpusCfg(chars_file=CHAR_DIR / "chn.txt", **font_cfg),
),
)
def eng_word_data():
return base_cfg(
inspect.currentframe().f_code.co_name,
corpus=WordCorpus(
WordCorpusCfg(
text_paths=[TEXT_DIR / "eng_text.txt"],
filter_by_chars=True,
chars_file=CHAR_DIR / "eng.txt",
**font_cfg
),
),
)
def same_line_data():
return base_cfg(
inspect.currentframe().f_code.co_name,
layout=SameLineLayout(),
gray=False,
corpus=[
EnumCorpus(
EnumCorpusCfg(
text_paths=[TEXT_DIR / "enum_text.txt"],
filter_by_chars=True,
chars_file=CHAR_DIR / "chn.txt",
**font_cfg
),
),
CharCorpus(
CharCorpusCfg(
text_paths=[
TEXT_DIR / "chn_text.txt",
TEXT_DIR / "eng_text.txt",
],
filter_by_chars=True,
chars_file=CHAR_DIR / "chn.txt",
length=(5, 10),
font_dir=font_cfg["font_dir"],
font_list_file=font_cfg["font_list_file"],
font_size=(30, 35),
),
),
],
corpus_effects=[Effects([Padding(), DropoutRand()]), NoEffects()],
layout_effects=Effects(Line(p=1)),
)
def extra_text_line_data():
return base_cfg(
inspect.currentframe().f_code.co_name,
layout=ExtraTextLineLayout(),
corpus=[
CharCorpus(
CharCorpusCfg(
text_paths=[
TEXT_DIR / "chn_text.txt",
TEXT_DIR / "eng_text.txt",
],
filter_by_chars=True,
chars_file=CHAR_DIR / "chn.txt",
length=(9, 10),
font_dir=font_cfg["font_dir"],
font_list_file=font_cfg["font_list_file"],
font_size=(30, 35),
),
),
CharCorpus(
CharCorpusCfg(
text_paths=[
TEXT_DIR / "chn_text.txt",
TEXT_DIR / "eng_text.txt",
],
filter_by_chars=True,
chars_file=CHAR_DIR / "chn.txt",
length=(9, 10),
font_dir=font_cfg["font_dir"],
font_list_file=font_cfg["font_list_file"],
font_size=(30, 35),
),
),
],
corpus_effects=[Effects([Padding()]), NoEffects()],
layout_effects=Effects(Line(p=1)),
)
def imgaug_emboss_example():
return base_cfg(
inspect.currentframe().f_code.co_name,
corpus=get_char_corpus(),
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))),
]
),
)
# fmt: off
# The configuration file must have a configs variable
configs = [
chn_data(),
enum_data(),
rand_data(),
eng_word_data(),
same_line_data(),
extra_text_line_data(),
imgaug_emboss_example()
]
# fmt: on