Update app.py
Browse files
app.py
CHANGED
|
@@ -3,16 +3,16 @@ from transformers import SamProcessor, SamModel
|
|
| 3 |
from PIL import Image
|
| 4 |
import torch
|
| 5 |
import numpy as np
|
| 6 |
-
import traceback
|
| 7 |
import random
|
|
|
|
| 8 |
|
| 9 |
-
#
|
| 10 |
model_id = "facebook/sam-vit-base"
|
| 11 |
processor = SamProcessor.from_pretrained(model_id)
|
| 12 |
model = SamModel.from_pretrained(model_id)
|
| 13 |
|
| 14 |
def random_color():
|
| 15 |
-
"""
|
| 16 |
return [random.randint(0, 255) for _ in range(3)]
|
| 17 |
|
| 18 |
def segment_image(image):
|
|
@@ -20,13 +20,11 @@ def segment_image(image):
|
|
| 20 |
device = torch.device("cpu")
|
| 21 |
model.to(device)
|
| 22 |
|
| 23 |
-
# Eingabe vorbereiten
|
| 24 |
inputs = processor(images=image, return_tensors="pt").to(device)
|
| 25 |
|
| 26 |
with torch.no_grad():
|
| 27 |
outputs = model(**inputs)
|
| 28 |
|
| 29 |
-
# Masken verarbeiten (ohne Parameternamen)
|
| 30 |
masks = processor.post_process_masks(
|
| 31 |
outputs.pred_masks.cpu(),
|
| 32 |
inputs["original_sizes"].cpu(),
|
|
@@ -34,34 +32,32 @@ def segment_image(image):
|
|
| 34 |
)
|
| 35 |
|
| 36 |
mask_arrays = masks[0].numpy()
|
| 37 |
-
|
| 38 |
-
# Originalbild als Basis
|
| 39 |
img_array = np.array(image)
|
| 40 |
-
|
| 41 |
|
| 42 |
-
# Jede Maske
|
| 43 |
-
for
|
| 44 |
-
mask =
|
| 45 |
color = random_color()
|
| 46 |
for c in range(3):
|
| 47 |
-
|
| 48 |
|
| 49 |
-
# Farbmischung
|
| 50 |
blended = Image.fromarray(
|
| 51 |
-
(0.
|
| 52 |
)
|
| 53 |
|
| 54 |
return blended
|
| 55 |
|
| 56 |
-
except Exception
|
| 57 |
return f"Fehler:\n{traceback.format_exc()}"
|
| 58 |
|
| 59 |
demo = gr.Interface(
|
| 60 |
fn=segment_image,
|
| 61 |
inputs=gr.Image(type="pil", label="Upload your fish image"),
|
| 62 |
outputs=gr.Image(type="pil", label="Segmented Output"),
|
| 63 |
-
title="FishBoost – Colorful SAM Segmentation (
|
| 64 |
-
description="
|
| 65 |
)
|
| 66 |
|
| 67 |
demo.launch()
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
import torch
|
| 5 |
import numpy as np
|
|
|
|
| 6 |
import random
|
| 7 |
+
import traceback
|
| 8 |
|
| 9 |
+
# Modell laden
|
| 10 |
model_id = "facebook/sam-vit-base"
|
| 11 |
processor = SamProcessor.from_pretrained(model_id)
|
| 12 |
model = SamModel.from_pretrained(model_id)
|
| 13 |
|
| 14 |
def random_color():
|
| 15 |
+
"""Zufällige RGB-Farbe"""
|
| 16 |
return [random.randint(0, 255) for _ in range(3)]
|
| 17 |
|
| 18 |
def segment_image(image):
|
|
|
|
| 20 |
device = torch.device("cpu")
|
| 21 |
model.to(device)
|
| 22 |
|
|
|
|
| 23 |
inputs = processor(images=image, return_tensors="pt").to(device)
|
| 24 |
|
| 25 |
with torch.no_grad():
|
| 26 |
outputs = model(**inputs)
|
| 27 |
|
|
|
|
| 28 |
masks = processor.post_process_masks(
|
| 29 |
outputs.pred_masks.cpu(),
|
| 30 |
inputs["original_sizes"].cpu(),
|
|
|
|
| 32 |
)
|
| 33 |
|
| 34 |
mask_arrays = masks[0].numpy()
|
|
|
|
|
|
|
| 35 |
img_array = np.array(image)
|
| 36 |
+
overlay = np.zeros_like(img_array, dtype=np.uint8)
|
| 37 |
|
| 38 |
+
# Jede Maske farbig einfärben
|
| 39 |
+
for mask in mask_arrays:
|
| 40 |
+
mask = mask[0]
|
| 41 |
color = random_color()
|
| 42 |
for c in range(3):
|
| 43 |
+
overlay[:, :, c] = np.where(mask > 0.5, color[c], overlay[:, :, c])
|
| 44 |
|
| 45 |
+
# Stärkere Farbmischung (80 % Maske / 20 % Original)
|
| 46 |
blended = Image.fromarray(
|
| 47 |
+
(0.2 * img_array + 0.8 * overlay).astype(np.uint8)
|
| 48 |
)
|
| 49 |
|
| 50 |
return blended
|
| 51 |
|
| 52 |
+
except Exception:
|
| 53 |
return f"Fehler:\n{traceback.format_exc()}"
|
| 54 |
|
| 55 |
demo = gr.Interface(
|
| 56 |
fn=segment_image,
|
| 57 |
inputs=gr.Image(type="pil", label="Upload your fish image"),
|
| 58 |
outputs=gr.Image(type="pil", label="Segmented Output"),
|
| 59 |
+
title="FishBoost – Colorful SAM Segmentation (Enhanced Colors)",
|
| 60 |
+
description="Erzeugt kräftige, farbige Masken mit Meta SAM (CPU-Version)."
|
| 61 |
)
|
| 62 |
|
| 63 |
demo.launch()
|