Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
from huggingface_hub import hf_hub_download
|
|
@@ -6,7 +5,7 @@ from inference import load_model, predict
|
|
| 6 |
from PIL import Image
|
| 7 |
import numpy as np
|
| 8 |
|
| 9 |
-
# 1. Baixa
|
| 10 |
model_path = hf_hub_download(
|
| 11 |
repo_id="vncgabriel/instancia-segmentation-model",
|
| 12 |
filename="pytorch_model.pth",
|
|
@@ -23,32 +22,26 @@ def segment(image: Image.Image):
|
|
| 23 |
Recebe uma PIL Image, converte para tensor, normaliza e gera máscara.
|
| 24 |
Retorna imagem original sobreposta com a máscara.
|
| 25 |
"""
|
| 26 |
-
# Converter PIL -> np.array -> tensor
|
| 27 |
img_arr = np.array(image).astype(np.float32) / 255.0
|
| 28 |
-
# formato HWC -> CHW
|
| 29 |
img_tensor = torch.from_numpy(img_arr).permute(2, 0, 1).to(device)
|
| 30 |
|
| 31 |
-
# predição
|
| 32 |
mask = predict(model, img_tensor).cpu().numpy()
|
| 33 |
-
# máscara binária
|
| 34 |
bin_mask = (mask > 0.5).astype(np.uint8) * 255
|
| 35 |
|
| 36 |
-
# sobrepor máscara vermelha semitransparente
|
| 37 |
overlay = image.convert("RGBA")
|
| 38 |
mask_img = Image.fromarray(bin_mask).convert("L")
|
| 39 |
red = Image.new("RGBA", image.size, color=(255, 0, 0, 100))
|
| 40 |
overlay.paste(red, mask=mask_img)
|
| 41 |
return overlay
|
| 42 |
|
| 43 |
-
# 4.
|
| 44 |
iface = gr.Interface(
|
| 45 |
fn=segment,
|
| 46 |
inputs=gr.Image(type="pil"),
|
| 47 |
outputs=gr.Image(type="pil"),
|
| 48 |
-
title="
|
| 49 |
-
description="
|
| 50 |
examples=[
|
| 51 |
-
# opcional: caminhos locais ou URLs para imagens de exemplo
|
| 52 |
["example1.jpg"],
|
| 53 |
["example2.jpg"],
|
| 54 |
],
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from huggingface_hub import hf_hub_download
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
import numpy as np
|
| 7 |
|
| 8 |
+
# 1. Baixa os pesos do modelo diretamente do Hugging Face Hub
|
| 9 |
model_path = hf_hub_download(
|
| 10 |
repo_id="vncgabriel/instancia-segmentation-model",
|
| 11 |
filename="pytorch_model.pth",
|
|
|
|
| 22 |
Recebe uma PIL Image, converte para tensor, normaliza e gera máscara.
|
| 23 |
Retorna imagem original sobreposta com a máscara.
|
| 24 |
"""
|
|
|
|
| 25 |
img_arr = np.array(image).astype(np.float32) / 255.0
|
|
|
|
| 26 |
img_tensor = torch.from_numpy(img_arr).permute(2, 0, 1).to(device)
|
| 27 |
|
|
|
|
| 28 |
mask = predict(model, img_tensor).cpu().numpy()
|
|
|
|
| 29 |
bin_mask = (mask > 0.5).astype(np.uint8) * 255
|
| 30 |
|
|
|
|
| 31 |
overlay = image.convert("RGBA")
|
| 32 |
mask_img = Image.fromarray(bin_mask).convert("L")
|
| 33 |
red = Image.new("RGBA", image.size, color=(255, 0, 0, 100))
|
| 34 |
overlay.paste(red, mask=mask_img)
|
| 35 |
return overlay
|
| 36 |
|
| 37 |
+
# 4. Interface Gradio
|
| 38 |
iface = gr.Interface(
|
| 39 |
fn=segment,
|
| 40 |
inputs=gr.Image(type="pil"),
|
| 41 |
outputs=gr.Image(type="pil"),
|
| 42 |
+
title="Segmentação de Instâncias",
|
| 43 |
+
description="Envie uma imagem e obtenha a segmentação de instâncias usando UNet pré-treinado.",
|
| 44 |
examples=[
|
|
|
|
| 45 |
["example1.jpg"],
|
| 46 |
["example2.jpg"],
|
| 47 |
],
|