Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,92 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import Sam3Processor, Sam3Model
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import numpy as np
|
| 6 |
+
import matplotlib
|
| 7 |
|
| 8 |
+
# Scegli il device
|
| 9 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
+
print("Using device:", device)
|
| 11 |
|
| 12 |
+
# Carichiamo modello e processor UNA SOLA VOLTA (global)
|
| 13 |
+
model = Sam3Model.from_pretrained("facebook/sam3").to(device)
|
| 14 |
+
processor = Sam3Processor.from_pretrained("facebook/sam3")
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def overlay_masks(image: Image.Image, masks: torch.Tensor) -> Image.Image:
|
| 18 |
+
"""Applica le maschere colorate sopra l'immagine."""
|
| 19 |
+
image = image.convert("RGBA")
|
| 20 |
+
masks_np = 255 * masks.cpu().numpy().astype(np.uint8)
|
| 21 |
+
|
| 22 |
+
n_masks = masks_np.shape[0]
|
| 23 |
+
if n_masks == 0:
|
| 24 |
+
return image
|
| 25 |
+
|
| 26 |
+
cmap = matplotlib.colormaps.get_cmap("rainbow").resampled(n_masks)
|
| 27 |
+
colors = [
|
| 28 |
+
tuple(int(c * 255) for c in cmap(i)[:3])
|
| 29 |
+
for i in range(n_masks)
|
| 30 |
+
]
|
| 31 |
+
|
| 32 |
+
for mask, color in zip(masks_np, colors):
|
| 33 |
+
mask_img = Image.fromarray(mask)
|
| 34 |
+
overlay = Image.new("RGBA", image.size, color + (0,))
|
| 35 |
+
# 0.4 = trasparenza 40%
|
| 36 |
+
alpha = mask_img.point(lambda v: int(v * 0.4))
|
| 37 |
+
overlay.putalpha(alpha)
|
| 38 |
+
image = Image.alpha_composite(image, overlay)
|
| 39 |
+
|
| 40 |
+
return image
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def segment(image, text_prompt):
|
| 44 |
+
"""Funzione chiamata da Gradio: prende immagine + testo e ritorna immagine con maschere."""
|
| 45 |
+
if image is None or not text_prompt:
|
| 46 |
+
return image
|
| 47 |
+
|
| 48 |
+
# Pre-process
|
| 49 |
+
inputs = processor(
|
| 50 |
+
images=image,
|
| 51 |
+
text=text_prompt,
|
| 52 |
+
return_tensors="pt"
|
| 53 |
+
).to(device)
|
| 54 |
+
|
| 55 |
+
# Forward
|
| 56 |
+
with torch.no_grad():
|
| 57 |
+
outputs = model(**inputs)
|
| 58 |
+
|
| 59 |
+
# Post-process: maschere alla risoluzione originale
|
| 60 |
+
results = processor.post_process_instance_segmentation(
|
| 61 |
+
outputs,
|
| 62 |
+
threshold=0.5,
|
| 63 |
+
mask_threshold=0.5,
|
| 64 |
+
target_sizes=inputs["original_sizes"].tolist()
|
| 65 |
+
)[0]
|
| 66 |
+
|
| 67 |
+
masks = results["masks"] # [N, H, W]
|
| 68 |
+
print("Found", len(masks), "objects for prompt:", text_prompt)
|
| 69 |
+
|
| 70 |
+
if len(masks) == 0:
|
| 71 |
+
# Nessun oggetto trovato → ritorno immagine originale
|
| 72 |
+
return image
|
| 73 |
+
|
| 74 |
+
# Applica overlay
|
| 75 |
+
out_img = overlay_masks(image, masks)
|
| 76 |
+
return out_img
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
# Interfaccia Gradio
|
| 80 |
+
demo = gr.Interface(
|
| 81 |
+
fn=segment,
|
| 82 |
+
inputs=[
|
| 83 |
+
gr.Image(type="pil", label="Immagine"),
|
| 84 |
+
gr.Textbox(label="Prompt (es. 'cat', 'dog', 'ear')", value="cat"),
|
| 85 |
+
],
|
| 86 |
+
outputs=gr.Image(type="pil", label="Risultato"),
|
| 87 |
+
title="SAM3 – Promptable Concept Segmentation",
|
| 88 |
+
description="Carica un'immagine, scrivi un concetto (es. 'cat', 'dog', 'ear') e SAM3 segmenterà tutte le istanze trovate."
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
if __name__ == "__main__":
|
| 92 |
+
demo.launch()
|