Spaces:
Runtime error
Runtime error
add example images
Browse files
app.py
CHANGED
|
@@ -1,12 +1,14 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
import torch
|
| 5 |
from PIL import Image
|
| 6 |
from diffusers import (
|
| 7 |
AutoencoderKL,
|
| 8 |
-
DiffusionPipeline,
|
| 9 |
-
# UNet2DConditionModel,
|
| 10 |
)
|
| 11 |
|
| 12 |
from transformers import CLIPTextModel, CLIPTokenizer
|
|
@@ -21,16 +23,8 @@ def load_example(example_image):
|
|
| 21 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 22 |
model_repo_id = "zysong212/DepthMaster" # Replace to the model you would like to use
|
| 23 |
|
| 24 |
-
# if torch.cuda.is_available():
|
| 25 |
-
# torch_dtype = torch.float16
|
| 26 |
-
# else:
|
| 27 |
torch_dtype = torch.float32
|
| 28 |
|
| 29 |
-
# pipe = DepthMasterPipeline.from_pretrained('eval', torch_dtype=torch_dtype)
|
| 30 |
-
# unet = UNet2DConditionModel.from_pretrained(os.path.join('eval', f'unet'))
|
| 31 |
-
# pipe = DepthMasterPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
|
| 32 |
-
# unet = UNet2DConditionModel.from_pretrained(model_repo_id, subfolder="unet", torch_dtype=torch_dtype)
|
| 33 |
-
# pipe.unet = unet
|
| 34 |
vae = AutoencoderKL.from_pretrained(model_repo_id, subfolder="vae", torch_dtype=torch_dtype, allow_pickle=False)
|
| 35 |
unet = UNet2DConditionModel.from_pretrained(model_repo_id, subfolder="unet", torch_dtype=torch_dtype, allow_pickle=False)
|
| 36 |
text_encoder = CLIPTextModel.from_pretrained(model_repo_id, subfolder="text_encoder", torch_dtype=torch_dtype)
|
|
@@ -45,29 +39,13 @@ except ImportError:
|
|
| 45 |
|
| 46 |
pipe = pipe.to(device)
|
| 47 |
|
| 48 |
-
# MAX_SEED = np.iinfo(np.int32).max
|
| 49 |
-
# MAX_IMAGE_SIZE = 1024
|
| 50 |
-
|
| 51 |
|
| 52 |
# @spaces.GPU #[uncomment to use ZeroGPU]
|
| 53 |
def infer(
|
| 54 |
input_image,
|
| 55 |
progress=gr.Progress(track_tqdm=True),
|
| 56 |
):
|
| 57 |
-
|
| 58 |
-
# seed = random.randint(0, MAX_SEED)
|
| 59 |
-
|
| 60 |
-
# generator = torch.Generator().manual_seed(seed)
|
| 61 |
-
|
| 62 |
-
# image = pipe(
|
| 63 |
-
# prompt=prompt,
|
| 64 |
-
# negative_prompt=negative_prompt,
|
| 65 |
-
# guidance_scale=guidance_scale,
|
| 66 |
-
# num_inference_steps=num_inference_steps,
|
| 67 |
-
# width=width,
|
| 68 |
-
# height=height,
|
| 69 |
-
# generator=generator,
|
| 70 |
-
# ).images[0]
|
| 71 |
pipe_out = pipe(
|
| 72 |
input_image,
|
| 73 |
processing_res=768,
|
|
@@ -96,155 +74,67 @@ example_images = [
|
|
| 96 |
"wild_example/sg-11134201-7rd5x-lvlh48byidbqca.jpg"
|
| 97 |
]
|
| 98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
css = """
|
| 100 |
-
#
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
}
|
| 104 |
-
#
|
| 105 |
-
height:
|
| 106 |
-
width: auto; /* 保持宽高比 */
|
| 107 |
-
margin: 0 auto; /* 图片间距 */
|
| 108 |
-
cursor: pointer; /* 鼠标指针变为手型 */
|
| 109 |
}
|
| 110 |
"""
|
| 111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
with gr.Blocks(css=css) as demo:
|
| 113 |
-
gr.Markdown(
|
| 114 |
-
gr.Markdown(
|
| 115 |
gr.Markdown(" ### Depth Estimation with DepthMaster.")
|
| 116 |
# with gr.Column(elem_id="col-container"):
|
| 117 |
# gr.Markdown(" # Depth Estimation")
|
| 118 |
with gr.Row():
|
| 119 |
with gr.Column():
|
| 120 |
-
input_image = gr.Image(label="Input Image", type="pil", elem_id="input
|
| 121 |
with gr.Column():
|
|
|
|
| 122 |
depth_map = gr.Image(label="Depth Map with Slider View", type="pil", interactive=False, elem_id="depth-map")
|
| 123 |
-
|
| 124 |
-
# 计算按钮
|
| 125 |
-
compute_button = gr.Button("Compute Depth")
|
| 126 |
-
|
| 127 |
-
# # 添加示例图片选择器
|
| 128 |
-
# with gr.Row():
|
| 129 |
-
# gr.Markdown("### example images")
|
| 130 |
-
# with gr.Row(elem_id="example-gallery"):
|
| 131 |
-
# example_gallery = gr.Gallery(
|
| 132 |
-
# label="",
|
| 133 |
-
# value=example_images,
|
| 134 |
-
# elem_id="example-gallery",
|
| 135 |
-
# show_label=False,
|
| 136 |
-
# interactive=True,
|
| 137 |
-
# columns=10
|
| 138 |
-
# )
|
| 139 |
-
|
| 140 |
-
# 设置默认图片点击后的操作
|
| 141 |
-
# example_gallery.select(
|
| 142 |
-
# fn=lambda img_path: img_path, # 回调函数:返回选择的路径
|
| 143 |
-
# inputs=[],
|
| 144 |
-
# outputs=input_image # 输出设置为 Input Image
|
| 145 |
-
# )
|
| 146 |
-
# example_gallery.click(
|
| 147 |
-
# fn=load_example, # 选择图片的回调
|
| 148 |
-
# inputs=[example_gallery], # 输入:用户点击的图片
|
| 149 |
-
# outputs=[input_image] # 输出:更新 Input Image
|
| 150 |
-
# )
|
| 151 |
|
|
|
|
|
|
|
| 152 |
|
| 153 |
# 设置计算按钮的回调
|
| 154 |
compute_button.click(
|
| 155 |
fn=infer, # 回调函数
|
| 156 |
-
inputs=input_image, # 输入
|
| 157 |
-
outputs=depth_map # 输出
|
| 158 |
)
|
| 159 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
# 启动 Gradio 应用
|
| 161 |
-
demo.launch()
|
| 162 |
-
# with gr.Column(scale=45):
|
| 163 |
-
# img_in = gr.Image(type="pil")
|
| 164 |
-
# with gr.Column(scale=45):
|
| 165 |
-
# img_out =
|
| 166 |
-
|
| 167 |
-
# with gr.Row():
|
| 168 |
-
# prompt = gr.Text(
|
| 169 |
-
# label="Prompt",
|
| 170 |
-
# show_label=False,
|
| 171 |
-
# max_lines=1,
|
| 172 |
-
# placeholder="Enter your prompt",
|
| 173 |
-
# container=False,
|
| 174 |
-
# )
|
| 175 |
-
|
| 176 |
-
# run_button = gr.Button("Run", scale=0, variant="primary")
|
| 177 |
-
|
| 178 |
-
# result = gr.Image(label="Result", show_label=False)
|
| 179 |
-
|
| 180 |
-
# with gr.Accordion("Advanced Settings", open=False):
|
| 181 |
-
# negative_prompt = gr.Text(
|
| 182 |
-
# label="Negative prompt",
|
| 183 |
-
# max_lines=1,
|
| 184 |
-
# placeholder="Enter a negative prompt",
|
| 185 |
-
# visible=False,
|
| 186 |
-
# )
|
| 187 |
-
|
| 188 |
-
# seed = gr.Slider(
|
| 189 |
-
# label="Seed",
|
| 190 |
-
# minimum=0,
|
| 191 |
-
# maximum=MAX_SEED,
|
| 192 |
-
# step=1,
|
| 193 |
-
# value=0,
|
| 194 |
-
# )
|
| 195 |
-
|
| 196 |
-
# randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 197 |
-
|
| 198 |
-
# with gr.Row():
|
| 199 |
-
# width = gr.Slider(
|
| 200 |
-
# label="Width",
|
| 201 |
-
# minimum=256,
|
| 202 |
-
# maximum=MAX_IMAGE_SIZE,
|
| 203 |
-
# step=32,
|
| 204 |
-
# value=1024, # Replace with defaults that work for your model
|
| 205 |
-
# )
|
| 206 |
-
|
| 207 |
-
# height = gr.Slider(
|
| 208 |
-
# label="Height",
|
| 209 |
-
# minimum=256,
|
| 210 |
-
# maximum=MAX_IMAGE_SIZE,
|
| 211 |
-
# step=32,
|
| 212 |
-
# value=1024, # Replace with defaults that work for your model
|
| 213 |
-
# )
|
| 214 |
-
|
| 215 |
-
# with gr.Row():
|
| 216 |
-
# guidance_scale = gr.Slider(
|
| 217 |
-
# label="Guidance scale",
|
| 218 |
-
# minimum=0.0,
|
| 219 |
-
# maximum=10.0,
|
| 220 |
-
# step=0.1,
|
| 221 |
-
# value=0.0, # Replace with defaults that work for your model
|
| 222 |
-
# )
|
| 223 |
-
|
| 224 |
-
# num_inference_steps = gr.Slider(
|
| 225 |
-
# label="Number of inference steps",
|
| 226 |
-
# minimum=1,
|
| 227 |
-
# maximum=50,
|
| 228 |
-
# step=1,
|
| 229 |
-
# value=2, # Replace with defaults that work for your model
|
| 230 |
-
# )
|
| 231 |
-
|
| 232 |
-
# gr.Examples(examples=examples, inputs=[prompt])
|
| 233 |
-
# gr.on(
|
| 234 |
-
# triggers=[run_button.click, prompt.submit],
|
| 235 |
-
# fn=infer,
|
| 236 |
-
# inputs=[
|
| 237 |
-
# prompt,
|
| 238 |
-
# negative_prompt,
|
| 239 |
-
# seed,
|
| 240 |
-
# randomize_seed,
|
| 241 |
-
# # width,
|
| 242 |
-
# # height,
|
| 243 |
-
# # guidance_scale,
|
| 244 |
-
# # num_inference_steps,
|
| 245 |
-
# ],
|
| 246 |
-
# outputs=[result, seed],
|
| 247 |
-
# )
|
| 248 |
-
|
| 249 |
-
# if __name__ == "__main__":
|
| 250 |
-
# demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
+
import spaces
|
| 4 |
+
import torch
|
| 5 |
+
import tempfile
|
| 6 |
+
from gradio_imageslider import ImageSlider
|
| 7 |
|
| 8 |
import torch
|
| 9 |
from PIL import Image
|
| 10 |
from diffusers import (
|
| 11 |
AutoencoderKL,
|
|
|
|
|
|
|
| 12 |
)
|
| 13 |
|
| 14 |
from transformers import CLIPTextModel, CLIPTokenizer
|
|
|
|
| 23 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 24 |
model_repo_id = "zysong212/DepthMaster" # Replace to the model you would like to use
|
| 25 |
|
|
|
|
|
|
|
|
|
|
| 26 |
torch_dtype = torch.float32
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
vae = AutoencoderKL.from_pretrained(model_repo_id, subfolder="vae", torch_dtype=torch_dtype, allow_pickle=False)
|
| 29 |
unet = UNet2DConditionModel.from_pretrained(model_repo_id, subfolder="unet", torch_dtype=torch_dtype, allow_pickle=False)
|
| 30 |
text_encoder = CLIPTextModel.from_pretrained(model_repo_id, subfolder="text_encoder", torch_dtype=torch_dtype)
|
|
|
|
| 39 |
|
| 40 |
pipe = pipe.to(device)
|
| 41 |
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
# @spaces.GPU #[uncomment to use ZeroGPU]
|
| 44 |
def infer(
|
| 45 |
input_image,
|
| 46 |
progress=gr.Progress(track_tqdm=True),
|
| 47 |
):
|
| 48 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
pipe_out = pipe(
|
| 50 |
input_image,
|
| 51 |
processing_res=768,
|
|
|
|
| 74 |
"wild_example/sg-11134201-7rd5x-lvlh48byidbqca.jpg"
|
| 75 |
]
|
| 76 |
|
| 77 |
+
# css = """
|
| 78 |
+
# #col-container {
|
| 79 |
+
# margin: 0 auto;
|
| 80 |
+
# max-width: 640px;
|
| 81 |
+
# }
|
| 82 |
+
# #example-gallery {
|
| 83 |
+
# height: 80px; /* 设置缩略图高度 */
|
| 84 |
+
# width: auto; /* 保持宽高比 */
|
| 85 |
+
# margin: 0 auto; /* 图片间距 */
|
| 86 |
+
# cursor: pointer; /* 鼠标指针变为手型 */
|
| 87 |
+
# }
|
| 88 |
+
# """
|
| 89 |
+
|
| 90 |
css = """
|
| 91 |
+
#img-display-container {
|
| 92 |
+
max-height: 100vh;
|
| 93 |
+
}
|
| 94 |
+
#img-display-input {
|
| 95 |
+
max-height: 80vh;
|
| 96 |
+
}
|
| 97 |
+
#img-display-output {
|
| 98 |
+
max-height: 80vh;
|
| 99 |
}
|
| 100 |
+
#download {
|
| 101 |
+
height: 62px;
|
|
|
|
|
|
|
|
|
|
| 102 |
}
|
| 103 |
"""
|
| 104 |
|
| 105 |
+
title = "# DepthMaster"
|
| 106 |
+
description = """**Official demo for DepthMaster**.
|
| 107 |
+
Please refer to our [paper](https://arxiv.org/abs/2501.02576), [project page](https://indu1ge.github.io/DepthMaster_page/), and [github](https://github.com/indu1ge/DepthMaster) for more details."""
|
| 108 |
+
|
| 109 |
+
|
| 110 |
with gr.Blocks(css=css) as demo:
|
| 111 |
+
gr.Markdown(title)
|
| 112 |
+
gr.Markdown(description)
|
| 113 |
gr.Markdown(" ### Depth Estimation with DepthMaster.")
|
| 114 |
# with gr.Column(elem_id="col-container"):
|
| 115 |
# gr.Markdown(" # Depth Estimation")
|
| 116 |
with gr.Row():
|
| 117 |
with gr.Column():
|
| 118 |
+
input_image = gr.Image(label="Input Image", type="pil", elem_id="img-display-input")
|
| 119 |
with gr.Column():
|
| 120 |
+
# depth_img_slider = ImageSlider(label="Depth Map with Slider View", elem_id="img-display-output", position=0.5)
|
| 121 |
depth_map = gr.Image(label="Depth Map with Slider View", type="pil", interactive=False, elem_id="depth-map")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
+
# 计算按钮
|
| 124 |
+
compute_button = gr.Button(value="Compute Depth")
|
| 125 |
|
| 126 |
# 设置计算按钮的回调
|
| 127 |
compute_button.click(
|
| 128 |
fn=infer, # 回调函数
|
| 129 |
+
inputs=[input_image], # 输入
|
| 130 |
+
outputs=[depth_map] # 输出
|
| 131 |
)
|
| 132 |
|
| 133 |
+
example_files = os.listdir('wild_example')
|
| 134 |
+
example_files.sort()
|
| 135 |
+
example_files = [os.path.join('wild_example', filename) for filename in example_files]
|
| 136 |
+
examples = gr.Examples(examples=example_files, inputs=[input_image], outputs=[depth_map], fn=infer)
|
| 137 |
+
|
| 138 |
+
|
| 139 |
# 启动 Gradio 应用
|
| 140 |
+
demo.queue().launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|