tombetthauser commited on
Commit
bf228e4
β€’
1 Parent(s): 3fdba19

Add controlnet resize before canny tracing

Browse files
Files changed (1) hide show
  1. app.py +18 -4
app.py CHANGED
@@ -480,7 +480,7 @@ def controlnet_edges(canny_input_prompt, input_image, input_low_threshold, input
480
  canny_2 = Image.fromarray(canny_1)
481
  canny_1 = Image.fromarray(canny_1)
482
 
483
- if int(canny_input_rotate) > 0:
484
  canny_rotation = 360 - int(canny_input_rotate)
485
  canny_2 = canny_2.rotate(canny_rotation, resample=Image.BICUBIC)
486
  canny_1 = canny_1.rotate(canny_rotation, resample=Image.BICUBIC)
@@ -490,28 +490,41 @@ def controlnet_edges(canny_input_prompt, input_image, input_low_threshold, input
490
  limit_size = 768
491
  # limit_size = 32
492
 
 
493
  if input_width > input_height:
494
  new_width = min(input_width, limit_size)
495
  new_height = int(new_width * input_height / input_width)
496
  else:
497
  new_height = min(input_height, limit_size)
498
  new_width = int(new_height * input_width / input_height)
499
-
500
  canny_2 = canny_2.resize((new_width, new_height))
501
  canny_1 = canny_1.resize((new_width, new_height))
502
 
 
 
 
 
 
 
 
 
 
 
 
 
 
503
  prompt = canny_input_prompt
504
  generator = torch.Generator(device="cpu").manual_seed(canny_input_seed)
505
 
506
  output_image = controlnet_pipe(
507
  prompt,
508
- canny_2,
509
  negative_prompt="monochrome, lowres, bad anatomy, worst quality, low quality",
510
  generator=generator,
511
  num_inference_steps=20,
512
  )
513
 
514
- return [canny_1, output_image[0][0]]
515
  # return output_image[0][0]
516
 
517
  import random
@@ -555,6 +568,7 @@ with gr.Blocks() as canny_blocks_interface:
555
 
556
 
557
 
 
558
  # ----- Old ControlNet Canny Gradio Setup without Block (working) -----------------------------------------------------------------
559
 
560
  # import gradio as gr
 
480
  canny_2 = Image.fromarray(canny_1)
481
  canny_1 = Image.fromarray(canny_1)
482
 
483
+ if canny_input_rotate and int(canny_input_rotate) > 0:
484
  canny_rotation = 360 - int(canny_input_rotate)
485
  canny_2 = canny_2.rotate(canny_rotation, resample=Image.BICUBIC)
486
  canny_1 = canny_1.rotate(canny_rotation, resample=Image.BICUBIC)
 
490
  limit_size = 768
491
  # limit_size = 32
492
 
493
+ # resize image
494
  if input_width > input_height:
495
  new_width = min(input_width, limit_size)
496
  new_height = int(new_width * input_height / input_width)
497
  else:
498
  new_height = min(input_height, limit_size)
499
  new_width = int(new_height * input_width / input_height)
 
500
  canny_2 = canny_2.resize((new_width, new_height))
501
  canny_1 = canny_1.resize((new_width, new_height))
502
 
503
+ # resize original input image
504
+ input_resize = np.array(input_image)
505
+ input_resize = Image.fromarray(input_resize)
506
+ input_resize = input_resize.resize((new_width, new_height))
507
+ # make canny image now, after resize
508
+ canny_resize = np.array(input_resize)
509
+ canny_resize = cv2.Canny(canny_resize, input_low_threshold, input_high_threshold)
510
+ canny_resize = canny_resize[:, :, None]
511
+ canny_resize = np.concatenate([canny_resize, canny_resize, canny_resize], axis=2)
512
+ if input_invert:
513
+ canny_resize = 255 - canny_resize
514
+ canny_resize = Image.fromarray(canny_resize)
515
+
516
  prompt = canny_input_prompt
517
  generator = torch.Generator(device="cpu").manual_seed(canny_input_seed)
518
 
519
  output_image = controlnet_pipe(
520
  prompt,
521
+ canny_resize,
522
  negative_prompt="monochrome, lowres, bad anatomy, worst quality, low quality",
523
  generator=generator,
524
  num_inference_steps=20,
525
  )
526
 
527
+ return [canny_resize, output_image[0][0]]
528
  # return output_image[0][0]
529
 
530
  import random
 
568
 
569
 
570
 
571
+
572
  # ----- Old ControlNet Canny Gradio Setup without Block (working) -----------------------------------------------------------------
573
 
574
  # import gradio as gr