tobiaspires commited on
Commit
319d641
·
1 Parent(s): 50a83a4

Atualização

Browse files
app.py CHANGED
@@ -30,6 +30,8 @@ sf_neg_prompt_2 = "animal"
30
  template1 = Image.open("templates/template1.png").resize((512, 512))
31
  template2 = Image.open("templates/template2.png").resize((512, 512))
32
 
 
 
33
 
34
  def numpy_to_pil(images):
35
  if images.ndim == 3:
@@ -115,9 +117,8 @@ async def predict(prompt, negative_prompt, image, mask_img):
115
  return (img_final)
116
 
117
 
118
- async def predicts(sf_prompt_1, sf_neg_prompt_1, sf_prompt_2, sf_neg_prompt_2, image, image_numpy, mask_img):
119
- testing_local = True
120
- if testing_local:
121
  img1 = Image.fromarray(image_numpy).convert("RGB").resize((512, 512))
122
  img2 = Image.fromarray(image_numpy).convert("RGB").resize((512, 512))
123
  return img1, img2
@@ -135,21 +136,24 @@ async def predicts(sf_prompt_1, sf_neg_prompt_1, sf_prompt_2, sf_neg_prompt_2, i
135
  return img1, img2
136
 
137
 
138
- def draw_text(img, template_coords, text1, text2):
139
- font1 = ImageFont.truetype(font="fonts/arial.ttf", size=18)
140
- font2 = ImageFont.truetype(font="fonts/arial.ttf", size=14)
141
-
142
  x1 = template_coords['x1']
143
  y1 = template_coords['y1']
144
  x2 = template_coords['x2']
145
  y2 = template_coords['y2']
146
 
 
 
 
 
 
 
147
  draw = ImageDraw.Draw(img)
148
- draw.text((x1, y1), text1, fill=(255, 0, 0), font=font1)
149
- draw.text((x2, y2), text2, fill=(255, 0, 0), font=font2)
150
 
151
 
152
- def inference(obj2mask, image_numpy, txt_1, txt_2):
153
  generator = torch.Generator()
154
  generator.manual_seed(int(52362))
155
 
@@ -157,7 +161,7 @@ def inference(obj2mask, image_numpy, txt_1, txt_2):
157
 
158
  mask_img = get_mask(obj2mask, image)
159
 
160
- img1, img2 = asyncio.run(predicts(sf_prompt_1, sf_neg_prompt_1, sf_prompt_2, sf_neg_prompt_2, image, image_numpy, mask_img))
161
 
162
  img1_1 = add_template(img1.copy(), template1.copy())
163
  img1_2 = add_template(img1.copy(), template2.copy())
@@ -166,23 +170,23 @@ def inference(obj2mask, image_numpy, txt_1, txt_2):
166
  img2_2 = add_template(img2.copy(), template2.copy())
167
 
168
  template1_coords = {
169
- 'x1': 5,
170
- 'y1': 300,
171
- 'x2': 15,
172
- 'y2': 400
173
  }
174
 
175
  template2_coords = {
176
- 'x1': 5,
177
- 'y1': 300,
178
- 'x2': 15,
179
- 'y2': 400
180
  }
181
 
182
- draw_text(img1_1, template1_coords, txt_1, txt_2)
183
- draw_text(img1_2, template2_coords, txt_1, txt_2)
184
- draw_text(img2_1, template1_coords, txt_1, txt_2)
185
- draw_text(img2_2, template2_coords, txt_1, txt_2)
186
 
187
  return [img1_1, img1_2, img2_1, img2_2]
188
 
@@ -190,12 +194,13 @@ def inference(obj2mask, image_numpy, txt_1, txt_2):
190
  with gr.Blocks() as demo:
191
  with gr.Row():
192
  with gr.Column():
193
- txt_1 = gr.Textbox(label="Texto principal da propaganda", value="Promoção Imperdível")
194
- txt_2 = gr.Textbox(label="Texto secundário da propaganda", value="Até 50% para alguns produtos")
195
 
196
  mask = gr.Textbox(label="Descrição da imagem", value="shoe")
197
  intput_img = gr.Image()
198
- run = gr.Button(value="Generate")
 
199
  with gr.Row():
200
  with gr.Column():
201
  output_img1_1 = gr.Image()
@@ -212,7 +217,7 @@ with gr.Blocks() as demo:
212
 
213
  run.click(
214
  inference,
215
- inputs=[mask, intput_img, txt_1, txt_2],
216
  outputs=[output_img1_1, output_img1_2, output_img2_1, output_img2_2],
217
  )
218
 
 
30
  template1 = Image.open("templates/template1.png").resize((512, 512))
31
  template2 = Image.open("templates/template2.png").resize((512, 512))
32
 
33
+ fontMain = ImageFont.truetype(font="fonts/arial.ttf", size=32)
34
+ fontSecond = ImageFont.truetype(font="fonts/arial.ttf", size=18)
35
 
36
  def numpy_to_pil(images):
37
  if images.ndim == 3:
 
117
  return (img_final)
118
 
119
 
120
+ async def predicts(sf_prompt_1, sf_neg_prompt_1, sf_prompt_2, sf_neg_prompt_2, image, image_numpy, mask_img, only_test):
121
+ if only_test:
 
122
  img1 = Image.fromarray(image_numpy).convert("RGB").resize((512, 512))
123
  img2 = Image.fromarray(image_numpy).convert("RGB").resize((512, 512))
124
  return img1, img2
 
136
  return img1, img2
137
 
138
 
139
+ def draw_text(img, template_coords, main_text, second_text):
 
 
 
140
  x1 = template_coords['x1']
141
  y1 = template_coords['y1']
142
  x2 = template_coords['x2']
143
  y2 = template_coords['y2']
144
 
145
+ if '\\n' in main_text:
146
+ main_text = main_text.replace('\\n', '\n')
147
+
148
+ if '\\n' in second_text:
149
+ second_text = second_text.replace('\\n', '\n')
150
+
151
  draw = ImageDraw.Draw(img)
152
+ draw.text((x1, y1), main_text, fill=(255, 255, 255), font=fontMain)
153
+ draw.text((x2, y2), second_text, fill=(255, 255, 255), font=fontSecond)
154
 
155
 
156
+ def inference(obj2mask, image_numpy, main_text, second_text, only_test):
157
  generator = torch.Generator()
158
  generator.manual_seed(int(52362))
159
 
 
161
 
162
  mask_img = get_mask(obj2mask, image)
163
 
164
+ img1, img2 = asyncio.run(predicts(sf_prompt_1, sf_neg_prompt_1, sf_prompt_2, sf_neg_prompt_2, image, image_numpy, mask_img, only_test))
165
 
166
  img1_1 = add_template(img1.copy(), template1.copy())
167
  img1_2 = add_template(img1.copy(), template2.copy())
 
170
  img2_2 = add_template(img2.copy(), template2.copy())
171
 
172
  template1_coords = {
173
+ 'x1': 700/2,
174
+ 'y1': 630/2,
175
+ 'x2': 420/2,
176
+ 'y2': 800/2
177
  }
178
 
179
  template2_coords = {
180
+ 'x1': 30/2,
181
+ 'y1': 800/2,
182
+ 'x2': 430/2,
183
+ 'y2': 780/2
184
  }
185
 
186
+ draw_text(img1_1, template1_coords, main_text, second_text)
187
+ draw_text(img1_2, template2_coords, main_text, second_text)
188
+ draw_text(img2_1, template1_coords, main_text, second_text)
189
+ draw_text(img2_2, template2_coords, main_text, second_text)
190
 
191
  return [img1_1, img1_2, img2_1, img2_2]
192
 
 
194
  with gr.Blocks() as demo:
195
  with gr.Row():
196
  with gr.Column():
197
+ txt_1 = gr.Textbox(label="Texto principal da propaganda", value="Promoção\nImperdível", lines=2)
198
+ txt_2 = gr.Textbox(label="Texto secundário da propaganda", value="Até 50% para alguns produtos\nEntre em contato com um dos\nnossos vendedores", lines=3)
199
 
200
  mask = gr.Textbox(label="Descrição da imagem", value="shoe")
201
  intput_img = gr.Image()
202
+ run = gr.Button(value="Gerar")
203
+ chk_test = gr.Checkbox(label='Gerar Prévia')
204
  with gr.Row():
205
  with gr.Column():
206
  output_img1_1 = gr.Image()
 
217
 
218
  run.click(
219
  inference,
220
+ inputs=[mask, intput_img, txt_1, txt_2, chk_test],
221
  outputs=[output_img1_1, output_img1_2, output_img2_1, output_img2_2],
222
  )
223
 
templates/template1.png CHANGED
templates/template2.png CHANGED
templates/template_orig.png ADDED