Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -227,19 +227,18 @@ def click_to_similarity_in_same_image(
|
|
| 227 |
return marked_ref, heatmap_pil, overlay_pil, overlay_boxes_pil
|
| 228 |
|
| 229 |
# ----------------------------
|
| 230 |
-
# Gradio UI (
|
| 231 |
# ----------------------------
|
| 232 |
with gr.Blocks(theme=gr.themes.Soft(), title="DINOv3 Single-Image Patch Similarity") as demo:
|
| 233 |
gr.Markdown("# 🦖 DINOv3 Single-Image Patch Similarity")
|
| 234 |
-
gr.Markdown("
|
| 235 |
-
gr.Markdown("Upload one image, then **click anywhere** to highlight the most similar regions in the *same* image.")
|
| 236 |
|
| 237 |
app_state = gr.State()
|
| 238 |
|
| 239 |
with gr.Row():
|
| 240 |
with gr.Column(scale=1):
|
| 241 |
input_image = gr.Image(
|
| 242 |
-
label="Image (click anywhere)",
|
| 243 |
type="pil",
|
| 244 |
value="https://images.squarespace-cdn.com/content/v1/607f89e638219e13eee71b1e/1684821560422-SD5V37BAG28BURTLIXUQ/michael-sum-LEpfefQf4rU-unsplash.jpg"
|
| 245 |
)
|
|
@@ -254,14 +253,14 @@ with gr.Blocks(theme=gr.themes.Soft(), title="DINOv3 Single-Image Patch Similari
|
|
| 254 |
["viridis", "magma", "plasma", "inferno", "turbo", "cividis"],
|
| 255 |
value="viridis", label="Colormap",
|
| 256 |
)
|
| 257 |
-
#
|
| 258 |
model_choice = gr.Dropdown(
|
| 259 |
choices=AVAILABLE_MODELS,
|
| 260 |
value=DEFAULT_MODEL_ID,
|
| 261 |
label="Backbone (DINOv3)",
|
| 262 |
info="ViT-S/16+ is smaller & faster; ViT-H/16+ is larger.",
|
| 263 |
)
|
| 264 |
-
# Start processing button
|
| 265 |
with gr.Row():
|
| 266 |
start_btn = gr.Button("▶️ Start processing", variant="primary")
|
| 267 |
|
|
@@ -271,7 +270,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="DINOv3 Single-Image Patch Similari
|
|
| 271 |
box_radius = gr.Slider(0, 10, value=1, step=1, label="Box radius (patches)")
|
| 272 |
|
| 273 |
with gr.Row():
|
| 274 |
-
marked_image = gr.Image(label="Click marker", interactive=False)
|
| 275 |
heatmap_output = gr.Image(label="Similarity heatmap", interactive=False)
|
| 276 |
with gr.Row():
|
| 277 |
overlay_output = gr.Image(label="Overlay (image ⊕ heatmap)", interactive=False)
|
|
@@ -284,7 +283,8 @@ with gr.Blocks(theme=gr.themes.Soft(), title="DINOv3 Single-Image Patch Similari
|
|
| 284 |
model = get_model(model_id)
|
| 285 |
_current_model_id = model_id
|
| 286 |
|
| 287 |
-
|
|
|
|
| 288 |
if img is None:
|
| 289 |
return None, None
|
| 290 |
_ensure_model(model_id)
|
|
@@ -293,6 +293,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="DINOv3 Single-Image Patch Similari
|
|
| 293 |
progress(1, desc="Done!")
|
| 294 |
return st["img"], st
|
| 295 |
|
|
|
|
| 296 |
def _on_click(st, a: float, m: str, excl: int, k: int, box_rad: int, evt: gr.SelectData):
|
| 297 |
if not st or evt is None:
|
| 298 |
return None, None, None, None
|
|
@@ -302,20 +303,29 @@ with gr.Blocks(theme=gr.themes.Soft(), title="DINOv3 Single-Image Patch Similari
|
|
| 302 |
box_radius_patches=int(box_rad),
|
| 303 |
)
|
| 304 |
|
| 305 |
-
#
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 314 |
|
| 315 |
-
#
|
| 316 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 317 |
|
| 318 |
-
#
|
| 319 |
marked_image.select(
|
| 320 |
_on_click,
|
| 321 |
inputs=[app_state, alpha, cmap, exclude_r, topk, box_radius],
|
|
|
|
| 227 |
return marked_ref, heatmap_pil, overlay_pil, overlay_boxes_pil
|
| 228 |
|
| 229 |
# ----------------------------
|
| 230 |
+
# Gradio UI (Manual-only processing)
|
| 231 |
# ----------------------------
|
| 232 |
with gr.Blocks(theme=gr.themes.Soft(), title="DINOv3 Single-Image Patch Similarity") as demo:
|
| 233 |
gr.Markdown("# 🦖 DINOv3 Single-Image Patch Similarity")
|
| 234 |
+
gr.Markdown("Upload one image, adjust settings, then press **▶️ Start processing**. Click on the processed image to find similar regions.")
|
|
|
|
| 235 |
|
| 236 |
app_state = gr.State()
|
| 237 |
|
| 238 |
with gr.Row():
|
| 239 |
with gr.Column(scale=1):
|
| 240 |
input_image = gr.Image(
|
| 241 |
+
label="Image (click anywhere after processing)",
|
| 242 |
type="pil",
|
| 243 |
value="https://images.squarespace-cdn.com/content/v1/607f89e638219e13eee71b1e/1684821560422-SD5V37BAG28BURTLIXUQ/michael-sum-LEpfefQf4rU-unsplash.jpg"
|
| 244 |
)
|
|
|
|
| 253 |
["viridis", "magma", "plasma", "inferno", "turbo", "cividis"],
|
| 254 |
value="viridis", label="Colormap",
|
| 255 |
)
|
| 256 |
+
# Backbone selector (default = smaller/faster ViT-S/16+)
|
| 257 |
model_choice = gr.Dropdown(
|
| 258 |
choices=AVAILABLE_MODELS,
|
| 259 |
value=DEFAULT_MODEL_ID,
|
| 260 |
label="Backbone (DINOv3)",
|
| 261 |
info="ViT-S/16+ is smaller & faster; ViT-H/16+ is larger.",
|
| 262 |
)
|
| 263 |
+
# Start processing button (manual trigger)
|
| 264 |
with gr.Row():
|
| 265 |
start_btn = gr.Button("▶️ Start processing", variant="primary")
|
| 266 |
|
|
|
|
| 270 |
box_radius = gr.Slider(0, 10, value=1, step=1, label="Box radius (patches)")
|
| 271 |
|
| 272 |
with gr.Row():
|
| 273 |
+
marked_image = gr.Image(label="Click marker / Preview", interactive=False)
|
| 274 |
heatmap_output = gr.Image(label="Similarity heatmap", interactive=False)
|
| 275 |
with gr.Row():
|
| 276 |
overlay_output = gr.Image(label="Overlay (image ⊕ heatmap)", interactive=False)
|
|
|
|
| 283 |
model = get_model(model_id)
|
| 284 |
_current_model_id = model_id
|
| 285 |
|
| 286 |
+
# Manual feature extraction (only runs on Start button)
|
| 287 |
+
def _run_extraction(img: Image.Image, long_side: int, model_id: str, progress=gr.Progress(track_tqdm=True)):
|
| 288 |
if img is None:
|
| 289 |
return None, None
|
| 290 |
_ensure_model(model_id)
|
|
|
|
| 293 |
progress(1, desc="Done!")
|
| 294 |
return st["img"], st
|
| 295 |
|
| 296 |
+
# Clicking on processed image to compute similarities
|
| 297 |
def _on_click(st, a: float, m: str, excl: int, k: int, box_rad: int, evt: gr.SelectData):
|
| 298 |
if not st or evt is None:
|
| 299 |
return None, None, None, None
|
|
|
|
| 303 |
box_radius_patches=int(box_rad),
|
| 304 |
)
|
| 305 |
|
| 306 |
+
# On image change: just preview and clear outputs/state (NO extraction)
|
| 307 |
+
def _on_image_changed(img: Image.Image):
|
| 308 |
+
if img is None:
|
| 309 |
+
return None, None, None, None, None
|
| 310 |
+
return img, None, None, None, None
|
| 311 |
+
|
| 312 |
+
# ---------- Wiring (Manual mode) ----------
|
| 313 |
+
# Do NOT auto-run on upload/slider/model change or on app load.
|
| 314 |
+
# Only the Start button triggers extraction.
|
| 315 |
+
start_btn.click(
|
| 316 |
+
_run_extraction,
|
| 317 |
+
inputs=[input_image, target_long_side, model_choice],
|
| 318 |
+
outputs=[marked_image, app_state],
|
| 319 |
+
)
|
| 320 |
|
| 321 |
+
# When a new image is picked, show it as preview and clear old results.
|
| 322 |
+
input_image.change(
|
| 323 |
+
_on_image_changed,
|
| 324 |
+
inputs=[input_image],
|
| 325 |
+
outputs=[marked_image, app_state, heatmap_output, overlay_output, overlay_boxes_output],
|
| 326 |
+
)
|
| 327 |
|
| 328 |
+
# Keep click handler the same.
|
| 329 |
marked_image.select(
|
| 330 |
_on_click,
|
| 331 |
inputs=[app_state, alpha, cmap, exclude_r, topk, box_radius],
|