inigo99 commited on
Commit
db1cecb
·
1 Parent(s): 6c46e5b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -0
app.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from huggingface_hub import from_pretrained_fastai
2
+ import gradio as gr
3
+ from fastai.all import *
4
+
5
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
6
+ model = torch.jit.load("hrnet.pth")
7
+ model = model.cpu()
8
+
9
+ import torchvision.transforms as transforms
10
+ def transform_image(image):
11
+ my_transforms = transforms.Compose([transforms.ToTensor(),
12
+ transforms.Normalize(
13
+ [0.485, 0.456, 0.406],
14
+ [0.229, 0.224, 0.225])])
15
+ image_aux = image
16
+ return my_transforms(image_aux).unsqueeze(0).to(device)
17
+
18
+ # Definimos una función que se encarga de llevar a cabo las predicciones
19
+ def predict(img):
20
+ image = transforms.Resize((480,640))(img)
21
+ tensor = transform_image(image=image)
22
+ model.to(device)
23
+ with torch.no_grad():
24
+ outputs = model(tensor)
25
+
26
+ outputs = torch.argmax(outputs,1)
27
+
28
+ mask = np.array(outputs.cpu())
29
+ mask[mask==1] = 255
30
+ mask[mask==2] = 150
31
+ mask[mask==3] = 76
32
+ mask[mask==4] = 29
33
+ mask=np.reshape(mask,(480,640))
34
+ return Image.fromarray(mask.astype('uint8'))
35
+
36
+ # Creamos la interfaz y la lanzamos.
37
+ gr.Interface(fn=predict, inputs=gr.inputs.Image(type='filepath'), outputs=gr.outputs.Image(type='pil'), examples=['color_154.jpg','color_155.jpg']).launch(share=False)
38
+