|
|
import gradio as gr |
|
|
from fastai.vision.all import * |
|
|
import torchvision.transforms as transforms |
|
|
|
|
|
|
|
|
learn = load_learner('mi_modelo.pth') |
|
|
|
|
|
def transform_image(image): |
|
|
my_transforms = transforms.Compose([transforms.ToTensor(), |
|
|
transforms.Normalize( |
|
|
[0.485, 0.456, 0.406], |
|
|
[0.229, 0.224, 0.225])]) |
|
|
image_aux = image |
|
|
return my_transforms(image_aux).unsqueeze(0) |
|
|
|
|
|
|
|
|
def predict(image): |
|
|
|
|
|
image = transforms.Resize((480,640))(image) |
|
|
tensor = transform_image(image=image) |
|
|
|
|
|
|
|
|
with torch.no_grad(): |
|
|
outputs = learn.model(tensor) |
|
|
outputs = torch.argmax(outputs,1) |
|
|
|
|
|
|
|
|
mask = np.array(outputs.cpu()) |
|
|
mask[mask==1]=255 |
|
|
mask=np.reshape(mask,(480,640)) |
|
|
mask = Image.fromarray(mask.astype('uint8')) |
|
|
|
|
|
return mask |
|
|
|
|
|
|
|
|
iface = gr.Interface( |
|
|
fn=predict, |
|
|
inputs=gr.Image(type="pil"), |
|
|
outputs=gr.Image(type="pil"), |
|
|
title="Segmentaci贸n Sem谩ntica", |
|
|
description="Suba una imagen para obtener su m谩scara de segmentaci贸n.", |
|
|
) |
|
|
|
|
|
iface.launch() |