-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.py
518 lines (496 loc) · 15.2 KB
/
run.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
import argparse
import os, errno
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
import random as rnd
import sys
from tqdm import tqdm
from string_generator import (
create_strings_from_dict,
create_strings_from_file,
create_strings_from_wikipedia,
create_strings_randomly,
)
from utils import load_dict, load_fonts
from data_generator import FakeTextDataGenerator
from multiprocessing import Pool
def margins(margin):
margins = margin.split(",")
if len(margins) == 1:
return [int(margins[0])] * 4
return [int(m) for m in margins]
def parse_arguments():
parser = argparse.ArgumentParser(
description="Generate synthetic text data for urdu text recognition."
)
parser.add_argument(
"--output_dir", type=str, nargs="?", help="The output directory", default="out/"
)
parser.add_argument(
"-i",
"--input_file",
type=str,
nargs="?",
help="When set, this argument uses a specified text file as source for the text",
default="",
)
parser.add_argument(
"-l",
"--language",
type=str,
nargs="?",
help="The language to use for the text",
default="ur",
)
parser.add_argument(
"-c",
"--count",
type=int,
nargs="?",
help="The number of images to be created.",
required=True,
)
parser.add_argument(
"-rs",
"--random_sequences",
action="store_true",
help="Use random sequences as the source text for the generation. Set '-let','-num','-sym' to use letters/numbers/symbols. If none specified, using all three.",
default=False,
)
parser.add_argument(
"-let",
"--include_letters",
action="store_true",
help="Define if random sequences should contain letters. Only works with -rs",
default=False,
)
parser.add_argument(
"-num",
"--include_numbers",
action="store_true",
help="Define if random sequences should contain numbers. Only works with -rs",
default=False,
)
parser.add_argument(
"-sym",
"--include_symbols",
action="store_true",
help="Define if random sequences should contain symbols. Only works with -rs",
default=False,
)
parser.add_argument(
"-w",
"--length",
type=int,
nargs="?",
help="Define how many words should be included in each generated sample. If the text source is Wikipedia, this is the MINIMUM length",
default=1,
)
parser.add_argument(
"-ml",
"--max_length",
type=int,
nargs="?",
help="Max number of characters in each generated sample",
default=-1,
)
parser.add_argument(
"-r",
"--random",
action="store_true",
help="Define if the produced string will have variable word count (with --length being the maximum)",
default=False,
)
parser.add_argument(
"-ht",
"--height",
type=int,
nargs="?",
help="Define the height of the produced images if horizontal, else the width",
default=32,
)
parser.add_argument(
"-t",
"--thread_count",
type=int,
nargs="?",
help="Define the number of thread to use for image generation",
default=1,
)
parser.add_argument(
"-e",
"--extension",
type=str,
nargs="?",
help="Define the extension to save the image with",
default="jpg",
)
parser.add_argument(
"-k",
"--skew_angle",
type=int,
nargs="?",
help="Define skewing angle of the generated text. In positive degrees",
default=0,
)
parser.add_argument(
"-rk",
"--random_skew",
action="store_true",
help="When set, the skew angle will be randomized between the value set with -k and it's opposite",
default=False,
)
parser.add_argument(
"-wk",
"--use_wikipedia",
action="store_true",
help="Use Wikipedia as the source text for the generation, using this paremeter ignores -r, -n, -s",
default=False,
)
parser.add_argument(
"-bl",
"--blur",
type=int,
nargs="?",
help="Apply gaussian blur to the resulting sample. Should be an integer defining the blur radius",
default=0,
)
parser.add_argument(
"-rbl",
"--random_blur",
action="store_true",
help="When set, the blur radius will be randomized between 0 and -bl.",
default=False,
)
parser.add_argument(
"-rx",
"--random_shearx",
action="store_true",
help="When set, random shearx",
default=False,
)
parser.add_argument(
"-sp",
"--salt_and_pepper",
type=float,
nargs="?",
help="Randomly add salt_and_pepper noise to the resulting sample. Should be a float defining the probability of adding noise",
default=0.0,
)
parser.add_argument(
"-b",
"--background",
type=int,
nargs="?",
help="Define what kind of background to use. 0: Gaussian Noise, 1: Plain white, 2: Quasicrystal, 3: Image, 4: Random",
default=4,
)
parser.add_argument(
"-hw",
"--handwritten",
action="store_true",
help='Define if the data will be "handwritten" by an RNN',
)
parser.add_argument(
"-na",
"--name_format",
type=int,
help="Define how the produced files will be named",
default=2,
)
parser.add_argument(
"-om",
"--output_mask",
type=int,
help="Define if the generator will return masks for the text",
default=0,
)
parser.add_argument(
"-d",
"--distorsion",
type=int,
nargs="?",
help="Define a distorsion applied to the resulting image. 0: None (Default), 1: Sine wave, 2: Cosine wave, 3: Random",
default=0,
)
parser.add_argument(
"-do",
"--distorsion_orientation",
type=int,
nargs="?",
help="Define the distorsion's orientation. Only used if -d is specified. 0: Vertical (Up and down), 1: Horizontal (Left and Right), 2: Both, 3: Random",
default=3,
)
parser.add_argument(
"-wd",
"--width",
type=int,
nargs="?",
help="Define the width of the resulting image. If not set it will be the width of the text + 10. If the width of the generated text is bigger that number will be used",
default=-1,
)
parser.add_argument(
"-rr",
"--random_resize",
action="store_true",
help="When set, the image width & height will be randomly resized betn half to double",
default=False,
)
parser.add_argument(
"-al",
"--alignment",
type=int,
nargs="?",
help="Define the alignment of the text in the image. Either use 3 for random or 0, 1 & 2 are only used if the width parameter is set. 0: left, 1: center, 2: right, 3: Random",
default=3,
)
parser.add_argument(
"-or",
"--orientation",
type=int,
nargs="?",
help="Define the orientation of the text. 0: Horizontal, 1: Vertical",
default=0,
)
parser.add_argument(
"-tc",
"--text_color",
type=str,
nargs="?",
help="Define the text's color, should be either a single hex color or a range in the ?,? format.",
default="#282828",
)
parser.add_argument(
"-sw",
"--space_width",
type=float,
nargs="?",
help="Define the width of the spaces between words. 2.0 means twice the normal space width",
default=1.0,
)
parser.add_argument(
"-cs",
"--character_spacing",
type=int,
nargs="?",
help="Define the width of the spaces between characters. 2 means two pixels",
default=0,
)
parser.add_argument(
"-m",
"--margins",
type=margins,
nargs="?",
help="Define the margins around the text when rendered. In pixels",
default=(10, 10, 10, 10),
)
parser.add_argument(
"-rm",
"--random_margins",
action="store_true",
help="Random the margins around the text",
default=False,
)
parser.add_argument(
"-fi",
"--fit",
action="store_true",
help="Apply a tight crop around the rendered text",
default=False,
)
parser.add_argument(
"-rf",
"--random_fit",
action="store_true",
help="Random decide whether to fit or not",
default=False,
)
parser.add_argument(
"-rc",
"--random_crop",
action="store_true",
help="Random crop near the margins around the text",
default=False,
)
parser.add_argument(
"-ft", "--font", type=str, nargs="?", help="Define font to be used"
)
parser.add_argument(
"-fd",
"--font_dir",
type=str,
nargs="?",
help="Define a font directory to be used",
)
parser.add_argument(
"-id",
"--image_dir",
type=str,
nargs="?",
help="Define an image directory to use when background is set to image",
default=os.path.join(os.path.split(os.path.realpath(__file__))[0], "images"),
)
parser.add_argument(
"-dt", "--dict", type=str, nargs="?", help="Define the dictionary to be used"
)
parser.add_argument(
"-ws",
"--word_split",
action="store_true",
help="Split on words instead of on characters (preserves ligatures, no character spacing)",
default=False,
)
parser.add_argument(
"-stw",
"--stroke_width",
type=int,
nargs="?",
help="Define the width of the strokes",
default=0,
)
parser.add_argument(
"-stf",
"--stroke_fill",
type=str,
nargs="?",
help="Define the color of the contour of the strokes, if stroke_width is bigger than 0",
default="#282828",
)
parser.add_argument(
"-im",
"--image_mode",
type=str,
nargs="?",
help="Define the image mode to be used",
default="RGB",
)
return parser.parse_args()
def main():
"""
Description: Main function
"""
# Argument parsing
args = parse_arguments()
# Create the directory if it does not exist.
try:
os.makedirs(args.output_dir)
except OSError as e:
if e.errno != errno.EEXIST:
raise
# Creating word list
if args.dict:
lang_dict = []
if os.path.isfile(args.dict):
with open(args.dict, "r", encoding="utf8", errors="ignore") as d:
lang_dict = [l for l in d.read().splitlines() if len(l) > 0]
else:
sys.exit("Cannot open dict")
else:
lang_dict = load_dict(args.language)
# Create font (path) list
if args.font_dir:
fonts = [
os.path.join(args.font_dir, p)
for p in os.listdir(args.font_dir)
if os.path.splitext(p)[1] == ".ttf"
]
elif args.font:
if os.path.isfile(args.font):
fonts = [args.font]
else:
sys.exit("Cannot open font")
else:
fonts = load_fonts(args.language)
# Creating synthetic sentences (or word)
strings = []
if args.use_wikipedia:
strings = create_strings_from_wikipedia(args.length, args.count, args.language)
elif args.input_file != "":
strings = create_strings_from_file(args.input_file, args.count)
elif args.random_sequences:
strings = create_strings_randomly(
args.length,
args.random,
args.count,
args.include_letters,
args.include_numbers,
args.include_symbols,
args.language,
)
# Set a name format compatible with special characters automatically if they are used
if args.include_symbols or True not in (
args.include_letters,
args.include_numbers,
args.include_symbols,
):
args.name_format = 2
else:
strings = create_strings_from_dict(
args.length, args.random, args.count, lang_dict
)
if args.max_length>0:
for i,x in enumerate(strings):
if (len(x)>args.max_length):
strings[i] = x[:len(x)//2]
else:
continue
string_count = len(strings)
p = Pool(args.thread_count)
for _ in tqdm(
p.imap_unordered(
FakeTextDataGenerator.generate_from_tuple,
zip(
[i for i in range(0, string_count)],
strings,
[fonts[rnd.randrange(0, len(fonts))] for _ in range(0, string_count)],
[args.output_dir] * string_count,
[args.height] * string_count,
[args.extension] * string_count,
[args.skew_angle] * string_count,
[args.random_skew] * string_count,
[args.blur] * string_count,
[args.random_blur] * string_count,
[args.random_shearx] * string_count,
[args.salt_and_pepper] * string_count,
[args.background] * string_count,
[args.distorsion] * string_count,
[args.distorsion_orientation] * string_count,
[args.handwritten] * string_count,
[args.name_format] * string_count,
[args.width] * string_count,
[args.alignment] * string_count,
[args.text_color] * string_count,
[args.orientation] * string_count,
[args.space_width] * string_count,
[args.character_spacing] * string_count,
[args.margins] * string_count,
[args.random_margins] * string_count,
[args.random_crop] * string_count,
[args.fit] * string_count,
[args.random_fit] * string_count,
[args.output_mask] * string_count,
[args.word_split] * string_count,
[args.image_dir] * string_count,
[args.stroke_width] * string_count,
[args.stroke_fill] * string_count,
[args.image_mode] * string_count,
[args.random_resize] * string_count,
),
),
total=args.count,
):
pass
p.terminate()
# strings = [x.replace("ﷺ","صلی اللہ علیہ وسلم") for x in strings]
if args.name_format == 2:
# Create file with filename-to-label connections
with open(
os.path.join(args.output_dir, "labels.txt"), "w", encoding="utf8"
) as f:
for i in range(string_count):
file_name = str(i) + "." + args.extension
if args.space_width == 0:
file_name = file_name.replace(" ", "")
f.write("{} {}\n".format(file_name, strings[i]))
if __name__ == "__main__":
main()