Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
# app.py
|
| 2 |
import os
|
| 3 |
import torch
|
| 4 |
import torch.nn.functional as F
|
|
@@ -203,19 +202,19 @@ def click_to_similarity_in_same_image(
|
|
| 203 |
return marked_ref, heatmap_pil, overlay_pil, overlay_boxes_pil
|
| 204 |
|
| 205 |
# ----------------------------
|
| 206 |
-
# Gradio UI
|
| 207 |
# ----------------------------
|
| 208 |
with gr.Blocks(theme=gr.themes.Soft(), title="DINOv3 Single-Image Patch Similarity") as demo:
|
| 209 |
gr.Markdown("# 🦖 DINOv3 Single-Image Patch Similarity")
|
| 210 |
gr.Markdown("## Running on CPU-only Space, feature extraction after uploading an image can take a moment")
|
| 211 |
-
gr.Markdown("Upload
|
| 212 |
|
| 213 |
app_state = gr.State()
|
| 214 |
|
| 215 |
with gr.Row():
|
| 216 |
with gr.Column(scale=1):
|
| 217 |
input_image = gr.Image(
|
| 218 |
-
label="
|
| 219 |
type="pil",
|
| 220 |
value="https://images.squarespace-cdn.com/content/v1/607f89e638219e13eee71b1e/1684821560422-SD5V37BAG28BURTLIXUQ/michael-sum-LEpfefQf4rU-unsplash.jpg"
|
| 221 |
)
|
|
@@ -224,6 +223,9 @@ with gr.Blocks(theme=gr.themes.Soft(), title="DINOv3 Single-Image Patch Similari
|
|
| 224 |
label="Processing Resolution",
|
| 225 |
info="Higher values = more detail but slower processing",
|
| 226 |
)
|
|
|
|
|
|
|
|
|
|
| 227 |
with gr.Row():
|
| 228 |
alpha = gr.Slider(0.0, 1.0, value=0.55, step=0.05, label="Overlay opacity")
|
| 229 |
cmap = gr.Dropdown(
|
|
@@ -236,22 +238,25 @@ with gr.Blocks(theme=gr.themes.Soft(), title="DINOv3 Single-Image Patch Similari
|
|
| 236 |
box_radius = gr.Slider(0, 10, value=1, step=1, label="Box radius (patches)")
|
| 237 |
|
| 238 |
with gr.Row():
|
| 239 |
-
marked_image = gr.Image(label="Click
|
| 240 |
heatmap_output = gr.Image(label="Similarity heatmap", interactive=False)
|
| 241 |
with gr.Row():
|
| 242 |
overlay_output = gr.Image(label="Overlay (image ⊕ heatmap)", interactive=False)
|
| 243 |
overlay_boxes_output = gr.Image(label="Overlay + top-K similar patch boxes", interactive=False)
|
| 244 |
|
| 245 |
-
|
|
|
|
| 246 |
if img is None:
|
|
|
|
| 247 |
return None, None
|
| 248 |
progress(0, desc="Extracting features...")
|
| 249 |
st = extract_image_features(img, int(long_side))
|
| 250 |
-
progress(1, desc="Done!")
|
| 251 |
return st["img"], st
|
| 252 |
|
| 253 |
def _on_click(st, a: float, m: str, excl: int, k: int, box_rad: int, evt: gr.SelectData):
|
| 254 |
if not st or evt is None:
|
|
|
|
| 255 |
return None, None, None, None
|
| 256 |
return click_to_similarity_in_same_image(
|
| 257 |
st, click_xy=evt.index, exclude_radius_patches=int(excl),
|
|
@@ -259,14 +264,18 @@ with gr.Blocks(theme=gr.themes.Soft(), title="DINOv3 Single-Image Patch Similari
|
|
| 259 |
box_radius_patches=int(box_rad),
|
| 260 |
)
|
| 261 |
|
| 262 |
-
#
|
| 263 |
-
|
| 264 |
-
|
| 265 |
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 270 |
marked_image.select(
|
| 271 |
_on_click,
|
| 272 |
inputs=[app_state, alpha, cmap, exclude_r, topk, box_radius],
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import torch
|
| 3 |
import torch.nn.functional as F
|
|
|
|
| 202 |
return marked_ref, heatmap_pil, overlay_pil, overlay_boxes_pil
|
| 203 |
|
| 204 |
# ----------------------------
|
| 205 |
+
# Gradio UI
|
| 206 |
# ----------------------------
|
| 207 |
with gr.Blocks(theme=gr.themes.Soft(), title="DINOv3 Single-Image Patch Similarity") as demo:
|
| 208 |
gr.Markdown("# 🦖 DINOv3 Single-Image Patch Similarity")
|
| 209 |
gr.Markdown("## Running on CPU-only Space, feature extraction after uploading an image can take a moment")
|
| 210 |
+
gr.Markdown("1. Upload an image. 2. Click **Process Image**. 3. **Click anywhere on the processed image** to find similar regions.")
|
| 211 |
|
| 212 |
app_state = gr.State()
|
| 213 |
|
| 214 |
with gr.Row():
|
| 215 |
with gr.Column(scale=1):
|
| 216 |
input_image = gr.Image(
|
| 217 |
+
label="1. Upload Image",
|
| 218 |
type="pil",
|
| 219 |
value="https://images.squarespace-cdn.com/content/v1/607f89e638219e13eee71b1e/1684821560422-SD5V37BAG28BURTLIXUQ/michael-sum-LEpfefQf4rU-unsplash.jpg"
|
| 220 |
)
|
|
|
|
| 223 |
label="Processing Resolution",
|
| 224 |
info="Higher values = more detail but slower processing",
|
| 225 |
)
|
| 226 |
+
# ⭐️ ADDED BUTTON
|
| 227 |
+
process_button = gr.Button("2. Process Image", variant="primary")
|
| 228 |
+
|
| 229 |
with gr.Row():
|
| 230 |
alpha = gr.Slider(0.0, 1.0, value=0.55, step=0.05, label="Overlay opacity")
|
| 231 |
cmap = gr.Dropdown(
|
|
|
|
| 238 |
box_radius = gr.Slider(0, 10, value=1, step=1, label="Box radius (patches)")
|
| 239 |
|
| 240 |
with gr.Row():
|
| 241 |
+
marked_image = gr.Image(label="3. Click on this image", interactive=True) # Changed from False to True for clarity
|
| 242 |
heatmap_output = gr.Image(label="Similarity heatmap", interactive=False)
|
| 243 |
with gr.Row():
|
| 244 |
overlay_output = gr.Image(label="Overlay (image ⊕ heatmap)", interactive=False)
|
| 245 |
overlay_boxes_output = gr.Image(label="Overlay + top-K similar patch boxes", interactive=False)
|
| 246 |
|
| 247 |
+
# ⭐️ RENAMED FUNCTION
|
| 248 |
+
def _process_image(img: Image.Image, long_side: int, progress=gr.Progress(track_tqdm=True)):
|
| 249 |
if img is None:
|
| 250 |
+
gr.Warning("Please upload an image first!")
|
| 251 |
return None, None
|
| 252 |
progress(0, desc="Extracting features...")
|
| 253 |
st = extract_image_features(img, int(long_side))
|
| 254 |
+
progress(1, desc="Done! You can now click on the image.")
|
| 255 |
return st["img"], st
|
| 256 |
|
| 257 |
def _on_click(st, a: float, m: str, excl: int, k: int, box_rad: int, evt: gr.SelectData):
|
| 258 |
if not st or evt is None:
|
| 259 |
+
gr.Warning("Please process an image before clicking on it.")
|
| 260 |
return None, None, None, None
|
| 261 |
return click_to_similarity_in_same_image(
|
| 262 |
st, click_xy=evt.index, exclude_radius_patches=int(excl),
|
|
|
|
| 264 |
box_radius_patches=int(box_rad),
|
| 265 |
)
|
| 266 |
|
| 267 |
+
# ⭐️ UPDATED EVENT WIRING
|
| 268 |
+
inputs_for_processing = [input_image, target_long_side]
|
| 269 |
+
outputs_for_processing = [marked_image, app_state]
|
| 270 |
|
| 271 |
+
# The button now triggers the main processing function
|
| 272 |
+
process_button.click(
|
| 273 |
+
_process_image,
|
| 274 |
+
inputs=inputs_for_processing,
|
| 275 |
+
outputs=outputs_for_processing
|
| 276 |
+
)
|
| 277 |
+
|
| 278 |
+
# The click event on the image remains the same
|
| 279 |
marked_image.select(
|
| 280 |
_on_click,
|
| 281 |
inputs=[app_state, alpha, cmap, exclude_r, topk, box_radius],
|