Spaces:
Runtime error
Runtime error
pablo
commited on
Commit
·
22a4ea9
1
Parent(s):
b9d1cce
3d visualization
Browse files- app.py +144 -64
- mesh.py +52 -0
- requirements.txt +2 -1
app.py
CHANGED
@@ -2,13 +2,17 @@ import gradio as gr
|
|
2 |
import torch
|
3 |
|
4 |
from diffuserslocal.src.diffusers import UNet2DConditionModel
|
5 |
-
import diffuserslocal.src.diffusers as diffusers
|
6 |
from share_btn import community_icon_html, loading_icon_html, share_js
|
7 |
from diffuserslocal.src.diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_ldm3d_inpaint import StableDiffusionLDM3DInpaintPipeline
|
8 |
from PIL import Image
|
9 |
import numpy as np
|
10 |
import cv2
|
11 |
|
|
|
|
|
|
|
|
|
|
|
12 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
13 |
|
14 |
# Inpainting pipeline
|
@@ -64,7 +68,7 @@ def read_content(file_path: str) -> str:
|
|
64 |
|
65 |
return content
|
66 |
|
67 |
-
def
|
68 |
if negative_prompt == "":
|
69 |
negative_prompt = None
|
70 |
scheduler_class_name = scheduler.split("-")[0]
|
@@ -83,13 +87,7 @@ def predict(dict, depth, prompt="", negative_prompt="", guidance_scale=7.5, step
|
|
83 |
depth_image = depth_image.astype("int32")
|
84 |
depth_image = Image.fromarray(depth_image)
|
85 |
|
86 |
-
init_image = Image.fromarray(init_image.astype("uint8"))
|
87 |
-
#init_image.save("temp_image.jpg")
|
88 |
-
|
89 |
-
#depth_image.save("temp_depth.jpg")
|
90 |
-
#scheduler = getattr(diffusers, scheduler_class_name)
|
91 |
-
#pipe.scheduler = scheduler.from_pretrained("Intel/ldm3d-4c", subfolder="scheduler")
|
92 |
-
|
93 |
|
94 |
depth_image = depth_image.resize((512, 512))
|
95 |
|
@@ -142,65 +140,147 @@ div#share-btn-container > div {flex-direction: row;background: black;align-items
|
|
142 |
'''
|
143 |
|
144 |
image_blocks = gr.Blocks(css=css, elem_id="total-container")
|
145 |
-
|
146 |
-
|
147 |
with gr.Row():
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
btn.click(fn=
|
177 |
-
prompt.submit(fn=
|
178 |
share_button.click(None, [], [], _js=share_js)
|
179 |
|
180 |
gr.Examples(
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
)
|
197 |
-
gr.HTML(
|
198 |
-
"""
|
199 |
-
<div class="footer">
|
200 |
-
<p>Model by <a href="https://huggingface.co/diffusers" style="text-decoration: underline;" target="_blank">Diffusers</a> - Gradio Demo by 🤗 Hugging Face
|
201 |
-
</p>
|
202 |
-
</div>
|
203 |
-
"""
|
204 |
)
|
205 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
206 |
image_blocks.queue(max_size=25).launch()
|
|
|
2 |
import torch
|
3 |
|
4 |
from diffuserslocal.src.diffusers import UNet2DConditionModel
|
|
|
5 |
from share_btn import community_icon_html, loading_icon_html, share_js
|
6 |
from diffuserslocal.src.diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_ldm3d_inpaint import StableDiffusionLDM3DInpaintPipeline
|
7 |
from PIL import Image
|
8 |
import numpy as np
|
9 |
import cv2
|
10 |
|
11 |
+
from functools import partial
|
12 |
+
import tempfile
|
13 |
+
|
14 |
+
from mesh import get_mesh
|
15 |
+
|
16 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
17 |
|
18 |
# Inpainting pipeline
|
|
|
68 |
|
69 |
return content
|
70 |
|
71 |
+
def predict_images(dict, depth, prompt="", negative_prompt="", guidance_scale=7.5, steps=20, strength=1.0, scheduler="EulerDiscreteScheduler"):
|
72 |
if negative_prompt == "":
|
73 |
negative_prompt = None
|
74 |
scheduler_class_name = scheduler.split("-")[0]
|
|
|
87 |
depth_image = depth_image.astype("int32")
|
88 |
depth_image = Image.fromarray(depth_image)
|
89 |
|
90 |
+
init_image = Image.fromarray(init_image.astype("uint8"))
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
|
92 |
depth_image = depth_image.resize((512, 512))
|
93 |
|
|
|
140 |
'''
|
141 |
|
142 |
image_blocks = gr.Blocks(css=css, elem_id="total-container")
|
143 |
+
|
144 |
+
def create_vis_demo():
|
145 |
with gr.Row():
|
146 |
+
with gr.Column():
|
147 |
+
image = gr.Image(source='upload', tool='sketch', elem_id="image_upload", type="numpy", label="Upload",height=400)
|
148 |
+
depth = gr.Image(source='upload', elem_id="depth_upload", type="numpy", label="Upload",height=400)
|
149 |
+
|
150 |
+
with gr.Row(elem_id="prompt-container", mobile_collapse=False, equal_height=True):
|
151 |
+
with gr.Row():
|
152 |
+
prompt = gr.Textbox(placeholder="Your prompt (what you want in place of what is erased)", show_label=False, elem_id="prompt")
|
153 |
+
btn = gr.Button("Inpaint!", elem_id="run_button")
|
154 |
+
|
155 |
+
with gr.Accordion(label="Advanced Settings", open=False):
|
156 |
+
with gr.Row(mobile_collapse=False, equal_height=True):
|
157 |
+
guidance_scale = gr.Number(value=7.5, minimum=1.0, maximum=20.0, step=0.1, label="guidance_scale")
|
158 |
+
steps = gr.Number(value=20, minimum=10, maximum=30, step=1, label="steps")
|
159 |
+
strength = gr.Number(value=0.99, minimum=0.01, maximum=0.99, step=0.01, label="strength")
|
160 |
+
negative_prompt = gr.Textbox(label="negative_prompt", placeholder="Your negative prompt", info="what you don't want to see in the image")
|
161 |
+
with gr.Row(mobile_collapse=False, equal_height=True):
|
162 |
+
schedulers = ["DEISMultistepScheduler", "HeunDiscreteScheduler", "EulerDiscreteScheduler", "DPMSolverMultistepScheduler", "DPMSolverMultistepScheduler-Karras", "DPMSolverMultistepScheduler-Karras-SDE"]
|
163 |
+
scheduler = gr.Dropdown(label="Schedulers", choices=schedulers, value="EulerDiscreteScheduler")
|
164 |
+
|
165 |
+
with gr.Column():
|
166 |
+
image_out = gr.Image(label="Output", elem_id="output-img", height=400)
|
167 |
+
depth_out = gr.Image(label="Depth", elem_id="depth-img", height=400)
|
168 |
+
|
169 |
+
with gr.Group(elem_id="share-btn-container", visible=False) as share_btn_container:
|
170 |
+
community_icon = gr.HTML(community_icon_html)
|
171 |
+
loading_icon = gr.HTML(loading_icon_html)
|
172 |
+
share_button = gr.Button("Share to community", elem_id="share-btn",visible=True)
|
173 |
+
|
174 |
+
btn.click(fn=predict_images, inputs=[image, depth, prompt, negative_prompt, guidance_scale, steps, strength, scheduler], outputs=[image_out, depth_out, share_btn_container], api_name='run')
|
175 |
+
prompt.submit(fn=predict_images, inputs=[image, depth, prompt, negative_prompt, guidance_scale, steps, strength, scheduler], outputs=[image_out, depth_out, share_btn_container])
|
176 |
share_button.click(None, [], [], _js=share_js)
|
177 |
|
178 |
gr.Examples(
|
179 |
+
examples=[
|
180 |
+
["./imgs/aaa (8).png"],
|
181 |
+
["./imgs/download (1).jpeg"],
|
182 |
+
["./imgs/0_oE0mLhfhtS_3Nfm2.png"],
|
183 |
+
["./imgs/02_HubertyBlog-1-1024x1024.jpg"],
|
184 |
+
["./imgs/jdn_jacques_de_nuce-1024x1024.jpg"],
|
185 |
+
["./imgs/c4ca473acde04280d44128ad8ee09e8a.jpg"],
|
186 |
+
["./imgs/canam-electric-motorcycles-scaled.jpg"],
|
187 |
+
["./imgs/e8717ce80b394d1b9a610d04a1decd3a.jpeg"],
|
188 |
+
["./imgs/Nature___Mountains_Big_Mountain_018453_31.jpg"],
|
189 |
+
["./imgs/Multible-sharing-room_ccexpress-2-1024x1024.jpeg"],
|
190 |
+
],
|
191 |
+
fn=predict_images,
|
192 |
+
inputs=[image],
|
193 |
+
cache_examples=False,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
194 |
)
|
195 |
|
196 |
+
|
197 |
+
def predict_images_3d(dict, depth, prompt="", negative_prompt="", guidance_scale=7.5, steps=20, strength=1.0, scheduler="EulerDiscreteScheduler", keep_edges=False):
|
198 |
+
if negative_prompt == "":
|
199 |
+
negative_prompt = None
|
200 |
+
scheduler_class_name = scheduler.split("-")[0]
|
201 |
+
|
202 |
+
init_image = cv2.resize(dict["image"], (512, 512))
|
203 |
+
|
204 |
+
mask = Image.fromarray(cv2.resize(dict["mask"], (512, 512))[:,:,0])
|
205 |
+
mask.save("temp_mask.jpg")
|
206 |
+
|
207 |
+
if (depth is None):
|
208 |
+
depth_image = estimate_depth(init_image)
|
209 |
+
|
210 |
+
else:
|
211 |
+
d_i = depth[:,:,0]
|
212 |
+
depth_image = 65535 * (d_i - np.min(d_i))/(np.max(d_i) - np.min(d_i))
|
213 |
+
depth_image = depth_image.astype("int32")
|
214 |
+
depth_image = Image.fromarray(depth_image)
|
215 |
+
|
216 |
+
init_image = Image.fromarray(init_image.astype("uint8"))
|
217 |
+
|
218 |
+
depth_image = depth_image.resize((512, 512))
|
219 |
+
|
220 |
+
output = pipe(prompt = prompt, negative_prompt=negative_prompt, image=init_image, mask_image=mask, depth_image=depth_image, guidance_scale=guidance_scale, num_inference_steps=int(steps), strength=strength)
|
221 |
+
|
222 |
+
depth_out = np.array(output.depth[0])
|
223 |
+
|
224 |
+
output_depth_vis = (depth_out - np.min(depth_out)) / (np.max(depth_out) - np.min(depth_out)) * 255
|
225 |
+
output_depth_vis = output_depth_vis.astype("uint8")
|
226 |
+
|
227 |
+
#init_image
|
228 |
+
#depth_image
|
229 |
+
output_depth = Image.fromarray(output_depth_vis)
|
230 |
+
output_image = output.rgb[0]
|
231 |
+
|
232 |
+
output_mesh = get_mesh(output_depth_vis, output_image, keep_edges=keep_edges)
|
233 |
+
input_mesh = get_mesh(np.array(depth_image),init_image, keep_edges=keep_edges)
|
234 |
+
|
235 |
+
return input_mesh, output_mesh
|
236 |
+
|
237 |
+
def create_3d_demo(model):
|
238 |
+
|
239 |
+
gr.Markdown("### Image to 3D mesh")
|
240 |
+
|
241 |
+
with gr.Column():
|
242 |
+
image = gr.Image(source='upload', tool='sketch', elem_id="image_upload", type="numpy", label="Upload",height=400)
|
243 |
+
depth = gr.Image(source='upload', elem_id="depth_upload", type="numpy", label="Upload",height=400)
|
244 |
+
checkbox = gr.Checkbox(label="Keep occlusion edges", value=False)
|
245 |
+
|
246 |
+
with gr.Row(elem_id="prompt-container", mobile_collapse=False, equal_height=True):
|
247 |
+
with gr.Row():
|
248 |
+
prompt = gr.Textbox(placeholder="Your prompt (what you want in place of what is erased)", show_label=False, elem_id="prompt")
|
249 |
+
btn = gr.Button("Inpaint!", elem_id="run_button")
|
250 |
+
|
251 |
+
with gr.Accordion(label="Advanced Settings", open=False):
|
252 |
+
with gr.Row(mobile_collapse=False, equal_height=True):
|
253 |
+
guidance_scale = gr.Number(value=7.5, minimum=1.0, maximum=20.0, step=0.1, label="guidance_scale")
|
254 |
+
steps = gr.Number(value=20, minimum=10, maximum=30, step=1, label="steps")
|
255 |
+
strength = gr.Number(value=0.99, minimum=0.01, maximum=0.99, step=0.01, label="strength")
|
256 |
+
negative_prompt = gr.Textbox(label="negative_prompt", placeholder="Your negative prompt", info="what you don't want to see in the image")
|
257 |
+
with gr.Row(mobile_collapse=False, equal_height=True):
|
258 |
+
schedulers = ["DEISMultistepScheduler", "HeunDiscreteScheduler", "EulerDiscreteScheduler", "DPMSolverMultistepScheduler", "DPMSolverMultistepScheduler-Karras", "DPMSolverMultistepScheduler-Karras-SDE"]
|
259 |
+
scheduler = gr.Dropdown(label="Schedulers", choices=schedulers, value="EulerDiscreteScheduler")
|
260 |
+
|
261 |
+
with gr.Column():
|
262 |
+
with gr.row():
|
263 |
+
result_og = gr.Model3D(label="original 3d reconstruction", clear_color=[
|
264 |
+
1.0, 1.0, 1.0, 1.0])
|
265 |
+
|
266 |
+
result_new = gr.Model3D(label="inpainted 3d reconstruction", clear_color=[
|
267 |
+
1.0, 1.0, 1.0, 1.0])
|
268 |
+
|
269 |
+
|
270 |
+
submit = gr.Button("Submit")
|
271 |
+
submit.click(fn=predict_images_3d, inputs=[image, depth, prompt, negative_prompt, guidance_scale, steps, strength, scheduler, checkbox], outputs=[image_out, depth_out, share_btn_container], api_name='run')
|
272 |
+
examples = gr.Examples(examples=["examples/aerial_beach.jpeg", "examples/mountains.jpeg", "examples/person_1.jpeg", "examples/ancient-carved.jpeg"],
|
273 |
+
inputs=[image])
|
274 |
+
|
275 |
+
|
276 |
+
|
277 |
+
with image_blocks as demo:
|
278 |
+
with gr.Tab("Image", default=True):
|
279 |
+
create_vis_demo()
|
280 |
+
with gr.Tab("3D"):
|
281 |
+
create_3d_demo()
|
282 |
+
|
283 |
+
gr.HTML(read_content("header.html"))
|
284 |
+
|
285 |
+
|
286 |
image_blocks.queue(max_size=25).launch()
|
mesh.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import trimesh
|
4 |
+
from geometry import depth_to_points, create_triangles
|
5 |
+
from functools import partial
|
6 |
+
import tempfile
|
7 |
+
|
8 |
+
|
9 |
+
def depth_edges_mask(depth):
|
10 |
+
"""Returns a mask of edges in the depth map.
|
11 |
+
Args:
|
12 |
+
depth: 2D numpy array of shape (H, W) with dtype float32.
|
13 |
+
Returns:
|
14 |
+
mask: 2D numpy array of shape (H, W) with dtype bool.
|
15 |
+
"""
|
16 |
+
# Compute the x and y gradients of the depth map.
|
17 |
+
depth_dx, depth_dy = np.gradient(depth)
|
18 |
+
# Compute the gradient magnitude.
|
19 |
+
depth_grad = np.sqrt(depth_dx ** 2 + depth_dy ** 2)
|
20 |
+
# Compute the edge mask.
|
21 |
+
mask = depth_grad > 0.05
|
22 |
+
return mask
|
23 |
+
|
24 |
+
|
25 |
+
def predict_depth(model, image):
|
26 |
+
depth = model.infer_pil(image)
|
27 |
+
return depth
|
28 |
+
|
29 |
+
def get_mesh(depth, image, keep_edges=False):
|
30 |
+
# limit the size of the input image
|
31 |
+
pts3d = depth_to_points(depth[None])
|
32 |
+
pts3d = pts3d.reshape(-1, 3)
|
33 |
+
|
34 |
+
# Create a trimesh mesh from the points
|
35 |
+
# Each pixel is connected to its 4 neighbors
|
36 |
+
# colors are the RGB values of the image
|
37 |
+
|
38 |
+
verts = pts3d.reshape(-1, 3)
|
39 |
+
image = np.array(image)
|
40 |
+
if keep_edges:
|
41 |
+
triangles = create_triangles(image.shape[0], image.shape[1])
|
42 |
+
else:
|
43 |
+
triangles = create_triangles(image.shape[0], image.shape[1], mask=~depth_edges_mask(depth))
|
44 |
+
colors = image.reshape(-1, 3)
|
45 |
+
mesh = trimesh.Trimesh(vertices=verts, faces=triangles, vertex_colors=colors)
|
46 |
+
|
47 |
+
# Save as glb
|
48 |
+
glb_file = tempfile.NamedTemporaryFile(suffix='.glb', delete=False)
|
49 |
+
glb_path = glb_file.name
|
50 |
+
mesh.export(glb_path)
|
51 |
+
return glb_path
|
52 |
+
|
requirements.txt
CHANGED
@@ -9,4 +9,5 @@ numpy
|
|
9 |
matplotlib
|
10 |
uuid
|
11 |
opencv-python
|
12 |
-
timm
|
|
|
|
9 |
matplotlib
|
10 |
uuid
|
11 |
opencv-python
|
12 |
+
timm
|
13 |
+
trimesh
|