sberbank-ai commited on
Commit
7d394f6
·
1 Parent(s): 4261396

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -4
app.py CHANGED
@@ -1,3 +1,6 @@
 
 
 
1
  import numpy as np
2
  import cv2
3
 
@@ -27,10 +30,66 @@ def get_text_from_image(img):
27
  return bg
28
 
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  def predict(text):
31
- imgs, texts = GENERATOR.generate(word_list=[text])
32
- image_on_white = get_text_from_image(imgs[0])
33
- return image_on_white
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
 
36
  CHAR_MAP_PATH, WEIGHTS_PATH = download_weights("sberbank-ai/scrabblegan-peter")
@@ -41,9 +100,14 @@ GENERATOR = ImgGenerator(
41
  char_map_path=CHAR_MAP_PATH
42
  )
43
 
 
 
 
 
 
44
  gr.Interface(
45
  predict,
46
- inputs=gr.Textbox(label="Type your text (RU) to generate it on an image"),
47
  outputs=gr.Image(label="Generated image"),
48
  title="Peter the Great handwritten image generation",
49
  ).launch()
 
1
+ import os
2
+ os.system("pip install git+https://github.com/ai-forever/ScrabbleGAN")
3
+
4
  import numpy as np
5
  import cv2
6
 
 
30
  return bg
31
 
32
 
33
+ def split_text_to_rows(text, n=4):
34
+ # https://stackoverflow.com/a/6187258
35
+ l = text.split()
36
+ return [' '.join(l[x:x+n]) for x in range(0, len(l), n)]
37
+
38
+
39
+ def remove_right_padding(img, len_text, char_w=32):
40
+ # char_w for a standard ScrabbleGAN char width
41
+ return img[:, :len_text*char_w]
42
+
43
+
44
+ def split_list2batches(lst, batch_size):
45
+ """Split list of images to list of bacthes."""
46
+ return [lst[i:i+batch_size] for i in range(0, len(lst), batch_size)]
47
+
48
+
49
+ def get_canvas_size(images, row_width, left_pad):
50
+ canvas_width = 0
51
+ canvas_height = 0
52
+ for image in images:
53
+ h, w = image.shape[:2]
54
+ canvas_height += h*row_width
55
+ if w > canvas_width:
56
+ canvas_width = w
57
+ canvas_width += left_pad
58
+ # expand canvas to the height of the last image
59
+ # (to correct the effect of rows shrinking)
60
+ h = images[-1].shape[0]
61
+ canvas_height += h - h*row_width
62
+ return int(canvas_height), canvas_width
63
+
64
+
65
  def predict(text):
66
+ if text.find(NEW_LINE_SYMB) == -1:
67
+ texts = split_text_to_rows(text)
68
+ else:
69
+ texts = [row.strip() for row in text.split(NEW_LINE_SYMB)]
70
+
71
+ texts_batches = split_list2batches(texts, BATCH_SIZE)
72
+
73
+ images_on_white = []
74
+ for texts_batch in texts_batches:
75
+ imgs, texts_on_image = GENERATOR.generate(word_list=texts_batch)
76
+ for img, text_on_image in zip(imgs, texts_on_image):
77
+ cropped_image = remove_right_padding(
78
+ img, len(text_on_image))
79
+ images_on_white.append(
80
+ get_text_from_image(cropped_image))
81
+
82
+ canvas_height, canvas_width = get_canvas_size(
83
+ images_on_white, ROW_WIDTH, LEFT_PAD)
84
+ canvas = np.zeros((canvas_height, canvas_width, 3), dtype=np.uint8)
85
+ canvas.fill(255)
86
+
87
+ start_draw = 0
88
+ for image_on_white in images_on_white:
89
+ h, w = image_on_white.shape[:2]
90
+ canvas[start_draw:start_draw+h, LEFT_PAD:LEFT_PAD+w] = image_on_white
91
+ start_draw += int(h * ROW_WIDTH)
92
+ return canvas
93
 
94
 
95
  CHAR_MAP_PATH, WEIGHTS_PATH = download_weights("sberbank-ai/scrabblegan-peter")
 
100
  char_map_path=CHAR_MAP_PATH
101
  )
102
 
103
+ BATCH_SIZE = 3
104
+ ROW_WIDTH = 0.7
105
+ LEFT_PAD = 10
106
+ NEW_LINE_SYMB = '{n}'
107
+
108
  gr.Interface(
109
  predict,
110
+ inputs=gr.Textbox(label=f"Type your text (RU) to generate it on an image. The text will be automatically splitted on lines, or you can use a new line symbol {NEW_LINE_SYMB}"),
111
  outputs=gr.Image(label="Generated image"),
112
  title="Peter the Great handwritten image generation",
113
  ).launch()