Ignaciobfp commited on
Commit
5c6cca8
1 Parent(s): 8fed3e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -29
app.py CHANGED
@@ -4,23 +4,12 @@ from fastai.vision.all import *
4
  import PIL
5
  import torchvision.transforms as transforms
6
 
7
- class TargetMaskConvertTransform(ItemTransform):
8
- def __init__(self):
9
- pass
10
- def encodes(self, x):
11
- img,mask = x
12
-
13
- #Convert to array
14
- mask = np.array(mask)
15
-
16
- mask[mask!=255]=0
17
- # Change 255 for 1
18
- mask[mask==255]=1
19
-
20
-
21
- # Back to PILMask
22
- mask = PILMask.create(mask)
23
- return img, mask
24
 
25
  def transform_image(image):
26
  my_transforms = transforms.Compose([transforms.ToTensor(),
@@ -30,20 +19,11 @@ def transform_image(image):
30
  image_aux = image
31
  return my_transforms(image_aux).unsqueeze(0).to(device)
32
 
33
- repo_id = "Ignaciobfp/segmentacion-dron-marras"
34
-
35
- learner = from_pretrained_fastai(repo_id)
36
-
37
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
38
- model = learner.model
39
- model = model.cpu()
40
-
41
-
42
 
43
  # Definimos una funci贸n que se encarga de llevar a cabo las predicciones
44
  def predict(img):
45
- #img = PILImage.create(img)
46
- image = transforms.Resize((400,400))(img)
47
  tensor = transform_image(image=image)
48
  model.to(device)
49
  with torch.no_grad():
@@ -52,7 +32,7 @@ def predict(img):
52
  mask = np.array(outputs.cpu())
53
  mask[mask==1]=255
54
  mask=np.reshape(mask,(400,400))
55
- return mask
56
 
57
  # Creamos la interfaz y la lanzamos.
58
  gr.Interface(fn=predict, inputs=gr.inputs.Image(shape=(400, 400)), outputs="image", examples=['examples/1CA SUR_1200_800.png', 'examples/1CA SUR_4000_1200.png', 'examples/1CA SUR_4800_2000.png']).launch(share=False)
 
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 = model.cpu()
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  def transform_image(image):
15
  my_transforms = transforms.Compose([transforms.ToTensor(),
 
19
  image_aux = image
20
  return my_transforms(image_aux).unsqueeze(0).to(device)
21
 
 
 
 
 
 
 
 
 
 
22
 
23
  # Definimos una funci贸n que se encarga de llevar a cabo las predicciones
24
  def predict(img):
25
+ img_pil = PIL.Image.fromarray(img, 'RGB')
26
+ image = transforms.Resize((400,400))(img_pil)
27
  tensor = transform_image(image=image)
28
  model.to(device)
29
  with torch.no_grad():
 
32
  mask = np.array(outputs.cpu())
33
  mask[mask==1]=255
34
  mask=np.reshape(mask,(400,400))
35
+ return Image.fromarray(mask.astype('uint8')
36
 
37
  # Creamos la interfaz y la lanzamos.
38
  gr.Interface(fn=predict, inputs=gr.inputs.Image(shape=(400, 400)), outputs="image", examples=['examples/1CA SUR_1200_800.png', 'examples/1CA SUR_4000_1200.png', 'examples/1CA SUR_4800_2000.png']).launch(share=False)