Spaces:
Running on Zero
Running on Zero
Translate Space interface to English
Browse files
app.py
CHANGED
|
@@ -62,7 +62,7 @@ def segment_street_scene(
|
|
| 62 |
):
|
| 63 |
"""Run semantic segmentation and return visual, tabular, and raw outputs."""
|
| 64 |
if image is None:
|
| 65 |
-
raise gr.Error("
|
| 66 |
|
| 67 |
started_at = time.perf_counter()
|
| 68 |
prepared_image = resize_for_output(image)
|
|
@@ -83,7 +83,7 @@ def segment_street_scene(
|
|
| 83 |
class_map = class_map_tensor.cpu().numpy().astype(np.uint8)
|
| 84 |
except Exception as exc:
|
| 85 |
raise gr.Error(
|
| 86 |
-
f"
|
| 87 |
) from exc
|
| 88 |
|
| 89 |
overlay, color_mask = render_segmentation(
|
|
@@ -109,8 +109,8 @@ def segment_street_scene(
|
|
| 109 |
elapsed = time.perf_counter() - started_at
|
| 110 |
visible_classes = len(np.unique(class_map))
|
| 111 |
status = (
|
| 112 |
-
f"
|
| 113 |
-
f"{visible_classes}
|
| 114 |
f"device={device.type}"
|
| 115 |
)
|
| 116 |
|
|
@@ -135,8 +135,8 @@ with gr.Blocks(title="Street Scene Segmentation", theme=gr.themes.Soft(), css=CS
|
|
| 135 |
gr.Markdown(
|
| 136 |
"""
|
| 137 |
<div class="hero">
|
| 138 |
-
<h1>🚦
|
| 139 |
-
<p>
|
| 140 |
<p class="muted">SegFormer-B0 · 19 street-scene classes · CPU/GPU adaptive</p>
|
| 141 |
</div>
|
| 142 |
"""
|
|
@@ -146,53 +146,54 @@ with gr.Blocks(title="Street Scene Segmentation", theme=gr.themes.Soft(), css=CS
|
|
| 146 |
with gr.Column(scale=5):
|
| 147 |
image_input = gr.Image(
|
| 148 |
type="pil",
|
| 149 |
-
label="
|
| 150 |
height=470,
|
| 151 |
sources=["upload", "clipboard", "webcam"],
|
| 152 |
)
|
| 153 |
-
with gr.Accordion("
|
| 154 |
opacity_input = gr.Slider(
|
| 155 |
0.15,
|
| 156 |
0.85,
|
| 157 |
value=0.55,
|
| 158 |
step=0.05,
|
| 159 |
-
label="
|
| 160 |
)
|
| 161 |
min_share_input = gr.Slider(
|
| 162 |
0.0,
|
| 163 |
5.0,
|
| 164 |
value=0.1,
|
| 165 |
step=0.1,
|
| 166 |
-
label="
|
| 167 |
)
|
| 168 |
with gr.Row():
|
| 169 |
-
run_button = gr.Button("
|
| 170 |
-
clear_button = gr.ClearButton(value="
|
| 171 |
status_output = gr.Markdown()
|
| 172 |
|
| 173 |
with gr.Column(scale=7):
|
| 174 |
with gr.Tabs():
|
| 175 |
-
with gr.Tab("
|
| 176 |
-
overlay_output = gr.Image(label="
|
| 177 |
-
with gr.Tab("
|
| 178 |
-
mask_output = gr.Image(label="Cityscapes
|
| 179 |
|
| 180 |
table_output = gr.Dataframe(
|
| 181 |
-
headers=["Class ID", "
|
| 182 |
datatype=["number", "str", "number", "number", "str"],
|
| 183 |
-
label="
|
| 184 |
interactive=False,
|
| 185 |
wrap=True,
|
| 186 |
)
|
| 187 |
files_output = gr.File(
|
| 188 |
-
label="
|
| 189 |
file_count="multiple",
|
| 190 |
)
|
| 191 |
|
| 192 |
gr.Markdown(
|
| 193 |
"""
|
| 194 |
-
>
|
| 195 |
-
>
|
|
|
|
| 196 |
"""
|
| 197 |
)
|
| 198 |
|
|
|
|
| 62 |
):
|
| 63 |
"""Run semantic segmentation and return visual, tabular, and raw outputs."""
|
| 64 |
if image is None:
|
| 65 |
+
raise gr.Error("Please upload a street-scene image first.")
|
| 66 |
|
| 67 |
started_at = time.perf_counter()
|
| 68 |
prepared_image = resize_for_output(image)
|
|
|
|
| 83 |
class_map = class_map_tensor.cpu().numpy().astype(np.uint8)
|
| 84 |
except Exception as exc:
|
| 85 |
raise gr.Error(
|
| 86 |
+
f"Segmentation failed: {type(exc).__name__}: {exc}"
|
| 87 |
) from exc
|
| 88 |
|
| 89 |
overlay, color_mask = render_segmentation(
|
|
|
|
| 109 |
elapsed = time.perf_counter() - started_at
|
| 110 |
visible_classes = len(np.unique(class_map))
|
| 111 |
status = (
|
| 112 |
+
f"Done · {prepared_image.width}×{prepared_image.height} · "
|
| 113 |
+
f"{visible_classes} street-scene classes · {elapsed:.1f}s · "
|
| 114 |
f"device={device.type}"
|
| 115 |
)
|
| 116 |
|
|
|
|
| 135 |
gr.Markdown(
|
| 136 |
"""
|
| 137 |
<div class="hero">
|
| 138 |
+
<h1>🚦 Street Scene Semantic Segmentation</h1>
|
| 139 |
+
<p>Upload a road or urban street image to identify Cityscapes classes such as roads, sidewalks, buildings, vegetation, vehicles, and people.</p>
|
| 140 |
<p class="muted">SegFormer-B0 · 19 street-scene classes · CPU/GPU adaptive</p>
|
| 141 |
</div>
|
| 142 |
"""
|
|
|
|
| 146 |
with gr.Column(scale=5):
|
| 147 |
image_input = gr.Image(
|
| 148 |
type="pil",
|
| 149 |
+
label="Upload a street-scene image",
|
| 150 |
height=470,
|
| 151 |
sources=["upload", "clipboard", "webcam"],
|
| 152 |
)
|
| 153 |
+
with gr.Accordion("Display settings", open=False):
|
| 154 |
opacity_input = gr.Slider(
|
| 155 |
0.15,
|
| 156 |
0.85,
|
| 157 |
value=0.55,
|
| 158 |
step=0.05,
|
| 159 |
+
label="Overlay opacity",
|
| 160 |
)
|
| 161 |
min_share_input = gr.Slider(
|
| 162 |
0.0,
|
| 163 |
5.0,
|
| 164 |
value=0.1,
|
| 165 |
step=0.1,
|
| 166 |
+
label="Minimum class area shown in table (%)",
|
| 167 |
)
|
| 168 |
with gr.Row():
|
| 169 |
+
run_button = gr.Button("Run segmentation", variant="primary", size="lg")
|
| 170 |
+
clear_button = gr.ClearButton(value="Clear", components=[image_input])
|
| 171 |
status_output = gr.Markdown()
|
| 172 |
|
| 173 |
with gr.Column(scale=7):
|
| 174 |
with gr.Tabs():
|
| 175 |
+
with gr.Tab("Overlay"):
|
| 176 |
+
overlay_output = gr.Image(label="Segmentation overlay", height=470)
|
| 177 |
+
with gr.Tab("Color mask"):
|
| 178 |
+
mask_output = gr.Image(label="Cityscapes color mask", height=470)
|
| 179 |
|
| 180 |
table_output = gr.Dataframe(
|
| 181 |
+
headers=["Class ID", "Class", "Pixels", "Area share (%)", "Color"],
|
| 182 |
datatype=["number", "str", "number", "number", "str"],
|
| 183 |
+
label="Detected street-scene classes",
|
| 184 |
interactive=False,
|
| 185 |
wrap=True,
|
| 186 |
)
|
| 187 |
files_output = gr.File(
|
| 188 |
+
label="Download results (overlay, color mask, class IDs, and CSV)",
|
| 189 |
file_count="multiple",
|
| 190 |
)
|
| 191 |
|
| 192 |
gr.Markdown(
|
| 193 |
"""
|
| 194 |
+
> The model is downloaded from Hugging Face on the first run and then reused from the container cache.
|
| 195 |
+
> Images are resized to a maximum side length of 2048 px to prevent memory issues.
|
| 196 |
+
> This Cityscapes model is intended for street scenes, not medical, remote-sensing, or indoor imagery.
|
| 197 |
"""
|
| 198 |
)
|
| 199 |
|