Update app.py
Browse files
app.py
CHANGED
|
@@ -19,7 +19,6 @@ def load_model():
|
|
| 19 |
try:
|
| 20 |
from transformers import SamModel, SamProcessor
|
| 21 |
|
| 22 |
-
# UPGRADE: Large Model für bessere Qualität
|
| 23 |
model_name = "facebook/sam-vit-large"
|
| 24 |
|
| 25 |
processor = SamProcessor.from_pretrained(model_name)
|
|
@@ -55,18 +54,13 @@ def prepare_image(image, max_size=1024):
|
|
| 55 |
return image_pil, image_np
|
| 56 |
|
| 57 |
def refine_mask(mask, kernel_size=5):
|
| 58 |
-
"""Maske glätten und Löcher füllen"""
|
| 59 |
mask_uint8 = (mask > 0).astype(np.uint8) * 255
|
| 60 |
-
|
| 61 |
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (kernel_size, kernel_size))
|
| 62 |
mask_closed = cv2.morphologyEx(mask_uint8, cv2.MORPH_CLOSE, kernel)
|
| 63 |
mask_refined = cv2.morphologyEx(mask_closed, cv2.MORPH_OPEN, kernel)
|
| 64 |
-
|
| 65 |
return mask_refined > 0
|
| 66 |
|
| 67 |
def segment_automatic(image, quality="high"):
|
| 68 |
-
"""Automatische Segmentierung mit Multi-Mask Output"""
|
| 69 |
-
|
| 70 |
if image is None:
|
| 71 |
return None, {"error": "Kein Bild hochgeladen"}
|
| 72 |
|
|
@@ -157,8 +151,6 @@ def segment_automatic(image, quality="high"):
|
|
| 157 |
return image, {"error": str(e)}
|
| 158 |
|
| 159 |
def segment_multi_dense(image, density="medium"):
|
| 160 |
-
"""Multi-Object mit dichterem Grid"""
|
| 161 |
-
|
| 162 |
if image is None:
|
| 163 |
return None, {"error": "Kein Bild"}
|
| 164 |
|
|
@@ -259,12 +251,13 @@ def segment_multi_dense(image, density="medium"):
|
|
| 259 |
return image, {"error": str(e)}
|
| 260 |
|
| 261 |
# Gradio Interface
|
| 262 |
-
|
| 263 |
-
|
|
|
|
| 264 |
gr.Markdown("# 🎨 SAM2 Segmentierung v2.0")
|
| 265 |
-
gr.Markdown("### Verbesserte Qualität mit Large Model
|
| 266 |
|
| 267 |
-
with gr.Tab("🤖 Automatisch
|
| 268 |
with gr.Row():
|
| 269 |
with gr.Column():
|
| 270 |
input_auto = gr.Image(type="pil", label="Bild hochladen")
|
|
@@ -274,8 +267,6 @@ with gr.Blocks(title="SAM2 v2.0", theme=gr.themes.Soft()) as demo:
|
|
| 274 |
label="Qualität"
|
| 275 |
)
|
| 276 |
btn_auto = gr.Button("Segmentieren", variant="primary", size="lg")
|
| 277 |
-
|
| 278 |
-
gr.Markdown("**Verbesserungen:** Large Model, Multi-Mask, Kanten-Glättung")
|
| 279 |
|
| 280 |
with gr.Column():
|
| 281 |
output_auto = gr.Image(label="Ergebnis")
|
|
@@ -287,7 +278,7 @@ with gr.Blocks(title="SAM2 v2.0", theme=gr.themes.Soft()) as demo:
|
|
| 287 |
outputs=[output_auto, json_auto]
|
| 288 |
)
|
| 289 |
|
| 290 |
-
with gr.Tab("🎯 Multi-Region
|
| 291 |
with gr.Row():
|
| 292 |
with gr.Column():
|
| 293 |
input_multi = gr.Image(type="pil", label="Bild hochladen")
|
|
@@ -297,8 +288,6 @@ with gr.Blocks(title="SAM2 v2.0", theme=gr.themes.Soft()) as demo:
|
|
| 297 |
label="Punkt-Dichte"
|
| 298 |
)
|
| 299 |
btn_multi = gr.Button("Alle Bereiche", variant="primary", size="lg")
|
| 300 |
-
|
| 301 |
-
gr.Markdown("**Grid:** High=5x5, Medium=4x4, Low=3x3")
|
| 302 |
|
| 303 |
with gr.Column():
|
| 304 |
output_multi = gr.Image(label="Ergebnis")
|
|
@@ -310,10 +299,28 @@ with gr.Blocks(title="SAM2 v2.0", theme=gr.themes.Soft()) as demo:
|
|
| 310 |
outputs=[output_multi, json_multi]
|
| 311 |
)
|
| 312 |
|
| 313 |
-
with gr.Tab("📡 API
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 318 |
|
| 319 |
-
|
|
|
|
|
|
|
|
|
| 19 |
try:
|
| 20 |
from transformers import SamModel, SamProcessor
|
| 21 |
|
|
|
|
| 22 |
model_name = "facebook/sam-vit-large"
|
| 23 |
|
| 24 |
processor = SamProcessor.from_pretrained(model_name)
|
|
|
|
| 54 |
return image_pil, image_np
|
| 55 |
|
| 56 |
def refine_mask(mask, kernel_size=5):
|
|
|
|
| 57 |
mask_uint8 = (mask > 0).astype(np.uint8) * 255
|
|
|
|
| 58 |
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (kernel_size, kernel_size))
|
| 59 |
mask_closed = cv2.morphologyEx(mask_uint8, cv2.MORPH_CLOSE, kernel)
|
| 60 |
mask_refined = cv2.morphologyEx(mask_closed, cv2.MORPH_OPEN, kernel)
|
|
|
|
| 61 |
return mask_refined > 0
|
| 62 |
|
| 63 |
def segment_automatic(image, quality="high"):
|
|
|
|
|
|
|
| 64 |
if image is None:
|
| 65 |
return None, {"error": "Kein Bild hochgeladen"}
|
| 66 |
|
|
|
|
| 151 |
return image, {"error": str(e)}
|
| 152 |
|
| 153 |
def segment_multi_dense(image, density="medium"):
|
|
|
|
|
|
|
| 154 |
if image is None:
|
| 155 |
return None, {"error": "Kein Bild"}
|
| 156 |
|
|
|
|
| 251 |
return image, {"error": str(e)}
|
| 252 |
|
| 253 |
# Gradio Interface
|
| 254 |
+
demo = gr.Blocks(title="SAM2 v2.0", theme=gr.themes.Soft())
|
| 255 |
+
|
| 256 |
+
with demo:
|
| 257 |
gr.Markdown("# 🎨 SAM2 Segmentierung v2.0")
|
| 258 |
+
gr.Markdown("### Verbesserte Qualität mit Large Model")
|
| 259 |
|
| 260 |
+
with gr.Tab("🤖 Automatisch"):
|
| 261 |
with gr.Row():
|
| 262 |
with gr.Column():
|
| 263 |
input_auto = gr.Image(type="pil", label="Bild hochladen")
|
|
|
|
| 267 |
label="Qualität"
|
| 268 |
)
|
| 269 |
btn_auto = gr.Button("Segmentieren", variant="primary", size="lg")
|
|
|
|
|
|
|
| 270 |
|
| 271 |
with gr.Column():
|
| 272 |
output_auto = gr.Image(label="Ergebnis")
|
|
|
|
| 278 |
outputs=[output_auto, json_auto]
|
| 279 |
)
|
| 280 |
|
| 281 |
+
with gr.Tab("🎯 Multi-Region"):
|
| 282 |
with gr.Row():
|
| 283 |
with gr.Column():
|
| 284 |
input_multi = gr.Image(type="pil", label="Bild hochladen")
|
|
|
|
| 288 |
label="Punkt-Dichte"
|
| 289 |
)
|
| 290 |
btn_multi = gr.Button("Alle Bereiche", variant="primary", size="lg")
|
|
|
|
|
|
|
| 291 |
|
| 292 |
with gr.Column():
|
| 293 |
output_multi = gr.Image(label="Ergebnis")
|
|
|
|
| 299 |
outputs=[output_multi, json_multi]
|
| 300 |
)
|
| 301 |
|
| 302 |
+
with gr.Tab("📡 API"):
|
| 303 |
+
gr.Markdown("### API Endpoint")
|
| 304 |
+
gr.Markdown("URL: https://EnginDev-Boostly.hf.space/api/predict")
|
| 305 |
+
gr.Markdown("### JavaScript Beispiel")
|
| 306 |
+
gr.Code('''
|
| 307 |
+
fetch('https://EnginDev-Boostly.hf.space/api/predict', {
|
| 308 |
+
method: 'POST',
|
| 309 |
+
headers: {'Content-Type': 'application/json'},
|
| 310 |
+
body: JSON.stringify({
|
| 311 |
+
data: [imageBase64, "high"],
|
| 312 |
+
fn_index: 0
|
| 313 |
+
})
|
| 314 |
+
})
|
| 315 |
+
.then(r => r.json())
|
| 316 |
+
.then(result => console.log(result.data));
|
| 317 |
+
''', language="javascript")
|
| 318 |
+
|
| 319 |
+
gr.Markdown("### Parameter")
|
| 320 |
+
gr.Markdown("- fn_index: 0 = Automatisch, 1 = Multi-Region")
|
| 321 |
+
gr.Markdown("- quality: high oder fast")
|
| 322 |
+
gr.Markdown("- density: high, medium, low")
|
| 323 |
|
| 324 |
+
if __name__ == "__main__":
|
| 325 |
+
print("🌐 Launching v2.0...")
|
| 326 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, show_error=True)
|