linoyts HF staff commited on
Commit
2de91e5
1 Parent(s): 4ce2c27

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -21
app.py CHANGED
@@ -180,19 +180,20 @@ def run(
180
  new_width, new_height = int(width * ratio), int(height * ratio)
181
  image = image['composite'].resize((new_width, new_height))
182
 
183
- if use_hed:
184
- controlnet_img = processor(image, scribble=False)
 
 
185
  # following is some processing to simulate human sketch draw, different threshold can generate different width of lines
186
- controlnet_img = np.array(controlnet_img)
187
- controlnet_img = nms(controlnet_img, 127, 3)
188
- controlnet_img = cv2.GaussianBlur(controlnet_img, (0, 0), 3)
189
-
190
- # higher threshold, thiner line
191
- random_val = int(round(random.uniform(0.01, 0.10), 2) * 255)
192
- controlnet_img[controlnet_img > random_val] = 255
193
- controlnet_img[controlnet_img < 255] = 0
194
- image = Image.fromarray(controlnet_img)
195
-
196
 
197
 
198
  prompt, negative_prompt = apply_style(style_name, prompt, negative_prompt)
@@ -210,7 +211,7 @@ def run(
210
  height=new_height,
211
  ).images[0]
212
 
213
- return out
214
 
215
 
216
  with gr.Blocks(css="style.css") as demo:
@@ -227,7 +228,7 @@ with gr.Blocks(css="style.css") as demo:
227
  image = gr.ImageEditor(type="pil", image_mode="L", crop_size=(512, 512),brush=gr.Brush(color_mode="fixed", colors=["#00000"]))
228
  prompt = gr.Textbox(label="Prompt")
229
  style = gr.Dropdown(label="Style", choices=STYLE_NAMES, value=DEFAULT_STYLE_NAME)
230
- use_hed = gr.Checkbox(label="use HED detector", value=False)
231
  run_button = gr.Button("Run")
232
  with gr.Accordion("Advanced options", open=False):
233
  negative_prompt = gr.Textbox(
@@ -265,7 +266,11 @@ with gr.Blocks(css="style.css") as demo:
265
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
266
 
267
  with gr.Column():
268
- result = gr.Image(label="Result", height=400)
 
 
 
 
269
 
270
  inputs = [
271
  image,
@@ -278,16 +283,17 @@ with gr.Blocks(css="style.css") as demo:
278
  seed,
279
  use_hed,
280
  ]
 
281
  prompt.submit(
282
  fn=randomize_seed_fn,
283
  inputs=[seed, randomize_seed],
284
  outputs=seed,
285
  queue=False,
286
  api_name=False,
287
- ).then(
288
  fn=run,
289
  inputs=inputs,
290
- outputs=result,
291
  api_name=False,
292
  )
293
  negative_prompt.submit(
@@ -296,10 +302,10 @@ with gr.Blocks(css="style.css") as demo:
296
  outputs=seed,
297
  queue=False,
298
  api_name=False,
299
- ).then(
300
  fn=run,
301
  inputs=inputs,
302
- outputs=result,
303
  api_name=False,
304
  )
305
  run_button.click(
@@ -308,10 +314,10 @@ with gr.Blocks(css="style.css") as demo:
308
  outputs=seed,
309
  queue=False,
310
  api_name=False,
311
- ).then(
312
  fn=run,
313
  inputs=inputs,
314
- outputs=result,
315
  api_name=False,
316
  )
317
 
 
180
  new_width, new_height = int(width * ratio), int(height * ratio)
181
  image = image['composite'].resize((new_width, new_height))
182
 
183
+ if not use_hed:
184
+ controlnet_img = image
185
+ else:
186
+ controlnet_img = processor(image, scribble=False)
187
  # following is some processing to simulate human sketch draw, different threshold can generate different width of lines
188
+ controlnet_img = np.array(controlnet_img)
189
+ controlnet_img = nms(controlnet_img, 127, 3)
190
+ controlnet_img = cv2.GaussianBlur(controlnet_img, (0, 0), 3)
191
+
192
+ # higher threshold, thiner line
193
+ random_val = int(round(random.uniform(0.01, 0.10), 2) * 255)
194
+ controlnet_img[controlnet_img > random_val] = 255
195
+ controlnet_img[controlnet_img < 255] = 0
196
+ image = Image.fromarray(controlnet_img)
 
197
 
198
 
199
  prompt, negative_prompt = apply_style(style_name, prompt, negative_prompt)
 
211
  height=new_height,
212
  ).images[0]
213
 
214
+ return out, controlnet_img
215
 
216
 
217
  with gr.Blocks(css="style.css") as demo:
 
228
  image = gr.ImageEditor(type="pil", image_mode="L", crop_size=(512, 512),brush=gr.Brush(color_mode="fixed", colors=["#00000"]))
229
  prompt = gr.Textbox(label="Prompt")
230
  style = gr.Dropdown(label="Style", choices=STYLE_NAMES, value=DEFAULT_STYLE_NAME)
231
+ use_hed = gr.Checkbox(label="use HED detector", value=False, info="check this box if you upload an image instead of sketching")
232
  run_button = gr.Button("Run")
233
  with gr.Accordion("Advanced options", open=False):
234
  negative_prompt = gr.Textbox(
 
266
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
267
 
268
  with gr.Column():
269
+ with gr.Group():
270
+ image_slider = ImageSlider(position=0.5)
271
+ with gr.Row():
272
+ result = gr.Image(label="result", height=400)
273
+ sketch_image = gr.Image(label="sketch")
274
 
275
  inputs = [
276
  image,
 
283
  seed,
284
  use_hed,
285
  ]
286
+
287
  prompt.submit(
288
  fn=randomize_seed_fn,
289
  inputs=[seed, randomize_seed],
290
  outputs=seed,
291
  queue=False,
292
  api_name=False,
293
+ ).then(lambda x: None, inputs=None, outputs=image_slider).then(
294
  fn=run,
295
  inputs=inputs,
296
+ outputs=[result, sketch_image],
297
  api_name=False,
298
  )
299
  negative_prompt.submit(
 
302
  outputs=seed,
303
  queue=False,
304
  api_name=False,
305
+ ).then(lambda x: None, inputs=None, outputs=image_slider).then(
306
  fn=run,
307
  inputs=inputs,
308
+ outputs=[result, sketch_image],
309
  api_name=False,
310
  )
311
  run_button.click(
 
314
  outputs=seed,
315
  queue=False,
316
  api_name=False,
317
+ ).then(lambda x: None, inputs=None, outputs=image_slider).then(
318
  fn=run,
319
  inputs=inputs,
320
+ outputs=[result, sketch_image],
321
  api_name=False,
322
  )
323