Spaces:
Runtime error
Runtime error
tombetthauser
commited on
Commit
β’
e084c52
1
Parent(s):
d6f1c22
Added depthmap tab
Browse files
app.py
CHANGED
@@ -654,8 +654,167 @@ with gr.Blocks() as canny_blocks_interface:
|
|
654 |
|
655 |
|
656 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
657 |
# ----- Launch Tabs -----------------------------------------------------------------
|
658 |
|
659 |
-
tabbed_interface = gr.TabbedInterface([new_welcome, advanced_tab, beta, canny_blocks_interface], ["
|
660 |
# tabbed_interface = gr.TabbedInterface([new_welcome, advanced_tab, beta], ["Artbots", "Advanced", "Beta"])
|
661 |
tabbed_interface.launch()
|
|
|
654 |
|
655 |
|
656 |
|
657 |
+
# ----- Depth Map Tab -----------------------------------------------------------------
|
658 |
+
|
659 |
+
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel, UniPCMultistepScheduler
|
660 |
+
from controlnet_aux import CannyDetector, ContentShuffleDetector, HEDdetector, LineartAnimeDetector, LineartDetector, MidasDetector, MLSDdetector, NormalBaeDetector, OpenposeDetector, PidiNetDetector
|
661 |
+
from PIL import Image, ImageChops, ImageOps
|
662 |
+
from diffusers.utils import load_image
|
663 |
+
from transformers import pipeline
|
664 |
+
import numpy as np
|
665 |
+
import requests
|
666 |
+
import torch
|
667 |
+
import cv2
|
668 |
+
|
669 |
+
def resize_image(image, max_dimension, multiplier=16):
|
670 |
+
original_width, original_height = image.size
|
671 |
+
aspect_ratio = original_width / original_height
|
672 |
+
|
673 |
+
if original_width > original_height:
|
674 |
+
new_width = min(max_dimension, original_width)
|
675 |
+
new_height = round(new_width / aspect_ratio)
|
676 |
+
else:
|
677 |
+
new_height = min(max_dimension, original_height)
|
678 |
+
new_width = round(new_height * aspect_ratio)
|
679 |
+
|
680 |
+
new_width = round(new_width / multiplier) * multiplier
|
681 |
+
new_height = round(new_height / multiplier) * multiplier
|
682 |
+
resized_image = image.resize((new_width, new_height), Image.ANTIALIAS)
|
683 |
+
|
684 |
+
return resized_image
|
685 |
+
|
686 |
+
def depth_map_prompt(prompt, image_url, controlnet_pipe, controlnet_model, negative_prompt):
|
687 |
+
image = load_image(image_url)
|
688 |
+
|
689 |
+
max_dimension = 768
|
690 |
+
resized_image = resize_image(image, max_dimension)
|
691 |
+
|
692 |
+
depth_map = controlnet_model(resized_image)
|
693 |
+
|
694 |
+
output = controlnet_pipe(
|
695 |
+
prompt,
|
696 |
+
depth_map,
|
697 |
+
negative_prompt=negative_prompt,
|
698 |
+
generator=torch.Generator(device="cpu").manual_seed(2),
|
699 |
+
num_inference_steps=20,
|
700 |
+
)
|
701 |
+
|
702 |
+
return {"output": output.images[0], "depth_map": depth_map}
|
703 |
+
|
704 |
+
|
705 |
+
|
706 |
+
|
707 |
+
controlnet_depth = ControlNetModel.from_pretrained(
|
708 |
+
"fusing/stable-diffusion-v1-5-controlnet-depth", torch_dtype=torch.float16
|
709 |
+
)
|
710 |
+
|
711 |
+
model_id = "runwayml/stable-diffusion-v1-5"
|
712 |
+
depth_pipe = StableDiffusionControlNetPipeline.from_pretrained(
|
713 |
+
model_id,
|
714 |
+
controlnet=controlnet_depth,
|
715 |
+
torch_dtype=torch.float16,
|
716 |
+
)
|
717 |
+
|
718 |
+
depth_pipe.scheduler = UniPCMultistepScheduler.from_config(depth_pipe.scheduler.config)
|
719 |
+
depth_pipe.enable_model_cpu_offload()
|
720 |
+
depth_pipe.enable_xformers_memory_efficient_attention()
|
721 |
+
|
722 |
+
loaded_model = MidasDetector.from_pretrained("lllyasviel/ControlNet") # works
|
723 |
+
|
724 |
+
|
725 |
+
|
726 |
+
|
727 |
+
def rotate_image(image, rotation):
|
728 |
+
rotation = 360 - int(rotation)
|
729 |
+
image = image.rotate(rotation, resample=Image.BICUBIC, expand=True)
|
730 |
+
return image
|
731 |
+
|
732 |
+
def controlnet_function(input_prompt, input_image, input_negative_prompt, input_seed, input_rotate, input_invert):
|
733 |
+
pil_image = Image.fromarray(input_image)
|
734 |
+
|
735 |
+
max_dimension = 768
|
736 |
+
processed_image = resize_image(pil_image, max_dimension, 32)
|
737 |
+
|
738 |
+
# rotate image
|
739 |
+
if input_rotate and int(input_rotate) > 0:
|
740 |
+
processed_image = rotate_image(processed_image, int(input_rotate))
|
741 |
+
|
742 |
+
depth_map = loaded_model(processed_image)
|
743 |
+
|
744 |
+
if input_invert:
|
745 |
+
depth_map = np.array(depth_map)
|
746 |
+
depth_map = 255 - depth_map
|
747 |
+
depth_map = Image.fromarray(depth_map)
|
748 |
+
|
749 |
+
generator = torch.Generator(device="cpu").manual_seed(input_seed)
|
750 |
+
|
751 |
+
output = depth_pipe(
|
752 |
+
input_prompt,
|
753 |
+
depth_map,
|
754 |
+
negative_prompt=input_negative_prompt,
|
755 |
+
generator=generator,
|
756 |
+
num_inference_steps=20,
|
757 |
+
)
|
758 |
+
|
759 |
+
return_text = f'''
|
760 |
+
prompt: "{input_prompt}"
|
761 |
+
seed: {input_seed}
|
762 |
+
negative-prompt: "{input_negative_prompt}"
|
763 |
+
controlnet: "fusing/stable-diffusion-v1-5-controlnet-depth"
|
764 |
+
stable-diffusion: "runwayml/stable-diffusion-v1-5"
|
765 |
+
inverted: {input_invert}
|
766 |
+
'''
|
767 |
+
|
768 |
+
return [return_text, output.images[0], depth_map]
|
769 |
+
|
770 |
+
# import random
|
771 |
+
def random_seed():
|
772 |
+
return random.randint(0, 99999999999999)
|
773 |
+
|
774 |
+
with gr.Blocks() as depth_controlnet_gradio:
|
775 |
+
gr.Markdown('''
|
776 |
+
# <span style="display: inline-block; height: 30px; width: 30px; margin-bottom: -3px; border-radius: 7px; background-size: 50px; background-position: center; background-image: url(http://www.astronaut.horse/thumbnail.jpg)"></span> ControlNet + Depthmap
|
777 |
+
---
|
778 |
+
''')
|
779 |
+
with gr.Row():
|
780 |
+
with gr.Column():
|
781 |
+
gr.Markdown('''
|
782 |
+
## Inputs...
|
783 |
+
''')
|
784 |
+
input_prompt = gr.inputs.Textbox(label="text prompt")
|
785 |
+
input_image = gr.inputs.Image(label="input image")
|
786 |
+
with gr.Accordion(label="options", open=False):
|
787 |
+
with gr.Row():
|
788 |
+
with gr.Column():
|
789 |
+
input_negative_prompt = gr.inputs.Textbox(label="negative prompt")
|
790 |
+
with gr.Column():
|
791 |
+
input_seed = gr.Slider(0, 99999999999999, label="seed", dtype=int, value=random_seed, interactive=True, step=1)
|
792 |
+
with gr.Row():
|
793 |
+
with gr.Column():
|
794 |
+
input_rotate = gr.Dropdown([0, 90, 180, 270], label="rotate image (for smartphones)")
|
795 |
+
with gr.Column():
|
796 |
+
input_invert = gr.inputs.Checkbox(label="invert depthmap")
|
797 |
+
submit = gr.Button('generate image')
|
798 |
+
|
799 |
+
with gr.Column():
|
800 |
+
gr.Markdown('''
|
801 |
+
## Outputs...
|
802 |
+
''')
|
803 |
+
output_image = gr.Image(label="output image")
|
804 |
+
with gr.Accordion(label="depth map image", open=False):
|
805 |
+
depth_map = gr.Image(label="depth map")
|
806 |
+
output_text = gr.Textbox(label="output details")
|
807 |
+
|
808 |
+
submit.click(fn=controlnet_function, inputs=[input_prompt, input_image, input_negative_prompt, input_seed, input_rotate, input_invert], outputs=[output_text, output_image, depth_map])
|
809 |
+
|
810 |
+
# depth_controlnet_gradio.launch(debug=False)
|
811 |
+
|
812 |
+
|
813 |
+
|
814 |
+
|
815 |
+
|
816 |
# ----- Launch Tabs -----------------------------------------------------------------
|
817 |
|
818 |
+
tabbed_interface = gr.TabbedInterface([new_welcome, advanced_tab, beta, canny_blocks_interface, depth_controlnet_gradio], ["Welcome", "Advanced", "Beta", "EdgeTrace", "DepthMap"])
|
819 |
# tabbed_interface = gr.TabbedInterface([new_welcome, advanced_tab, beta], ["Artbots", "Advanced", "Beta"])
|
820 |
tabbed_interface.launch()
|