Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from fastai.vision.all import *
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from huggingface_hub import hf_hub_download
|
| 6 |
+
import torchvision.transforms as transforms
|
| 7 |
+
import torch
|
| 8 |
+
|
| 9 |
+
# Cargar modelo desde Hugging Face Hub
|
| 10 |
+
model_path = hf_hub_download(repo_id="PablitoGil14/AP-Practica3", filename="model.pkl")
|
| 11 |
+
learn = load_learner(model_path)
|
| 12 |
+
|
| 13 |
+
def segmentar(img: Image.Image):
|
| 14 |
+
img = img.resize((640, 480))
|
| 15 |
+
x = transforms.Compose([
|
| 16 |
+
transforms.ToTensor(),
|
| 17 |
+
transforms.Normalize(*imagenet_stats)
|
| 18 |
+
])(img).unsqueeze(0)
|
| 19 |
+
|
| 20 |
+
with torch.no_grad():
|
| 21 |
+
preds = learn.model.eval()(x)
|
| 22 |
+
mask = torch.argmax(preds, dim=1).squeeze().cpu().numpy()
|
| 23 |
+
|
| 24 |
+
# Asignar colores según los valores de clase
|
| 25 |
+
out_mask = np.zeros_like(mask, dtype=np.uint8)
|
| 26 |
+
out_mask[mask == 1] = 255
|
| 27 |
+
out_mask[mask == 2] = 150
|
| 28 |
+
out_mask[mask == 3] = 29
|
| 29 |
+
out_mask[mask == 4] = 74
|
| 30 |
+
return Image.fromarray(out_mask)
|
| 31 |
+
|
| 32 |
+
# Interfaz de Gradio
|
| 33 |
+
demo = gr.Interface(
|
| 34 |
+
fn=segmentar,
|
| 35 |
+
inputs=gr.Image(type="pil"),
|
| 36 |
+
outputs=gr.Image(type="pil"),
|
| 37 |
+
title="Segmentador de Viñedos",
|
| 38 |
+
description="Sube una imagen y el modelo segmentará racimos de uva, hojas, madera y postes."
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
demo.launch()
|