Ignaciobfp commited on
Commit
3185fbd
·
1 Parent(s): 6b6fe6a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -0
app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from huggingface_hub import from_pretrained_fastai
2
+ import gradio as gr
3
+ from fastai.vision.all import *
4
+ import PIL
5
+ import torchvision.transforms as transforms
6
+
7
+ #repo_id = "Ignaciobfp/segmentacion-dron-marras"
8
+ #learner = from_pretrained_fastai(repo_id)
9
+
10
+ device = torch.device("cpu")
11
+ #model = learner.model
12
+ model = torch.jit.load("pr3.pth")
13
+ model = model.cpu()
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
+ return my_transforms(image_aux).unsqueeze(0).to(device)
22
+
23
+
24
+ # Definimos una función que se encarga de llevar a cabo las predicciones
25
+ def predict(img):
26
+ img_pil = PIL.Image.fromarray(img, 'RGB')
27
+ image = transforms.Resize((400,400))(img_pil)
28
+ tensor = transform_image(image=image)
29
+ model.to(device)
30
+ with torch.no_grad():
31
+ outputs = model(tensor)
32
+ outputs = torch.argmax(outputs,1)
33
+ mask = np.array(outputs.cpu())
34
+ mask[mask==1]=255
35
+ mask=np.reshape(mask,(400,400))
36
+ return Image.fromarray(mask.astype('uint8'))
37
+
38
+ # Creamos la interfaz y la lanzamos.
39
+ gr.Interface(fn=predict, inputs=gr.inputs.Image(shape=(400, 400)), outputs=gr.outputs.Image(type="pil"), examples=['examples/1CA SUR_1200_800.png', 'examples/1CA SUR_4000_1200.png', 'examples/1CA SUR_4800_2000.png']).launch(share=False)
40
+
41
+