Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,49 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import pipeline
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
|
|
|
| 5 |
import requests
|
| 6 |
from io import BytesIO
|
| 7 |
|
| 8 |
-
# Ładuj model
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
def remove_bg(image):
|
| 14 |
if image is None:
|
| 15 |
return None
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
# Zastosuj maskę
|
| 22 |
-
result =
|
| 23 |
-
return result
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
fn=remove_bg,
|
| 28 |
-
inputs=gr.Image(type="pil", label="Upload zdjęcie"),
|
| 29 |
-
outputs=gr.Image(type="pil", label="Bez tła (PNG)"),
|
| 30 |
-
title="
|
| 31 |
-
description="
|
| 32 |
examples=[
|
| 33 |
"https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?w=512",
|
| 34 |
"https://images.unsplash.com/photo-1544725176-7c40e5a71c5e?w=512"
|
|
@@ -36,4 +51,4 @@ iface = gr.Interface(
|
|
| 36 |
)
|
| 37 |
|
| 38 |
if __name__ == "__main__":
|
| 39 |
-
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
from PIL import Image
|
| 4 |
+
import onnxruntime as ort
|
| 5 |
import requests
|
| 6 |
from io import BytesIO
|
| 7 |
|
| 8 |
+
# Ładuj model ONNX bezpośrednio (CPU)
|
| 9 |
+
MODEL_ID = "schirrmacher/ormbg"
|
| 10 |
+
ort_session = ort.InferenceSession(f"https://huggingface.co/{MODEL_ID}/resolve/main/model.onnx")
|
| 11 |
+
|
| 12 |
+
def preprocess_image(image):
|
| 13 |
+
if isinstance(image, str): # URL
|
| 14 |
+
image = Image.open(requests.get(image, stream=True).raw)
|
| 15 |
+
image = image.convert("RGB").resize((1024, 1024))
|
| 16 |
+
img_array = np.array(image).astype(np.float32) / 255.0
|
| 17 |
+
img_array = np.transpose(img_array, (2, 0, 1)) # HWC -> CHW
|
| 18 |
+
img_array = np.expand_dims(img_array, 0) # Batch
|
| 19 |
+
return img_array
|
| 20 |
|
| 21 |
def remove_bg(image):
|
| 22 |
if image is None:
|
| 23 |
return None
|
| 24 |
|
| 25 |
+
# Preprocess
|
| 26 |
+
input_tensor = preprocess_image(image)
|
| 27 |
+
|
| 28 |
+
# Inferencja ONNX
|
| 29 |
+
outputs = ort_session.run(None, {"input": input_tensor})
|
| 30 |
+
mask = outputs[0][0, 0] # [H, W]
|
| 31 |
+
|
| 32 |
+
# Postprocess
|
| 33 |
+
mask = (mask > 0.5).astype(np.uint8) * 255
|
| 34 |
+
mask_pil = Image.fromarray(mask).resize(image.size)
|
| 35 |
|
| 36 |
# Zastosuj maskę
|
| 37 |
+
result = np.array(image) * np.stack([mask_pil, mask_pil, mask_pil], axis=2)
|
| 38 |
+
return Image.fromarray(result.astype(np.uint8))
|
| 39 |
|
| 40 |
+
# Gradio UI
|
| 41 |
+
demo = gr.Interface(
|
| 42 |
fn=remove_bg,
|
| 43 |
+
inputs=gr.Image(type="pil", label="📁 Upload zdjęcie"),
|
| 44 |
+
outputs=gr.Image(type="pil", label="✅ Bez tła (PNG)"),
|
| 45 |
+
title="⚡ ORMBG - Szybki CPU Background Remover",
|
| 46 |
+
description="schirrmacher/ormbg (~350ms CPU) - precyzyjne krawędzie, włos/futro",
|
| 47 |
examples=[
|
| 48 |
"https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?w=512",
|
| 49 |
"https://images.unsplash.com/photo-1544725176-7c40e5a71c5e?w=512"
|
|
|
|
| 51 |
)
|
| 52 |
|
| 53 |
if __name__ == "__main__":
|
| 54 |
+
demo.launch()
|