Spaces:
Sleeping
Sleeping
Commit ·
39db02f
1
Parent(s): 8cd3811
Pinned gradio version to 5.6.0
Browse files- README.md +1 -1
- app.py +22 -40
- requirements.txt +1 -1
README.md
CHANGED
|
@@ -4,7 +4,7 @@ emoji: 🌀
|
|
| 4 |
colorFrom: green
|
| 5 |
colorTo: yellow
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version: "5.
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
|
|
|
| 4 |
colorFrom: green
|
| 5 |
colorTo: yellow
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: "5.6.0"
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
app.py
CHANGED
|
@@ -6,6 +6,7 @@ Estimates depth from a single image using MiDaS models.
|
|
| 6 |
Optimized for free-tier CPU inference.
|
| 7 |
"""
|
| 8 |
|
|
|
|
| 9 |
import time
|
| 10 |
|
| 11 |
import gradio as gr
|
|
@@ -29,33 +30,20 @@ estimator = DepthEstimator(model_size="small")
|
|
| 29 |
print("Ready!")
|
| 30 |
|
| 31 |
|
| 32 |
-
def predict(
|
| 33 |
-
image
|
| 34 |
-
colormap: str,
|
| 35 |
-
output_mode: str,
|
| 36 |
-
overlay_alpha: float,
|
| 37 |
-
) -> tuple:
|
| 38 |
-
"""
|
| 39 |
-
Run depth estimation and return results.
|
| 40 |
-
|
| 41 |
-
Returns:
|
| 42 |
-
(result_image, depth_colored, stats_string)
|
| 43 |
-
"""
|
| 44 |
if image is None:
|
| 45 |
raise gr.Error("Please upload an image first.")
|
| 46 |
|
| 47 |
start = time.time()
|
| 48 |
|
| 49 |
-
|
| 50 |
-
image_rgb = image.convert("RGB")
|
| 51 |
depth = estimator.predict(image_rgb)
|
| 52 |
|
| 53 |
inference_time = time.time() - start
|
| 54 |
|
| 55 |
-
# Create colormapped depth
|
| 56 |
depth_colored = depth_to_colormap(depth, colormap.lower())
|
| 57 |
|
| 58 |
-
# Create output based on mode
|
| 59 |
if output_mode == "Side-by-Side":
|
| 60 |
result = create_side_by_side(image_rgb, depth_colored)
|
| 61 |
elif output_mode == "Overlay":
|
|
@@ -63,11 +51,20 @@ def predict(
|
|
| 63 |
else:
|
| 64 |
result = depth_colored
|
| 65 |
|
| 66 |
-
# Stats
|
| 67 |
w, h = image_rgb.size
|
| 68 |
stats = f"{w}×{h} · {inference_time:.2f}s inference · MiDaS Small"
|
| 69 |
|
| 70 |
-
return result,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
|
| 73 |
# ──────────────────────────────────────────────
|
|
@@ -104,40 +101,25 @@ with gr.Blocks(
|
|
| 104 |
overlay_alpha = gr.Slider(
|
| 105 |
minimum=0.2, maximum=0.8, value=0.5, step=0.1,
|
| 106 |
label="Overlay Opacity",
|
| 107 |
-
visible=False,
|
| 108 |
)
|
| 109 |
run_btn = gr.Button("Estimate Depth", variant="primary")
|
| 110 |
stats = gr.Textbox(label="Info", interactive=False)
|
| 111 |
|
| 112 |
with gr.Column(scale=1):
|
| 113 |
result_image = gr.Image(type="pil", label="Result")
|
| 114 |
-
depth_image = gr.Image(type="pil", label="Depth Map", visible=False)
|
| 115 |
|
| 116 |
-
# Show/hide overlay slider
|
| 117 |
-
def toggle_overlay(mode):
|
| 118 |
-
return gr.update(visible=(mode == "Overlay"))
|
| 119 |
-
|
| 120 |
-
output_mode.change(toggle_overlay, output_mode, overlay_alpha)
|
| 121 |
-
|
| 122 |
-
# Run prediction
|
| 123 |
run_btn.click(
|
| 124 |
fn=predict,
|
| 125 |
inputs=[input_image, colormap, output_mode, overlay_alpha],
|
| 126 |
-
outputs=[result_image,
|
|
|
|
| 127 |
)
|
| 128 |
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
[
|
| 133 |
-
|
| 134 |
-
["examples/indoor.jpg", "Viridis", "Overlay", 0.5],
|
| 135 |
-
],
|
| 136 |
-
inputs=[input_image, colormap, output_mode, overlay_alpha],
|
| 137 |
-
outputs=[result_image, depth_image, stats],
|
| 138 |
-
fn=predict,
|
| 139 |
-
cache_examples=False,
|
| 140 |
-
)
|
| 141 |
|
| 142 |
|
| 143 |
if __name__ == "__main__":
|
|
|
|
| 6 |
Optimized for free-tier CPU inference.
|
| 7 |
"""
|
| 8 |
|
| 9 |
+
import os
|
| 10 |
import time
|
| 11 |
|
| 12 |
import gradio as gr
|
|
|
|
| 30 |
print("Ready!")
|
| 31 |
|
| 32 |
|
| 33 |
+
def predict(image, colormap, output_mode, overlay_alpha):
|
| 34 |
+
"""Run depth estimation and return result image + stats string."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
if image is None:
|
| 36 |
raise gr.Error("Please upload an image first.")
|
| 37 |
|
| 38 |
start = time.time()
|
| 39 |
|
| 40 |
+
image_rgb = Image.fromarray(image) if isinstance(image, np.ndarray) else image.convert("RGB")
|
|
|
|
| 41 |
depth = estimator.predict(image_rgb)
|
| 42 |
|
| 43 |
inference_time = time.time() - start
|
| 44 |
|
|
|
|
| 45 |
depth_colored = depth_to_colormap(depth, colormap.lower())
|
| 46 |
|
|
|
|
| 47 |
if output_mode == "Side-by-Side":
|
| 48 |
result = create_side_by_side(image_rgb, depth_colored)
|
| 49 |
elif output_mode == "Overlay":
|
|
|
|
| 51 |
else:
|
| 52 |
result = depth_colored
|
| 53 |
|
|
|
|
| 54 |
w, h = image_rgb.size
|
| 55 |
stats = f"{w}×{h} · {inference_time:.2f}s inference · MiDaS Small"
|
| 56 |
|
| 57 |
+
return result, stats
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
# ──────────────────────────────────────────────
|
| 61 |
+
# Build example list (only include files that exist)
|
| 62 |
+
# ──────────────────────────────────────────────
|
| 63 |
+
example_list = []
|
| 64 |
+
for name in ["street.jpg", "landscape.jpg", "indoor.jpg"]:
|
| 65 |
+
path = os.path.join("examples", name)
|
| 66 |
+
if os.path.exists(path):
|
| 67 |
+
example_list.append([path])
|
| 68 |
|
| 69 |
|
| 70 |
# ──────────────────────────────────────────────
|
|
|
|
| 101 |
overlay_alpha = gr.Slider(
|
| 102 |
minimum=0.2, maximum=0.8, value=0.5, step=0.1,
|
| 103 |
label="Overlay Opacity",
|
|
|
|
| 104 |
)
|
| 105 |
run_btn = gr.Button("Estimate Depth", variant="primary")
|
| 106 |
stats = gr.Textbox(label="Info", interactive=False)
|
| 107 |
|
| 108 |
with gr.Column(scale=1):
|
| 109 |
result_image = gr.Image(type="pil", label="Result")
|
|
|
|
| 110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
run_btn.click(
|
| 112 |
fn=predict,
|
| 113 |
inputs=[input_image, colormap, output_mode, overlay_alpha],
|
| 114 |
+
outputs=[result_image, stats],
|
| 115 |
+
api_name="predict",
|
| 116 |
)
|
| 117 |
|
| 118 |
+
if example_list:
|
| 119 |
+
gr.Examples(
|
| 120 |
+
examples=example_list,
|
| 121 |
+
inputs=[input_image],
|
| 122 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
|
| 125 |
if __name__ == "__main__":
|
requirements.txt
CHANGED
|
@@ -5,4 +5,4 @@ opencv-python-headless>=4.7.0
|
|
| 5 |
Pillow>=9.4.0
|
| 6 |
numpy>=1.23.0
|
| 7 |
matplotlib>=3.6.0
|
| 8 |
-
gradio
|
|
|
|
| 5 |
Pillow>=9.4.0
|
| 6 |
numpy>=1.23.0
|
| 7 |
matplotlib>=3.6.0
|
| 8 |
+
gradio==5.6.0
|