Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import SamModel, SamProcessor
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# SAM 2 Modell laden (von Meta)
|
| 7 |
+
model_id = "facebook/sam-vit-huge"
|
| 8 |
+
processor = SamProcessor.from_pretrained(model_id)
|
| 9 |
+
model = SamModel.from_pretrained(model_id)
|
| 10 |
+
|
| 11 |
+
def segment_image(image):
|
| 12 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 13 |
+
with torch.no_grad():
|
| 14 |
+
outputs = model(**inputs)
|
| 15 |
+
masks = processor.post_process_masks(outputs, target_sizes=[image.size[::-1]])[0]
|
| 16 |
+
mask_image = Image.fromarray((masks[0][0].cpu().numpy() * 255).astype("uint8"))
|
| 17 |
+
return mask_image
|
| 18 |
+
|
| 19 |
+
demo = gr.Interface(
|
| 20 |
+
fn=segment_image,
|
| 21 |
+
inputs=gr.Image(type="pil"),
|
| 22 |
+
outputs=gr.Image(type="pil"),
|
| 23 |
+
title="FishBoost Segment Anything (Meta SAM 2 Demo)",
|
| 24 |
+
description="Upload an image and get a segmented result using Meta’s SAM 2 model."
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
demo.launch()
|