Update app.py
Browse files
app.py
CHANGED
@@ -1,49 +1,48 @@
|
|
1 |
-
|
2 |
-
import gradio as gr
|
3 |
from fastai.vision.all import *
|
4 |
-
|
|
|
5 |
from pathlib import Path
|
6 |
import PIL
|
7 |
-
import torchvision.transforms as transforms
|
8 |
-
|
9 |
-
|
10 |
|
11 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
12 |
model = torch.jit.load("unet.pth")
|
13 |
model = model.cpu()
|
14 |
model.eval()
|
15 |
|
16 |
-
def transform_image(image):
|
17 |
-
my_transforms = transforms.Compose([transforms.ToTensor(),
|
18 |
-
transforms.Normalize(
|
19 |
-
[0.485, 0.456, 0.406],
|
20 |
-
[0.229, 0.224, 0.225])])
|
21 |
-
image_aux = image
|
22 |
-
|
23 |
-
image = transforms.Resize((480,640))(Image.fromarray(image))
|
24 |
-
tensor = my_transforms(image_aux).unsqueeze(0).to(device)
|
25 |
|
|
|
26 |
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
outputs = model(tensor)
|
30 |
-
|
31 |
-
outputs = torch.argmax(outputs,1)
|
32 |
-
|
33 |
-
|
34 |
-
mask
|
35 |
-
mask[mask==
|
36 |
-
mask[mask==
|
37 |
-
mask[mask==
|
38 |
-
mask[mask==
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
return Image.fromarray(mask.astype('uint8'))
|
43 |
-
|
44 |
|
45 |
|
46 |
# Creamos la interfaz y la lanzamos.
|
47 |
-
gr.Interface(fn=transform_image, inputs=gr.inputs.Image(shape=(640, 480)), outputs=gr.outputs.Image(),examples=['
|
48 |
-
|
49 |
|
|
|
|
|
|
|
1 |
from fastai.vision.all import *
|
2 |
+
import gradio as gr
|
3 |
+
import torchvision.transforms as transforms
|
4 |
from pathlib import Path
|
5 |
import PIL
|
|
|
|
|
|
|
6 |
|
7 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
8 |
model = torch.jit.load("unet.pth")
|
9 |
model = model.cpu()
|
10 |
model.eval()
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
+
def transform_image(image):
|
14 |
|
15 |
+
#mask = PILMask.create(Path(str(image).replace("Images","Labels").replace("color","gt").replace(".jpg",".png")))
|
16 |
+
#image = PIL.Image.open(image)
|
17 |
+
my_transforms = transforms.Compose([transforms.ToTensor(),
|
18 |
+
transforms.Normalize(
|
19 |
+
[0.485, 0.456, 0.406],
|
20 |
+
[0.229, 0.224, 0.225])])
|
21 |
+
image_aux = image
|
22 |
+
#my_transforms(image_aux).unsqueeze(0).to(device)
|
23 |
+
image = transforms.Resize((480,640))(Image.fromarray(image))
|
24 |
+
tensor = my_transforms(image_aux).unsqueeze(0).to(device)
|
25 |
+
#tensor = transform_image(image=image)
|
26 |
+
|
27 |
+
|
28 |
+
|
29 |
+
model.to(device)
|
30 |
+
with torch.no_grad():
|
31 |
outputs = model(tensor)
|
32 |
+
|
33 |
+
outputs = torch.argmax(outputs,1)
|
34 |
+
|
35 |
+
mask = np.array(outputs.cpu())
|
36 |
+
mask[mask==0]=255
|
37 |
+
mask[mask==1]=150
|
38 |
+
mask[mask==2]=76
|
39 |
+
mask[mask==3]=25
|
40 |
+
mask[mask==4]=0
|
41 |
+
|
42 |
+
mask=np.reshape(mask,(480,640))
|
43 |
+
return Image.fromarray(mask.astype('uint8'))
|
|
|
|
|
44 |
|
45 |
|
46 |
# Creamos la interfaz y la lanzamos.
|
47 |
+
gr.Interface(fn=transform_image, inputs=gr.inputs.Image(shape=(640, 480)), outputs=gr.outputs.Image(),examples=['color_156.jpg','color_179.jpg']).launch(share=False)
|
|
|
48 |
|