Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from fastai.vision.all import *
|
| 3 |
+
import torchvision.transforms as transforms
|
| 4 |
+
|
| 5 |
+
# Cargar el modelo exportado
|
| 6 |
+
learn = load_learner('mi_modelo.pth')
|
| 7 |
+
|
| 8 |
+
def transform_image(image):
|
| 9 |
+
my_transforms = transforms.Compose([transforms.ToTensor(),
|
| 10 |
+
transforms.Normalize(
|
| 11 |
+
[0.485, 0.456, 0.406],
|
| 12 |
+
[0.229, 0.224, 0.225])])
|
| 13 |
+
image_aux = image
|
| 14 |
+
return my_transforms(image_aux).unsqueeze(0)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def predict(image):
|
| 18 |
+
# Preprocesamiento de la imagen
|
| 19 |
+
image = transforms.Resize((480,640))(image)
|
| 20 |
+
tensor = transform_image(image=image)
|
| 21 |
+
|
| 22 |
+
# Predicci贸n
|
| 23 |
+
with torch.no_grad():
|
| 24 |
+
outputs = learn.model(tensor) # Usamos learn.model para acceder al modelo subyacente
|
| 25 |
+
outputs = torch.argmax(outputs,1)
|
| 26 |
+
|
| 27 |
+
# Postprocesamiento
|
| 28 |
+
mask = np.array(outputs.cpu())
|
| 29 |
+
mask[mask==1]=255
|
| 30 |
+
mask=np.reshape(mask,(480,640))
|
| 31 |
+
mask = Image.fromarray(mask.astype('uint8'))
|
| 32 |
+
|
| 33 |
+
return mask
|
| 34 |
+
|
| 35 |
+
# Crear interfaz
|
| 36 |
+
iface = gr.Interface(
|
| 37 |
+
fn=predict,
|
| 38 |
+
inputs=gr.Image(type="pil"),
|
| 39 |
+
outputs=gr.Image(type="pil"),
|
| 40 |
+
title="Segmentaci贸n Sem谩ntica",
|
| 41 |
+
description="Suba una imagen para obtener su m谩scara de segmentaci贸n.",
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
iface.launch()
|