Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,57 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import numpy as np
|
| 3 |
import torch
|
|
|
|
|
|
|
|
|
|
| 4 |
from inference import load_model, predict
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
fn=segment_instance,
|
| 27 |
-
inputs=gr.Image(type="pil", label="
|
| 28 |
-
outputs=gr.Image(type="
|
| 29 |
-
title=
|
| 30 |
-
description=
|
| 31 |
)
|
| 32 |
|
| 33 |
if __name__ == "__main__":
|
| 34 |
-
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
import torch
|
| 4 |
+
import numpy as np
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from huggingface_hub import hf_hub_download
|
| 7 |
from inference import load_model, predict
|
| 8 |
+
import traceback
|
| 9 |
+
|
| 10 |
+
# 1) Baixa o peso do modelo do Hugging Face Model Hub
|
| 11 |
+
model_path = hf_hub_download(
|
| 12 |
+
repo_id="vncgabriel/instancia-segmentation-model",
|
| 13 |
+
filename="pytorch_model.bin",
|
| 14 |
+
repo_type="model"
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
# 2) Carrega o modelo (na CPU ou GPU)
|
| 18 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 19 |
+
model = load_model(model_path, device=device)
|
| 20 |
+
|
| 21 |
+
def segment_instance(image: Image.Image):
|
| 22 |
+
try:
|
| 23 |
+
# 3) Preprocessamento: PIL->numpy->[0,1]->tensor [C,H,W]
|
| 24 |
+
img = image.convert("RGB") # garante 3 canais
|
| 25 |
+
arr = np.array(img).astype(np.float32) / 255.0
|
| 26 |
+
tensor = torch.from_numpy(arr).permute(2, 0, 1).to(device)
|
| 27 |
|
| 28 |
+
# 4) Inferência
|
| 29 |
+
mask = predict(model, tensor).cpu().numpy()
|
| 30 |
+
mask = (mask > 0.5).astype(np.uint8) * 255 # binariza e escala
|
| 31 |
+
|
| 32 |
+
# 5) Cria overlay vermelho semi-transparente
|
| 33 |
+
overlay = img.convert("RGBA")
|
| 34 |
+
mask_img = Image.fromarray(mask).convert("L")
|
| 35 |
+
red_layer = Image.new("RGBA", overlay.size, (255, 0, 0, 100))
|
| 36 |
+
overlay.paste(red_layer, mask=mask_img)
|
| 37 |
+
|
| 38 |
+
return overlay
|
| 39 |
+
|
| 40 |
+
except Exception as e:
|
| 41 |
+
# Log completo para o console do Space
|
| 42 |
+
print(traceback.format_exc())
|
| 43 |
+
# Mostra mensagem de erro no frontend
|
| 44 |
+
return None, str(e)
|
| 45 |
+
|
| 46 |
+
# 6) Monta a interface
|
| 47 |
+
iface = gr.Interface(
|
| 48 |
fn=segment_instance,
|
| 49 |
+
inputs=gr.Image(type="pil", label="Input Image"),
|
| 50 |
+
outputs=gr.Image(type="pil", label="Segmented Overlay"),
|
| 51 |
+
title="Segmentação de Instâncias",
|
| 52 |
+
description="Envie uma imagem e veja os objetos segmentados em vermelho."
|
| 53 |
)
|
| 54 |
|
| 55 |
if __name__ == "__main__":
|
| 56 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|
| 57 |
+
|