Update app.py
Browse files
app.py
CHANGED
|
@@ -1,39 +1,47 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import
|
| 3 |
from PIL import Image
|
| 4 |
import torch
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
model_id = "facebook/sam-vit-base"
|
| 8 |
-
processor =
|
| 9 |
model = SamModel.from_pretrained(model_id)
|
| 10 |
|
| 11 |
def segment_image(image):
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
)[0]
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
demo = gr.Interface(
|
| 32 |
fn=segment_image,
|
| 33 |
-
inputs=gr.Image(type="pil"),
|
| 34 |
-
outputs=
|
| 35 |
-
title="FishBoost Segment Anything (Meta SAM
|
| 36 |
-
description="
|
| 37 |
)
|
| 38 |
|
| 39 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoProcessor, SamModel
|
| 3 |
from PIL import Image
|
| 4 |
import torch
|
| 5 |
+
import numpy as np
|
| 6 |
+
import traceback
|
| 7 |
|
| 8 |
+
# CPU-freundliches Modell
|
| 9 |
model_id = "facebook/sam-vit-base"
|
| 10 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
| 11 |
model = SamModel.from_pretrained(model_id)
|
| 12 |
|
| 13 |
def segment_image(image):
|
| 14 |
+
try:
|
| 15 |
+
device = torch.device("cpu")
|
| 16 |
+
model.to(device)
|
| 17 |
+
inputs = processor(images=image, return_tensors="pt").to(device)
|
| 18 |
|
| 19 |
+
with torch.no_grad():
|
| 20 |
+
outputs = model(**inputs)
|
| 21 |
|
| 22 |
+
masks = processor.post_process_masks(
|
| 23 |
+
outputs=outputs,
|
| 24 |
+
original_sizes=[image.size[::-1]],
|
| 25 |
+
reshaped_input_sizes=[image.size[::-1]]
|
| 26 |
+
)
|
|
|
|
| 27 |
|
| 28 |
+
if len(masks) == 0 or "masks" not in masks[0]:
|
| 29 |
+
return "Keine Maske erkannt."
|
| 30 |
+
|
| 31 |
+
mask_array = masks[0]["masks"][0][0].cpu().numpy()
|
| 32 |
+
mask_image = Image.fromarray((mask_array * 255).astype(np.uint8))
|
| 33 |
+
return mask_image
|
| 34 |
+
|
| 35 |
+
except Exception as e:
|
| 36 |
+
# Volle Fehlerausgabe anzeigen
|
| 37 |
+
return f"Fehler:\n{traceback.format_exc()}"
|
| 38 |
|
| 39 |
demo = gr.Interface(
|
| 40 |
fn=segment_image,
|
| 41 |
+
inputs=gr.Image(type="pil", label="Upload your fish image"),
|
| 42 |
+
outputs="text",
|
| 43 |
+
title="FishBoost Segment Anything (Meta SAM CPU Debug)",
|
| 44 |
+
description="Debug-Version: Zeigt genaue Fehlermeldung bei Problemen mit SAM."
|
| 45 |
)
|
| 46 |
|
| 47 |
demo.launch()
|