Spaces:
Running
on
Zero
Running
on
Zero
Commit
Β·
eef56f8
1
Parent(s):
9bf6375
Fix ZeroGPU recursion: split click handler from GPU function
Browse files
app.py
CHANGED
|
@@ -130,11 +130,22 @@ def segment_with_text(image: np.ndarray, text_prompt: str):
|
|
| 130 |
return image, None, f"β Error: {e}"
|
| 131 |
|
| 132 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
@spaces.GPU(duration=60)
|
| 134 |
-
def
|
| 135 |
-
"""Segment object at
|
| 136 |
if image is None:
|
| 137 |
return None, None, "β No image provided"
|
|
|
|
|
|
|
| 138 |
|
| 139 |
try:
|
| 140 |
from PIL import Image as PILImage
|
|
@@ -146,12 +157,9 @@ def segment_with_click(image: np.ndarray, evt: gr.SelectData):
|
|
| 146 |
else:
|
| 147 |
pil_image = image
|
| 148 |
|
| 149 |
-
# Get click coordinates
|
| 150 |
-
point = [evt.index[0], evt.index[1]]
|
| 151 |
-
|
| 152 |
# Run SAM3 with point prompt
|
| 153 |
state = processor.set_image(pil_image)
|
| 154 |
-
output = processor.set_point_prompt(state=state, points=[
|
| 155 |
|
| 156 |
if output is None or "masks" not in output:
|
| 157 |
return image, None, "β οΈ No object found"
|
|
@@ -243,6 +251,8 @@ with gr.Blocks(title="SAM 3D Objects MCP") as demo:
|
|
| 243 |
""")
|
| 244 |
|
| 245 |
mask_state = gr.State(None)
|
|
|
|
|
|
|
| 246 |
|
| 247 |
with gr.Row():
|
| 248 |
with gr.Column():
|
|
@@ -278,9 +288,14 @@ with gr.Blocks(title="SAM 3D Objects MCP") as demo:
|
|
| 278 |
outputs=[preview, mask_state, status]
|
| 279 |
)
|
| 280 |
|
|
|
|
| 281 |
input_image.select(
|
| 282 |
-
|
| 283 |
inputs=[input_image],
|
|
|
|
|
|
|
|
|
|
|
|
|
| 284 |
outputs=[preview, mask_state, status]
|
| 285 |
)
|
| 286 |
|
|
|
|
| 130 |
return image, None, f"β Error: {e}"
|
| 131 |
|
| 132 |
|
| 133 |
+
def handle_click(image, evt: gr.SelectData):
|
| 134 |
+
"""Handle click event and extract coordinates"""
|
| 135 |
+
if image is None or evt is None:
|
| 136 |
+
return None, None, None, "β Click on an image first"
|
| 137 |
+
# Store coordinates and pass to GPU function
|
| 138 |
+
x, y = evt.index[0], evt.index[1]
|
| 139 |
+
return image, x, y, "Processing..."
|
| 140 |
+
|
| 141 |
+
|
| 142 |
@spaces.GPU(duration=60)
|
| 143 |
+
def segment_with_point(image: np.ndarray, x: int, y: int):
|
| 144 |
+
"""Segment object at point with SAM3"""
|
| 145 |
if image is None:
|
| 146 |
return None, None, "β No image provided"
|
| 147 |
+
if x is None or y is None:
|
| 148 |
+
return None, None, "β No point selected"
|
| 149 |
|
| 150 |
try:
|
| 151 |
from PIL import Image as PILImage
|
|
|
|
| 157 |
else:
|
| 158 |
pil_image = image
|
| 159 |
|
|
|
|
|
|
|
|
|
|
| 160 |
# Run SAM3 with point prompt
|
| 161 |
state = processor.set_image(pil_image)
|
| 162 |
+
output = processor.set_point_prompt(state=state, points=[[x, y]], labels=[1])
|
| 163 |
|
| 164 |
if output is None or "masks" not in output:
|
| 165 |
return image, None, "β οΈ No object found"
|
|
|
|
| 251 |
""")
|
| 252 |
|
| 253 |
mask_state = gr.State(None)
|
| 254 |
+
click_x = gr.State(None)
|
| 255 |
+
click_y = gr.State(None)
|
| 256 |
|
| 257 |
with gr.Row():
|
| 258 |
with gr.Column():
|
|
|
|
| 288 |
outputs=[preview, mask_state, status]
|
| 289 |
)
|
| 290 |
|
| 291 |
+
# Click handler: first extract coordinates (no GPU), then segment (GPU)
|
| 292 |
input_image.select(
|
| 293 |
+
handle_click,
|
| 294 |
inputs=[input_image],
|
| 295 |
+
outputs=[input_image, click_x, click_y, status]
|
| 296 |
+
).then(
|
| 297 |
+
segment_with_point,
|
| 298 |
+
inputs=[input_image, click_x, click_y],
|
| 299 |
outputs=[preview, mask_state, status]
|
| 300 |
)
|
| 301 |
|