Update app.py
Browse files
app.py
CHANGED
|
@@ -2,27 +2,32 @@ import gradio as gr
|
|
| 2 |
import numpy as np
|
| 3 |
import torch
|
| 4 |
import cv2
|
| 5 |
-
from segment_anything import sam_model_registry, SamAutomaticMaskGenerator
|
| 6 |
from PIL import Image
|
| 7 |
import os
|
| 8 |
import urllib.request
|
| 9 |
|
| 10 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
MODEL_URL = "https://dl.fbaipublicfiles.com/segment_anything/sam_vit_b_01ec64.pth"
|
| 12 |
MODEL_PATH = "sam_vit_b_01ec64.pth"
|
| 13 |
|
| 14 |
-
#
|
| 15 |
if not os.path.exists(MODEL_PATH):
|
| 16 |
print("Modell wird heruntergeladen...")
|
| 17 |
urllib.request.urlretrieve(MODEL_URL, MODEL_PATH)
|
| 18 |
-
print("
|
| 19 |
|
| 20 |
-
#
|
| 21 |
model_type = "vit_b"
|
| 22 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 23 |
-
|
| 24 |
sam = sam_model_registry[model_type](checkpoint=MODEL_PATH)
|
| 25 |
sam.to(device=device)
|
|
|
|
| 26 |
mask_generator = SamAutomaticMaskGenerator(sam)
|
| 27 |
|
| 28 |
def segment_all_objects(image):
|
|
@@ -35,7 +40,7 @@ def segment_all_objects(image):
|
|
| 35 |
color = np.random.randint(0, 255, size=(3,))
|
| 36 |
overlay[m] = overlay[m] * 0.3 + color * 0.7
|
| 37 |
|
| 38 |
-
#
|
| 39 |
y, x = np.where(m)
|
| 40 |
if len(x) > 0 and len(y) > 0:
|
| 41 |
cx, cy = int(np.mean(x)), int(np.mean(y))
|
|
@@ -49,7 +54,7 @@ demo = gr.Interface(
|
|
| 49 |
inputs=gr.Image(type="pil", label="Bild hochladen"),
|
| 50 |
outputs=gr.Image(type="pil", label="Segmentiertes Ergebnis"),
|
| 51 |
title="FishBoost SAM2 Segmentierung",
|
| 52 |
-
description="Automatische Segmentierung aller Objekte im Bild
|
| 53 |
)
|
| 54 |
|
| 55 |
demo.launch()
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import torch
|
| 4 |
import cv2
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
import os
|
| 7 |
import urllib.request
|
| 8 |
|
| 9 |
+
# SAM importieren
|
| 10 |
+
try:
|
| 11 |
+
from segment_anything import sam_model_registry, SamAutomaticMaskGenerator
|
| 12 |
+
except ImportError:
|
| 13 |
+
raise ImportError("Bitte stelle sicher, dass 'segment-anything' in requirements.txt steht.")
|
| 14 |
+
|
| 15 |
+
# Modellpfad & Download-Link
|
| 16 |
MODEL_URL = "https://dl.fbaipublicfiles.com/segment_anything/sam_vit_b_01ec64.pth"
|
| 17 |
MODEL_PATH = "sam_vit_b_01ec64.pth"
|
| 18 |
|
| 19 |
+
# Modell automatisch laden, falls nicht vorhanden
|
| 20 |
if not os.path.exists(MODEL_PATH):
|
| 21 |
print("Modell wird heruntergeladen...")
|
| 22 |
urllib.request.urlretrieve(MODEL_URL, MODEL_PATH)
|
| 23 |
+
print("Download abgeschlossen.")
|
| 24 |
|
| 25 |
+
# SAM laden
|
| 26 |
model_type = "vit_b"
|
| 27 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
| 28 |
sam = sam_model_registry[model_type](checkpoint=MODEL_PATH)
|
| 29 |
sam.to(device=device)
|
| 30 |
+
|
| 31 |
mask_generator = SamAutomaticMaskGenerator(sam)
|
| 32 |
|
| 33 |
def segment_all_objects(image):
|
|
|
|
| 40 |
color = np.random.randint(0, 255, size=(3,))
|
| 41 |
overlay[m] = overlay[m] * 0.3 + color * 0.7
|
| 42 |
|
| 43 |
+
# Text auf Maske
|
| 44 |
y, x = np.where(m)
|
| 45 |
if len(x) > 0 and len(y) > 0:
|
| 46 |
cx, cy = int(np.mean(x)), int(np.mean(y))
|
|
|
|
| 54 |
inputs=gr.Image(type="pil", label="Bild hochladen"),
|
| 55 |
outputs=gr.Image(type="pil", label="Segmentiertes Ergebnis"),
|
| 56 |
title="FishBoost SAM2 Segmentierung",
|
| 57 |
+
description="Automatische Segmentierung aller Objekte im Bild (Meta SAM). CPU-kompatibel."
|
| 58 |
)
|
| 59 |
|
| 60 |
demo.launch()
|