EnginDev commited on
Commit
7eb97fc
·
verified ·
1 Parent(s): 421969c

Delete app.py

Browse files

import gradio as gr
from transformers import SamModel, SamProcessor
from PIL import Image
import torch

# Kleineres Modell (läuft stabiler auf CPU)
model_id = "facebook/sam-vit-base"
processor = SamProcessor.from_pretrained(model_id)
model = SamModel.from_pretrained(model_id)

def segment_image(image):
# Sicherstellen, dass CPU verwendet wird
device = torch.device("cpu")
model.to(device)
inputs = processor(images=image, return_tensors="pt").to(device)

with torch.no_grad():
outputs = model(**inputs)

# Masken ausgeben
masks = processor.post_process_masks(
outputs,
original_sizes=[image.size[::-1]],
reshaped_input_sizes=[image.size[::-1]]
)[0]

mask_array = (masks[0][0].cpu().numpy() * 255).astype("uint8")
mask_image = Image.fromarray(mask_array)
return mask_image

demo = gr.Interface(
fn=segment_image,
inputs=gr.Image(type="pil"),
outputs=gr.Image(type="pil"),
title="FishBoost Segment Anything (Meta SAM 2 Demo)",
description="Upload an image and get the segmented result using Meta’s SAM model (CPU compatible)."
)

demo.launch()

Files changed (1) hide show
  1. app.py +0 -27
app.py DELETED
@@ -1,27 +0,0 @@
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()