Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +30 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
# Obter a máscara da imagem
|
| 9 |
+
segmentation = pipe(image)
|
| 10 |
+
# Aplicar a máscara para remover o fundo
|
| 11 |
+
# Filtrando a parte da segmentação onde a label é "person" ou qualquer outra desejada
|
| 12 |
+
mask = Image.fromarray(segmentation[0]['mask']) # Segmentação com maior probabilidade
|
| 13 |
+
original_image = Image.fromarray(image)
|
| 14 |
+
|
| 15 |
+
# Remover fundo
|
| 16 |
+
output = Image.composite(original_image, Image.new("RGBA", original_image.size), mask)
|
| 17 |
+
return output
|
| 18 |
+
|
| 19 |
+
# Criar a interface Gradio
|
| 20 |
+
app = gr.Interface(
|
| 21 |
+
fn=remove_background,
|
| 22 |
+
inputs=gr.Image(type="pil"),
|
| 23 |
+
outputs=gr.Image(type="pil", format="png"), # Especificar a saída como PNG
|
| 24 |
+
title="Remoção de Background de Imagens",
|
| 25 |
+
description="Envie uma imagem e veja o background sendo removido automaticamente. A imagem resultante será no formato PNG."
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
# Executar a aplicação
|
| 29 |
+
if __name__ == "__main__":
|
| 30 |
+
app.launch(share=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
transformers
|
| 3 |
+
torch
|
| 4 |
+
Pillow
|