Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -32,34 +32,9 @@ def end_session(req: gr.Request):
|
|
| 32 |
|
| 33 |
|
| 34 |
def preprocess_image(image: Image.Image) -> Image.Image:
|
| 35 |
-
"""
|
| 36 |
-
Preprocess the input image.
|
| 37 |
-
|
| 38 |
-
Args:
|
| 39 |
-
image (Image.Image): The input image.
|
| 40 |
-
|
| 41 |
-
Returns:
|
| 42 |
-
Image.Image: The preprocessed image.
|
| 43 |
-
"""
|
| 44 |
processed_image = pipeline.preprocess_image(image)
|
| 45 |
return processed_image
|
| 46 |
|
| 47 |
-
|
| 48 |
-
def preprocess_images(images: List[Tuple[Image.Image, str]]) -> List[Image.Image]:
|
| 49 |
-
"""
|
| 50 |
-
Preprocess a list of input images.
|
| 51 |
-
|
| 52 |
-
Args:
|
| 53 |
-
images (List[Tuple[Image.Image, str]]): The input images.
|
| 54 |
-
|
| 55 |
-
Returns:
|
| 56 |
-
List[Image.Image]: The preprocessed images.
|
| 57 |
-
"""
|
| 58 |
-
images = [image[0] for image in images]
|
| 59 |
-
processed_images = [pipeline.preprocess_image(image) for image in images]
|
| 60 |
-
return processed_images
|
| 61 |
-
|
| 62 |
-
|
| 63 |
def pack_state(gs: Gaussian, mesh: MeshExtractResult) -> dict:
|
| 64 |
return {
|
| 65 |
'gaussian': {
|
|
@@ -110,66 +85,29 @@ def get_seed(randomize_seed: bool, seed: int) -> int:
|
|
| 110 |
@spaces.GPU
|
| 111 |
def image_to_3d(
|
| 112 |
image: Image.Image,
|
| 113 |
-
multiimages: List[Tuple[Image.Image, str]],
|
| 114 |
-
is_multiimage: bool,
|
| 115 |
seed: int,
|
| 116 |
ss_guidance_strength: float,
|
| 117 |
ss_sampling_steps: int,
|
| 118 |
slat_guidance_strength: float,
|
| 119 |
slat_sampling_steps: int,
|
| 120 |
-
multiimage_algo: Literal["multidiffusion", "stochastic"],
|
| 121 |
req: gr.Request,
|
| 122 |
) -> Tuple[dict, str]:
|
| 123 |
-
"""
|
| 124 |
-
Convert an image to a 3D model.
|
| 125 |
-
|
| 126 |
-
Args:
|
| 127 |
-
image (Image.Image): The input image.
|
| 128 |
-
multiimages (List[Tuple[Image.Image, str]]): The input images in multi-image mode.
|
| 129 |
-
is_multiimage (bool): Whether is in multi-image mode.
|
| 130 |
-
seed (int): The random seed.
|
| 131 |
-
ss_guidance_strength (float): The guidance strength for sparse structure generation.
|
| 132 |
-
ss_sampling_steps (int): The number of sampling steps for sparse structure generation.
|
| 133 |
-
slat_guidance_strength (float): The guidance strength for structured latent generation.
|
| 134 |
-
slat_sampling_steps (int): The number of sampling steps for structured latent generation.
|
| 135 |
-
multiimage_algo (Literal["multidiffusion", "stochastic"]): The algorithm for multi-image generation.
|
| 136 |
-
|
| 137 |
-
Returns:
|
| 138 |
-
dict: The information of the generated 3D model.
|
| 139 |
-
str: The path to the video of the 3D model.
|
| 140 |
-
"""
|
| 141 |
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
else:
|
| 158 |
-
outputs = pipeline.run_multi_image(
|
| 159 |
-
[image[0] for image in multiimages],
|
| 160 |
-
seed=seed,
|
| 161 |
-
formats=["gaussian", "mesh"],
|
| 162 |
-
preprocess_image=False,
|
| 163 |
-
sparse_structure_sampler_params={
|
| 164 |
-
"steps": ss_sampling_steps,
|
| 165 |
-
"cfg_strength": ss_guidance_strength,
|
| 166 |
-
},
|
| 167 |
-
slat_sampler_params={
|
| 168 |
-
"steps": slat_sampling_steps,
|
| 169 |
-
"cfg_strength": slat_guidance_strength,
|
| 170 |
-
},
|
| 171 |
-
mode=multiimage_algo,
|
| 172 |
-
)
|
| 173 |
video = render_utils.render_video(outputs['gaussian'][0], num_frames=120)['color']
|
| 174 |
video_geo = render_utils.render_video(outputs['mesh'][0], num_frames=120)['normal']
|
| 175 |
video = [np.concatenate([video[i], video_geo[i]], axis=1) for i in range(len(video))]
|
|
@@ -187,17 +125,7 @@ def extract_glb(
|
|
| 187 |
texture_size: int,
|
| 188 |
req: gr.Request,
|
| 189 |
) -> Tuple[str, str]:
|
| 190 |
-
"""
|
| 191 |
-
Extract a GLB file from the 3D model.
|
| 192 |
-
|
| 193 |
-
Args:
|
| 194 |
-
state (dict): The state of the generated 3D model.
|
| 195 |
-
mesh_simplify (float): The mesh simplification factor.
|
| 196 |
-
texture_size (int): The texture resolution.
|
| 197 |
|
| 198 |
-
Returns:
|
| 199 |
-
str: The path to the extracted GLB file.
|
| 200 |
-
"""
|
| 201 |
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
| 202 |
gs, mesh = unpack_state(state)
|
| 203 |
glb = postprocessing_utils.to_glb(gs, mesh, simplify=mesh_simplify, texture_size=texture_size, verbose=False)
|
|
@@ -209,15 +137,7 @@ def extract_glb(
|
|
| 209 |
|
| 210 |
@spaces.GPU
|
| 211 |
def extract_gaussian(state: dict, req: gr.Request) -> Tuple[str, str]:
|
| 212 |
-
"""
|
| 213 |
-
Extract a Gaussian file from the 3D model.
|
| 214 |
-
|
| 215 |
-
Args:
|
| 216 |
-
state (dict): The state of the generated 3D model.
|
| 217 |
|
| 218 |
-
Returns:
|
| 219 |
-
str: The path to the extracted Gaussian file.
|
| 220 |
-
"""
|
| 221 |
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
| 222 |
gs, _ = unpack_state(state)
|
| 223 |
gaussian_path = os.path.join(user_dir, 'sample.ply')
|
|
@@ -225,21 +145,6 @@ def extract_gaussian(state: dict, req: gr.Request) -> Tuple[str, str]:
|
|
| 225 |
torch.cuda.empty_cache()
|
| 226 |
return gaussian_path, gaussian_path
|
| 227 |
|
| 228 |
-
|
| 229 |
-
def prepare_multi_example() -> List[Image.Image]:
|
| 230 |
-
multi_case = list(set([i.split('_')[0] for i in os.listdir("assets/example_multi_image")]))
|
| 231 |
-
images = []
|
| 232 |
-
for case in multi_case:
|
| 233 |
-
_images = []
|
| 234 |
-
for i in range(1, 4):
|
| 235 |
-
img = Image.open(f'assets/example_multi_image/{case}_{i}.png')
|
| 236 |
-
W, H = img.size
|
| 237 |
-
img = img.resize((int(W / H * 512), 512))
|
| 238 |
-
_images.append(np.array(img))
|
| 239 |
-
images.append(Image.fromarray(np.concatenate(_images, axis=1)))
|
| 240 |
-
return images
|
| 241 |
-
|
| 242 |
-
|
| 243 |
def split_image(image: Image.Image) -> List[Image.Image]:
|
| 244 |
"""
|
| 245 |
Split an image into multiple views.
|
|
@@ -269,14 +174,7 @@ with gr.Blocks(delete_cache=(600, 600)) as demo:
|
|
| 269 |
with gr.Tabs() as input_tabs:
|
| 270 |
with gr.Tab(label="Single Image", id=0) as single_image_input_tab:
|
| 271 |
image_prompt = gr.Image(label="Image Prompt", format="png", image_mode="RGBA", type="pil", height=300)
|
| 272 |
-
|
| 273 |
-
multiimage_prompt = gr.Gallery(label="Image Prompt", format="png", type="pil", height=300, columns=3)
|
| 274 |
-
gr.Markdown("""
|
| 275 |
-
Input different views of the object in separate images.
|
| 276 |
-
|
| 277 |
-
*NOTE: this is an experimental algorithm without training a specialized model. It may not produce the best results for all images, especially those having different poses or inconsistent details.*
|
| 278 |
-
""")
|
| 279 |
-
|
| 280 |
with gr.Accordion(label="Generation Settings", open=False):
|
| 281 |
seed = gr.Slider(0, MAX_SEED, label="Seed", value=0, step=1)
|
| 282 |
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
|
|
@@ -288,8 +186,7 @@ with gr.Blocks(delete_cache=(600, 600)) as demo:
|
|
| 288 |
with gr.Row():
|
| 289 |
slat_guidance_strength = gr.Slider(0.0, 10.0, label="Guidance Strength", value=3.0, step=0.1)
|
| 290 |
slat_sampling_steps = gr.Slider(1, 50, label="Sampling Steps", value=12, step=1)
|
| 291 |
-
|
| 292 |
-
|
| 293 |
generate_btn = gr.Button("Generate")
|
| 294 |
|
| 295 |
with gr.Accordion(label="GLB Extraction Settings", open=False):
|
|
@@ -311,7 +208,6 @@ with gr.Blocks(delete_cache=(600, 600)) as demo:
|
|
| 311 |
download_glb = gr.DownloadButton(label="Download GLB", interactive=False)
|
| 312 |
download_gs = gr.DownloadButton(label="Download Gaussian", interactive=False)
|
| 313 |
|
| 314 |
-
is_multiimage = gr.State(False)
|
| 315 |
output_buf = gr.State()
|
| 316 |
|
| 317 |
# Example images at the bottom of the page
|
|
@@ -327,15 +223,6 @@ with gr.Blocks(delete_cache=(600, 600)) as demo:
|
|
| 327 |
run_on_click=True,
|
| 328 |
examples_per_page=64,
|
| 329 |
)
|
| 330 |
-
with gr.Row(visible=False) as multiimage_example:
|
| 331 |
-
examples_multi = gr.Examples(
|
| 332 |
-
examples=prepare_multi_example(),
|
| 333 |
-
inputs=[image_prompt],
|
| 334 |
-
fn=split_image,
|
| 335 |
-
outputs=[multiimage_prompt],
|
| 336 |
-
run_on_click=True,
|
| 337 |
-
examples_per_page=8,
|
| 338 |
-
)
|
| 339 |
|
| 340 |
# Handlers
|
| 341 |
demo.load(start_session)
|
|
@@ -345,21 +232,12 @@ with gr.Blocks(delete_cache=(600, 600)) as demo:
|
|
| 345 |
lambda: tuple([False, gr.Row.update(visible=True), gr.Row.update(visible=False)]),
|
| 346 |
outputs=[is_multiimage, single_image_example, multiimage_example]
|
| 347 |
)
|
| 348 |
-
multiimage_input_tab.select(
|
| 349 |
-
lambda: tuple([True, gr.Row.update(visible=False), gr.Row.update(visible=True)]),
|
| 350 |
-
outputs=[is_multiimage, single_image_example, multiimage_example]
|
| 351 |
-
)
|
| 352 |
|
| 353 |
image_prompt.upload(
|
| 354 |
preprocess_image,
|
| 355 |
inputs=[image_prompt],
|
| 356 |
outputs=[image_prompt],
|
| 357 |
)
|
| 358 |
-
multiimage_prompt.upload(
|
| 359 |
-
preprocess_images,
|
| 360 |
-
inputs=[multiimage_prompt],
|
| 361 |
-
outputs=[multiimage_prompt],
|
| 362 |
-
)
|
| 363 |
|
| 364 |
generate_btn.click(
|
| 365 |
get_seed,
|
|
@@ -367,7 +245,7 @@ with gr.Blocks(delete_cache=(600, 600)) as demo:
|
|
| 367 |
outputs=[seed],
|
| 368 |
).then(
|
| 369 |
image_to_3d,
|
| 370 |
-
inputs=[image_prompt,
|
| 371 |
outputs=[output_buf, video_output],
|
| 372 |
).then(
|
| 373 |
lambda: tuple([gr.Button(interactive=True), gr.Button(interactive=True)]),
|
|
|
|
| 32 |
|
| 33 |
|
| 34 |
def preprocess_image(image: Image.Image) -> Image.Image:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
processed_image = pipeline.preprocess_image(image)
|
| 36 |
return processed_image
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
def pack_state(gs: Gaussian, mesh: MeshExtractResult) -> dict:
|
| 39 |
return {
|
| 40 |
'gaussian': {
|
|
|
|
| 85 |
@spaces.GPU
|
| 86 |
def image_to_3d(
|
| 87 |
image: Image.Image,
|
|
|
|
|
|
|
| 88 |
seed: int,
|
| 89 |
ss_guidance_strength: float,
|
| 90 |
ss_sampling_steps: int,
|
| 91 |
slat_guidance_strength: float,
|
| 92 |
slat_sampling_steps: int,
|
|
|
|
| 93 |
req: gr.Request,
|
| 94 |
) -> Tuple[dict, str]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
| 96 |
+
outputs = pipeline.run(
|
| 97 |
+
image,
|
| 98 |
+
seed=seed,
|
| 99 |
+
formats=["gaussian", "mesh"],
|
| 100 |
+
preprocess_image=False,
|
| 101 |
+
sparse_structure_sampler_params={
|
| 102 |
+
"steps": ss_sampling_steps,
|
| 103 |
+
"cfg_strength": ss_guidance_strength,
|
| 104 |
+
},
|
| 105 |
+
slat_sampler_params={
|
| 106 |
+
"steps": slat_sampling_steps,
|
| 107 |
+
"cfg_strength": slat_guidance_strength,
|
| 108 |
+
},
|
| 109 |
+
)
|
| 110 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
video = render_utils.render_video(outputs['gaussian'][0], num_frames=120)['color']
|
| 112 |
video_geo = render_utils.render_video(outputs['mesh'][0], num_frames=120)['normal']
|
| 113 |
video = [np.concatenate([video[i], video_geo[i]], axis=1) for i in range(len(video))]
|
|
|
|
| 125 |
texture_size: int,
|
| 126 |
req: gr.Request,
|
| 127 |
) -> Tuple[str, str]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
|
|
|
|
|
|
|
|
|
| 129 |
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
| 130 |
gs, mesh = unpack_state(state)
|
| 131 |
glb = postprocessing_utils.to_glb(gs, mesh, simplify=mesh_simplify, texture_size=texture_size, verbose=False)
|
|
|
|
| 137 |
|
| 138 |
@spaces.GPU
|
| 139 |
def extract_gaussian(state: dict, req: gr.Request) -> Tuple[str, str]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
|
|
|
|
|
|
|
|
|
| 141 |
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
| 142 |
gs, _ = unpack_state(state)
|
| 143 |
gaussian_path = os.path.join(user_dir, 'sample.ply')
|
|
|
|
| 145 |
torch.cuda.empty_cache()
|
| 146 |
return gaussian_path, gaussian_path
|
| 147 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
def split_image(image: Image.Image) -> List[Image.Image]:
|
| 149 |
"""
|
| 150 |
Split an image into multiple views.
|
|
|
|
| 174 |
with gr.Tabs() as input_tabs:
|
| 175 |
with gr.Tab(label="Single Image", id=0) as single_image_input_tab:
|
| 176 |
image_prompt = gr.Image(label="Image Prompt", format="png", image_mode="RGBA", type="pil", height=300)
|
| 177 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
with gr.Accordion(label="Generation Settings", open=False):
|
| 179 |
seed = gr.Slider(0, MAX_SEED, label="Seed", value=0, step=1)
|
| 180 |
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
|
|
|
|
| 186 |
with gr.Row():
|
| 187 |
slat_guidance_strength = gr.Slider(0.0, 10.0, label="Guidance Strength", value=3.0, step=0.1)
|
| 188 |
slat_sampling_steps = gr.Slider(1, 50, label="Sampling Steps", value=12, step=1)
|
| 189 |
+
|
|
|
|
| 190 |
generate_btn = gr.Button("Generate")
|
| 191 |
|
| 192 |
with gr.Accordion(label="GLB Extraction Settings", open=False):
|
|
|
|
| 208 |
download_glb = gr.DownloadButton(label="Download GLB", interactive=False)
|
| 209 |
download_gs = gr.DownloadButton(label="Download Gaussian", interactive=False)
|
| 210 |
|
|
|
|
| 211 |
output_buf = gr.State()
|
| 212 |
|
| 213 |
# Example images at the bottom of the page
|
|
|
|
| 223 |
run_on_click=True,
|
| 224 |
examples_per_page=64,
|
| 225 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
|
| 227 |
# Handlers
|
| 228 |
demo.load(start_session)
|
|
|
|
| 232 |
lambda: tuple([False, gr.Row.update(visible=True), gr.Row.update(visible=False)]),
|
| 233 |
outputs=[is_multiimage, single_image_example, multiimage_example]
|
| 234 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
|
| 236 |
image_prompt.upload(
|
| 237 |
preprocess_image,
|
| 238 |
inputs=[image_prompt],
|
| 239 |
outputs=[image_prompt],
|
| 240 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
|
| 242 |
generate_btn.click(
|
| 243 |
get_seed,
|
|
|
|
| 245 |
outputs=[seed],
|
| 246 |
).then(
|
| 247 |
image_to_3d,
|
| 248 |
+
inputs=[image_prompt, seed, ss_guidance_strength, ss_sampling_steps, slat_guidance_strength, slat_sampling_steps],
|
| 249 |
outputs=[output_buf, video_output],
|
| 250 |
).then(
|
| 251 |
lambda: tuple([gr.Button(interactive=True), gr.Button(interactive=True)]),
|