Spaces:
Running
on
Zero
Running
on
Zero
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -341,7 +341,7 @@ def remove_background(input: Image.Image) -> Image.Image:
|
|
| 341 |
return output
|
| 342 |
|
| 343 |
|
| 344 |
-
def preprocess_image(input: Image.Image) -> Image.Image:
|
| 345 |
"""
|
| 346 |
Preprocess the input image.
|
| 347 |
"""
|
|
@@ -380,19 +380,17 @@ def preprocess_image(input: Image.Image) -> Image.Image:
|
|
| 380 |
alpha = output[:, :, 3:4]
|
| 381 |
rgb = rgb * alpha # premultiply alpha
|
| 382 |
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
# Only apply to foreground pixels
|
| 395 |
-
rgb[:, :, c] = np.where(fg_mask, ch_norm, 0)
|
| 396 |
|
| 397 |
output = Image.fromarray((rgb * 255).astype(np.uint8))
|
| 398 |
return output
|
|
@@ -521,6 +519,7 @@ def image_to_3d(
|
|
| 521 |
multiimages: List[Tuple[Image.Image, str]],
|
| 522 |
multiimage_algo: Literal["multidiffusion", "stochastic"],
|
| 523 |
tex_multiimage_algo: Literal["multidiffusion", "stochastic"],
|
|
|
|
| 524 |
req: gr.Request,
|
| 525 |
progress=gr.Progress(track_tqdm=True),
|
| 526 |
) -> str:
|
|
@@ -529,7 +528,7 @@ def image_to_3d(
|
|
| 529 |
|
| 530 |
# Preprocess images (background removal, cropping, etc.)
|
| 531 |
images = [image[0] for image in multiimages]
|
| 532 |
-
processed_images = [preprocess_image(img) for img in images]
|
| 533 |
|
| 534 |
# Debug: save preprocessed images and log stats
|
| 535 |
for i, img in enumerate(processed_images):
|
|
@@ -758,6 +757,7 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="orange", neutral_hue="slate"))
|
|
| 758 |
tex_slat_rescale_t = gr.Slider(1.0, 6.0, label="Rescale T", value=3.0, step=0.1)
|
| 759 |
multiimage_algo = gr.Radio(["stochastic", "multidiffusion"], label="Structure Algorithm", value="stochastic")
|
| 760 |
tex_multiimage_algo = gr.Radio(["stochastic", "multidiffusion"], label="Texture Algorithm", value="stochastic")
|
|
|
|
| 761 |
|
| 762 |
with gr.Column(scale=10):
|
| 763 |
with gr.Row():
|
|
@@ -802,7 +802,7 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="orange", neutral_hue="slate"))
|
|
| 802 |
ss_guidance_strength, ss_guidance_rescale, ss_sampling_steps, ss_rescale_t,
|
| 803 |
shape_slat_guidance_strength, shape_slat_guidance_rescale, shape_slat_sampling_steps, shape_slat_rescale_t,
|
| 804 |
tex_slat_guidance_strength, tex_slat_guidance_rescale, tex_slat_sampling_steps, tex_slat_rescale_t,
|
| 805 |
-
multiimage_prompt, multiimage_algo, tex_multiimage_algo
|
| 806 |
],
|
| 807 |
outputs=[output_buf, preview_output],
|
| 808 |
)
|
|
|
|
| 341 |
return output
|
| 342 |
|
| 343 |
|
| 344 |
+
def preprocess_image(input: Image.Image, histogram_normalize: bool = False) -> Image.Image:
|
| 345 |
"""
|
| 346 |
Preprocess the input image.
|
| 347 |
"""
|
|
|
|
| 380 |
alpha = output[:, :, 3:4]
|
| 381 |
rgb = rgb * alpha # premultiply alpha
|
| 382 |
|
| 383 |
+
if histogram_normalize:
|
| 384 |
+
fg_mask = (alpha[:, :, 0] > 0.05)
|
| 385 |
+
if fg_mask.any():
|
| 386 |
+
for c in range(3):
|
| 387 |
+
ch = rgb[:, :, c]
|
| 388 |
+
fg_vals = ch[fg_mask]
|
| 389 |
+
if fg_vals.max() > fg_vals.min():
|
| 390 |
+
lo, hi = np.percentile(fg_vals, [1, 99])
|
| 391 |
+
if hi > lo:
|
| 392 |
+
ch_norm = np.clip((ch - lo) / (hi - lo), 0, 1)
|
| 393 |
+
rgb[:, :, c] = np.where(fg_mask, ch_norm, 0)
|
|
|
|
|
|
|
| 394 |
|
| 395 |
output = Image.fromarray((rgb * 255).astype(np.uint8))
|
| 396 |
return output
|
|
|
|
| 519 |
multiimages: List[Tuple[Image.Image, str]],
|
| 520 |
multiimage_algo: Literal["multidiffusion", "stochastic"],
|
| 521 |
tex_multiimage_algo: Literal["multidiffusion", "stochastic"],
|
| 522 |
+
histogram_normalize: bool,
|
| 523 |
req: gr.Request,
|
| 524 |
progress=gr.Progress(track_tqdm=True),
|
| 525 |
) -> str:
|
|
|
|
| 528 |
|
| 529 |
# Preprocess images (background removal, cropping, etc.)
|
| 530 |
images = [image[0] for image in multiimages]
|
| 531 |
+
processed_images = [preprocess_image(img, histogram_normalize=histogram_normalize) for img in images]
|
| 532 |
|
| 533 |
# Debug: save preprocessed images and log stats
|
| 534 |
for i, img in enumerate(processed_images):
|
|
|
|
| 757 |
tex_slat_rescale_t = gr.Slider(1.0, 6.0, label="Rescale T", value=3.0, step=0.1)
|
| 758 |
multiimage_algo = gr.Radio(["stochastic", "multidiffusion"], label="Structure Algorithm", value="stochastic")
|
| 759 |
tex_multiimage_algo = gr.Radio(["stochastic", "multidiffusion"], label="Texture Algorithm", value="stochastic")
|
| 760 |
+
histogram_normalize = gr.Checkbox(label="Histogram Normalize", value=False)
|
| 761 |
|
| 762 |
with gr.Column(scale=10):
|
| 763 |
with gr.Row():
|
|
|
|
| 802 |
ss_guidance_strength, ss_guidance_rescale, ss_sampling_steps, ss_rescale_t,
|
| 803 |
shape_slat_guidance_strength, shape_slat_guidance_rescale, shape_slat_sampling_steps, shape_slat_rescale_t,
|
| 804 |
tex_slat_guidance_strength, tex_slat_guidance_rescale, tex_slat_sampling_steps, tex_slat_rescale_t,
|
| 805 |
+
multiimage_prompt, multiimage_algo, tex_multiimage_algo, histogram_normalize
|
| 806 |
],
|
| 807 |
outputs=[output_buf, preview_output],
|
| 808 |
)
|