Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,28 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
-
import
|
| 4 |
|
| 5 |
-
# Carica modello
|
| 6 |
-
|
| 7 |
-
model = AutoModelForImageSegmentation.from_pretrained("BritishWerewolf/U-2-Netp")
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
mask =
|
|
|
|
| 17 |
|
| 18 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
from PIL import Image
|
| 4 |
+
import numpy as np
|
| 5 |
|
| 6 |
+
# 1) Carica il modello di segmentazione
|
| 7 |
+
seg = pipeline("image-segmentation", model="BritishWerewolf/U-2-Netp", framework="pt")
|
|
|
|
| 8 |
|
| 9 |
+
def remove_bg(image: Image.Image):
|
| 10 |
+
# 2) Ottieni la maschera
|
| 11 |
+
outputs = seg(image)
|
| 12 |
+
mask = outputs[0]["mask"].astype(np.uint8) * 255
|
| 13 |
|
| 14 |
+
# 3) Applica la maschera all’alpha channel
|
| 15 |
+
rgba = image.convert("RGBA")
|
| 16 |
+
rgba_arr = np.array(rgba)
|
| 17 |
+
rgba_arr[..., 3] = mask # canale alpha = mask
|
| 18 |
+
return Image.fromarray(rgba_arr)
|
| 19 |
|
| 20 |
+
# 4) Definisci l’interfaccia “chat”
|
| 21 |
+
with gr.Blocks() as demo:
|
| 22 |
+
chatbot = gr.Chatbot(label="BG-Removal Bot")
|
| 23 |
+
img_in = gr.Image(type="pil", label="Invia la tua foto")
|
| 24 |
+
img_out = gr.Image(type="pil", label="Risultato")
|
| 25 |
+
|
| 26 |
+
img_in.change(fn=remove_bg, inputs=img_in, outputs=img_out)
|
| 27 |
+
|
| 28 |
+
demo.launch()
|