Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,27 @@
|
|
| 1 |
-
from transformers import pipeline
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
-
import numpy as np
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
def remove_background(input_image):
|
| 11 |
-
# Ensure input is a PIL Image
|
| 12 |
-
if isinstance(input_image, np.ndarray):
|
| 13 |
-
input_image = Image.fromarray(input_image)
|
| 14 |
-
|
| 15 |
-
# Get the mask and the processed image
|
| 16 |
-
pillow_mask = pipe(input_image, return_mask=True) # Returns a binary mask
|
| 17 |
-
no_bg_image = pipe(input_image) # Returns the image with background removed
|
| 18 |
-
|
| 19 |
-
# Ensure the output is a PIL Image with transparency
|
| 20 |
-
if isinstance(no_bg_image, np.ndarray):
|
| 21 |
-
no_bg_image = Image.fromarray(no_bg_image)
|
| 22 |
-
|
| 23 |
-
return no_bg_image
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
fn=remove_background,
|
| 28 |
-
inputs=gr.Image(type="pil"
|
| 29 |
-
outputs=gr.Image(type="pil",
|
| 30 |
-
title="
|
| 31 |
-
description="
|
| 32 |
)
|
| 33 |
|
| 34 |
-
# Launch the app
|
| 35 |
if __name__ == "__main__":
|
| 36 |
-
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
from PIL import Image
|
|
|
|
| 4 |
|
| 5 |
+
# Função para remover o background da imagem
|
| 6 |
+
def remove_background(image):
|
| 7 |
+
pipe = pipeline("image-segmentation", model="briaai/RMBG-1.4", trust_remote_code=True)
|
| 8 |
+
|
| 9 |
+
# Obter a máscara da imagem
|
| 10 |
+
pillow_mask = pipe(image, return_mask=True)
|
| 11 |
+
|
| 12 |
+
# Aplicar máscara na imagem original
|
| 13 |
+
pillow_image = pipe(image)
|
| 14 |
|
| 15 |
+
return pillow_image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
# Criar uma interface Gradio
|
| 18 |
+
app = gr.Interface(
|
| 19 |
fn=remove_background,
|
| 20 |
+
inputs=gr.components.Image(type="pil"),
|
| 21 |
+
outputs=gr.components.Image(type="pil", format="png"), # Especificar saída como PNG
|
| 22 |
+
title="Remoção de Background de Imagens",
|
| 23 |
+
description="Envie uma imagem e veja o background sendo removido automaticamente. A imagem resultante será no formato PNG."
|
| 24 |
)
|
| 25 |
|
|
|
|
| 26 |
if __name__ == "__main__":
|
| 27 |
+
app.launch(share=True)
|