gyrojeff commited on
Commit
e8cd6a9
1 Parent(s): 89ec64c

feat: add text generation

Browse files
Files changed (1) hide show
  1. font_dataset/layout.py +11 -16
font_dataset/layout.py CHANGED
@@ -15,7 +15,7 @@ short_ratio = 0.1
15
  median_ratio = 0.6
16
  long_ratio = 0.3
17
 
18
- from .text import CorpusGenerationConfig
19
 
20
  short_condition = CorpusGenerationConfig(
21
  min_num_line=1, max_num_line=1, min_num_char_per_line=2, max_num_char_per_line=5
@@ -242,7 +242,9 @@ def RGB2RGBA(color):
242
  return color + (255,)
243
 
244
 
245
- def generate(img_path: str, font: DSFont) -> tuple[Image.Image, FontLabel]:
 
 
246
  while True:
247
  try:
248
  im = Image.open(img_path)
@@ -274,20 +276,13 @@ def generate(img_path: str, font: DSFont) -> tuple[Image.Image, FontLabel]:
274
  else:
275
  text_direction = "ttb"
276
 
277
- # # text length
278
- # if random.random() < short_ratio:
279
- # text_length = random.randint(1, short_condition['char'])
280
- # # TODO: generate text
281
- # text = 'a' * text_length
282
- # elif random.random() < median_ratio:
283
- # text_line = random.randint(short_condition['line'], median_condition['line'])
284
- # # TODO: generate text
285
- # text = 'a\n' * text_line
286
- # else:
287
- # text_line = random.randint(median_condition['line'], long_condition['line'])
288
- # # TODO: generate text
289
- # text = 'a\n' * text_line
290
- text = "测试文本\n第二行"
291
 
292
  # text color & stroke
293
  if random.random() < gray_ratio:
 
15
  median_ratio = 0.6
16
  long_ratio = 0.3
17
 
18
+ from .text import CorpusGenerationConfig, CorpusGeneratorManager
19
 
20
  short_condition = CorpusGenerationConfig(
21
  min_num_line=1, max_num_line=1, min_num_char_per_line=2, max_num_char_per_line=5
 
242
  return color + (255,)
243
 
244
 
245
+ def generate(
246
+ img_path: str, font: DSFont, corpus_manager: CorpusGeneratorManager
247
+ ) -> tuple[Image.Image, FontLabel]:
248
  while True:
249
  try:
250
  im = Image.open(img_path)
 
276
  else:
277
  text_direction = "ttb"
278
 
279
+ # text length
280
+ if random.random() < short_ratio:
281
+ text = corpus_manager.generate(short_condition, font, render_language)
282
+ elif random.random() < median_ratio:
283
+ text = corpus_manager.generate(median_condition, font, render_language)
284
+ else:
285
+ text = corpus_manager.generate(long_condition, font, render_language)
 
 
 
 
 
 
 
286
 
287
  # text color & stroke
288
  if random.random() < gray_ratio: