luis56125 commited on
Commit
c420df7
1 Parent(s): 6ef0a15

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -22
app.py CHANGED
@@ -1,33 +1,26 @@
1
  import gradio as gr
2
  from fastai.vision.all import *
3
-
4
- from pathlib import Path
5
- import PIL
6
  import torchvision.transforms as transforms
7
-
8
-
 
9
 
10
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
11
  model = torch.jit.load("unet.pth")
12
- model = model.cpu()
13
  model.eval()
14
 
15
  def transform_image(image):
16
- my_transforms = transforms.Compose([transforms.ToTensor(),
17
- transforms.Normalize(
18
- [0.485, 0.456, 0.406],
19
- [0.229, 0.224, 0.225])])
20
- image_aux = image
21
  image = transforms.Resize((480,640))(Image.fromarray(image))
22
- tensor = my_transforms(image_aux).unsqueeze(0).to(device)
23
-
24
 
25
- model.to(device)
26
  with torch.no_grad():
27
  outputs = model(tensor)
28
-
29
- outputs = torch.argmax(outputs,1)
30
-
31
 
32
  mask = np.array(outputs.cpu())
33
  mask[mask==0]=255
@@ -36,10 +29,12 @@ def transform_image(image):
36
  mask[mask==3]=25
37
  mask[mask==4]=0
38
 
39
- mask=np.reshape(mask,(480,640))
40
  return Image.fromarray(mask.astype('uint8'))
41
 
42
-
43
-
44
- # Creamos la interfaz y la lanzamos.
45
- gr.Interface(fn=transform_image, inputs=gr.inputs.Image(shape=(640, 480)), outputs=gr.outputs.Image(),examples=['color_154.jpg','color_189.jpg']).launch(share=False)
 
 
 
1
  import gradio as gr
2
  from fastai.vision.all import *
 
 
 
3
  import torchvision.transforms as transforms
4
+ import torch
5
+ from PIL import Image
6
+ import numpy as np
7
 
8
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
9
  model = torch.jit.load("unet.pth")
10
+ model = model.to(device)
11
  model.eval()
12
 
13
  def transform_image(image):
14
+ my_transforms = transforms.Compose([
15
+ transforms.ToTensor(),
16
+ transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
17
+ ])
 
18
  image = transforms.Resize((480,640))(Image.fromarray(image))
19
+ tensor = my_transforms(image).unsqueeze(0).to(device)
 
20
 
 
21
  with torch.no_grad():
22
  outputs = model(tensor)
23
+ outputs = torch.argmax(outputs, 1)
 
 
24
 
25
  mask = np.array(outputs.cpu())
26
  mask[mask==0]=255
 
29
  mask[mask==3]=25
30
  mask[mask==4]=0
31
 
32
+ mask = np.reshape(mask, (480, 640))
33
  return Image.fromarray(mask.astype('uint8'))
34
 
35
+ # Modificando la creación de la interfaz para usar gr.components en lugar de gr.inputs y gr.outputs
36
+ interface = gr.Interface(fn=transform_image,
37
+ inputs=gr.components.Image(shape=(640, 480)),
38
+ outputs=gr.components.Image(),
39
+ examples=['color_154.jpg', 'color_189.jpg'])
40
+ interface.launch(share=False)