tombetthauser commited on
Commit
211a2dd
Β·
1 Parent(s): bf228e4

Fix controlnet rotation cropping

Browse files
Files changed (1) hide show
  1. app.py +21 -5
app.py CHANGED
@@ -446,6 +446,14 @@ canny_interface = gr.Interface(fn=canny_process_image, inputs=[canny_input_image
446
  # ----- New ControlNet Canny Gradio Setup with Block -----------------------------------------------------------------
447
 
448
 
 
 
 
 
 
 
 
 
449
  from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
450
  from diffusers import UniPCMultistepScheduler
451
  from PIL import Image
@@ -463,7 +471,7 @@ controlnet_pipe.scheduler = UniPCMultistepScheduler.from_config(controlnet_pipe.
463
  controlnet_pipe.enable_model_cpu_offload()
464
 
465
 
466
- def controlnet_edges(canny_input_prompt, input_image, input_low_threshold, input_high_threshold, input_invert, canny_input_seed, canny_input_rotate):
467
  np_image = np.array(input_image)
468
 
469
  output_image = input_image
@@ -512,6 +520,10 @@ def controlnet_edges(canny_input_prompt, input_image, input_low_threshold, input
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)
@@ -519,7 +531,7 @@ def controlnet_edges(canny_input_prompt, input_image, input_low_threshold, input
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
  )
@@ -546,11 +558,13 @@ with gr.Blocks() as canny_blocks_interface:
546
  ''')
547
  with gr.Row():
548
  with gr.Column():
549
- canny_input_prompt = gr.inputs.Textbox(label="Enter a single word or phrase")
 
 
550
  canny_input_low_threshold = gr.inputs.Slider(minimum=0, maximum=1000, step=1, label="Lower Threshold:", default=100)
551
  canny_input_high_threshold = gr.inputs.Slider(minimum=0, maximum=1000, step=1, label="Upper Threshold:", default=120)
552
  canny_input_seed = gr.Slider(0, 99999999999999, label="seed", dtype=int, value=random_seed, interactive=True, step=1)
553
- canny_input_invert = gr.inputs.Checkbox(label="Invert Image")
554
  canny_input_rotate = gr.Dropdown([0, 90, 180, 270], label="rotate image (for smartphones)")
555
  with gr.Column():
556
  canny_input_image = gr.inputs.Image(label="input image")
@@ -560,7 +574,7 @@ with gr.Blocks() as canny_blocks_interface:
560
  canny_output_1 = gr.outputs.Image(type="pil", label="traced edges")
561
  with gr.Row():
562
  canny_output_2 = gr.outputs.Image(type="pil", label="final image")
563
- go_button.click(fn=controlnet_edges, inputs=[canny_input_prompt, canny_input_image, canny_input_low_threshold, canny_input_high_threshold, canny_input_invert, canny_input_seed, canny_input_rotate], outputs=[canny_output_1, canny_output_2])
564
 
565
 
566
  # canny_blocks_interface.launch(debug=False)
@@ -569,6 +583,8 @@ with gr.Blocks() as canny_blocks_interface:
569
 
570
 
571
 
 
 
572
  # ----- Old ControlNet Canny Gradio Setup without Block (working) -----------------------------------------------------------------
573
 
574
  # import gradio as gr
 
446
  # ----- New ControlNet Canny Gradio Setup with Block -----------------------------------------------------------------
447
 
448
 
449
+ # !pip install -qq diffusers==0.14.0 transformers xformers git+https://github.com/huggingface/accelerate.git
450
+ # !pip install -qq opencv-contrib-python
451
+ # !pip install -qq controlnet_aux
452
+ # !pip install -qq opencv-python
453
+ # !pip install -qq gradio
454
+ # !pip install -qq Pillow
455
+ # !pip install -qq numpy
456
+
457
  from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
458
  from diffusers import UniPCMultistepScheduler
459
  from PIL import Image
 
471
  controlnet_pipe.enable_model_cpu_offload()
472
 
473
 
474
+ def controlnet_edges(canny_input_prompt, input_image, input_low_threshold, input_high_threshold, input_invert, canny_input_seed, canny_input_rotate, canny_negative_prompt):
475
  np_image = np.array(input_image)
476
 
477
  output_image = input_image
 
520
  if input_invert:
521
  canny_resize = 255 - canny_resize
522
  canny_resize = Image.fromarray(canny_resize)
523
+ # rotate new resized canny image
524
+ if canny_input_rotate and int(canny_input_rotate) > 0:
525
+ canny_rotation = 360 - int(canny_input_rotate)
526
+ canny_resize = canny_resize.rotate(canny_rotation, resample=Image.BICUBIC, expand=True)
527
 
528
  prompt = canny_input_prompt
529
  generator = torch.Generator(device="cpu").manual_seed(canny_input_seed)
 
531
  output_image = controlnet_pipe(
532
  prompt,
533
  canny_resize,
534
+ negative_prompt=canny_negative_prompt,
535
  generator=generator,
536
  num_inference_steps=20,
537
  )
 
558
  ''')
559
  with gr.Row():
560
  with gr.Column():
561
+ canny_input_prompt = gr.inputs.Textbox(label="enter your text prompt here")
562
+ with gr.Accordion(label='negative prompt (optional)', open=False):
563
+ canny_negative_prompt = gr.inputs.Textbox()
564
  canny_input_low_threshold = gr.inputs.Slider(minimum=0, maximum=1000, step=1, label="Lower Threshold:", default=100)
565
  canny_input_high_threshold = gr.inputs.Slider(minimum=0, maximum=1000, step=1, label="Upper Threshold:", default=120)
566
  canny_input_seed = gr.Slider(0, 99999999999999, label="seed", dtype=int, value=random_seed, interactive=True, step=1)
567
+ canny_input_invert = gr.inputs.Checkbox(label="invert edge tracing image")
568
  canny_input_rotate = gr.Dropdown([0, 90, 180, 270], label="rotate image (for smartphones)")
569
  with gr.Column():
570
  canny_input_image = gr.inputs.Image(label="input image")
 
574
  canny_output_1 = gr.outputs.Image(type="pil", label="traced edges")
575
  with gr.Row():
576
  canny_output_2 = gr.outputs.Image(type="pil", label="final image")
577
+ go_button.click(fn=controlnet_edges, inputs=[canny_input_prompt, canny_input_image, canny_input_low_threshold, canny_input_high_threshold, canny_input_invert, canny_input_seed, canny_input_rotate, canny_negative_prompt], outputs=[canny_output_1, canny_output_2])
578
 
579
 
580
  # canny_blocks_interface.launch(debug=False)
 
583
 
584
 
585
 
586
+
587
+
588
  # ----- Old ControlNet Canny Gradio Setup without Block (working) -----------------------------------------------------------------
589
 
590
  # import gradio as gr