Delete app.py
Browse files
app.py
DELETED
|
@@ -1,60 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 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):
|
| 34 |
-
image_np = np.array(image)
|
| 35 |
-
masks = mask_generator.generate(image_np)
|
| 36 |
-
|
| 37 |
-
overlay = image_np.copy()
|
| 38 |
-
for i, mask in enumerate(masks):
|
| 39 |
-
m = mask["segmentation"]
|
| 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))
|
| 47 |
-
cv2.putText(overlay, f"Obj {i+1}", (cx, cy),
|
| 48 |
-
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
|
| 49 |
-
|
| 50 |
-
return Image.fromarray(overlay.astype(np.uint8))
|
| 51 |
-
|
| 52 |
-
demo = gr.Interface(
|
| 53 |
-
fn=segment_all_objects,
|
| 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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|