File size: 1,252 Bytes
569737e
2e3563e
 
c420df7
 
 
03c8d09
 
633d3c4
c420df7
03c8d09
 
569737e
c420df7
 
 
 
633d3c4
c420df7
633d3c4
c71ac74
 
c420df7
633d3c4
 
 
 
 
 
 
 
c420df7
c71ac74
 
e614339
c420df7
6df61ab
c420df7
 
6df61ab
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import gradio as gr
from fastai.vision.all import *
import torchvision.transforms as transforms
import torch
from PIL import Image
import numpy as np

device = torch.device("cuda" if torch.cuda.is_available() else "cpu") 
model = torch.jit.load("unet.pth")
model = model.to(device)
model.eval()

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 = transforms.Resize((480,640))(Image.fromarray(image))
    tensor = my_transforms(image).unsqueeze(0).to(device)

    with torch.no_grad():
        outputs = model(tensor)
    outputs = torch.argmax(outputs, 1)

    mask = np.array(outputs.cpu())
    mask[mask==0]=255
    mask[mask==1]=150
    mask[mask==2]=76
    mask[mask==3]=25
    mask[mask==4]=0

    mask = np.reshape(mask, (480, 640))
    return Image.fromarray(mask.astype('uint8'))

# Ajuste de la creación de la interfaz 
interface = gr.Interface(fn=transform_image, 
                         inputs=gr.components.Image(width=640, height=480), 
                         outputs=gr.components.Image(),
                         examples=['color_154.jpg', 'color_189.jpg'])
interface.launch(share=False)