Spaces:
Runtime error
Runtime error
Download app.py from Studentepercaso/SAM3Test: direct link, hf CLI and curl.
- Browser
- Download file 2.69 kB
-
https://huggingface.co/spaces/Studentepercaso/SAM3Test/resolve/main/app.py
- Command line
-
hf download hf://spaces/Studentepercaso/SAM3Test/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Studentepercaso/SAM3Test/resolve/main/app.py
2.69 kB
| import gradio as gr | |
| import torch | |
| from transformers import Sam3Processor, Sam3Model | |
| from PIL import Image | |
| import numpy as np | |
| import matplotlib | |
| # Scegli il device | |
| device = "cuda" if torch.cuda.is_available() else "cpu" | |
| print("Using device:", device) | |
| # Carichiamo modello e processor UNA SOLA VOLTA (global) | |
| model = Sam3Model.from_pretrained("facebook/sam3").to(device) | |
| processor = Sam3Processor.from_pretrained("facebook/sam3") | |
| def overlay_masks(image: Image.Image, masks: torch.Tensor) -> Image.Image: | |
| """Applica le maschere colorate sopra l'immagine.""" | |
| image = image.convert("RGBA") | |
| masks_np = 255 * masks.cpu().numpy().astype(np.uint8) | |
| n_masks = masks_np.shape[0] | |
| if n_masks == 0: | |
| return image | |
| cmap = matplotlib.colormaps.get_cmap("rainbow").resampled(n_masks) | |
| colors = [ | |
| tuple(int(c * 255) for c in cmap(i)[:3]) | |
| for i in range(n_masks) | |
| ] | |
| for mask, color in zip(masks_np, colors): | |
| mask_img = Image.fromarray(mask) | |
| overlay = Image.new("RGBA", image.size, color + (0,)) | |
| # 0.4 = trasparenza 40% | |
| alpha = mask_img.point(lambda v: int(v * 0.4)) | |
| overlay.putalpha(alpha) | |
| image = Image.alpha_composite(image, overlay) | |
| return image | |
| def segment(image, text_prompt): | |
| """Funzione chiamata da Gradio: prende immagine + testo e ritorna immagine con maschere.""" | |
| if image is None or not text_prompt: | |
| return image | |
| # Pre-process | |
| inputs = processor( | |
| images=image, | |
| text=text_prompt, | |
| return_tensors="pt" | |
| ).to(device) | |
| # Forward | |
| with torch.no_grad(): | |
| outputs = model(**inputs) | |
| # Post-process: maschere alla risoluzione originale | |
| results = processor.post_process_instance_segmentation( | |
| outputs, | |
| threshold=0.5, | |
| mask_threshold=0.5, | |
| target_sizes=inputs["original_sizes"].tolist() | |
| )[0] | |
| masks = results["masks"] # [N, H, W] | |
| print("Found", len(masks), "objects for prompt:", text_prompt) | |
| if len(masks) == 0: | |
| # Nessun oggetto trovato → ritorno immagine originale | |
| return image | |
| # Applica overlay | |
| out_img = overlay_masks(image, masks) | |
| return out_img | |
| # Interfaccia Gradio | |
| demo = gr.Interface( | |
| fn=segment, | |
| inputs=[ | |
| gr.Image(type="pil", label="Immagine"), | |
| gr.Textbox(label="Prompt (es. 'cat', 'dog', 'ear')", value="cat"), | |
| ], | |
| outputs=gr.Image(type="pil", label="Risultato"), | |
| title="SAM3 – Promptable Concept Segmentation", | |
| description="Carica un'immagine, scrivi un concetto (es. 'cat', 'dog', 'ear') e SAM3 segmenterà tutte le istanze trovate." | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |