LuisCe commited on
Commit
38e58cc
verified
1 Parent(s): 264b326

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -26
app.py CHANGED
@@ -3,38 +3,54 @@ from fastai.vision.all import *
3
  from fastai.learner import load_learner
4
  from PIL import Image
5
 
6
- # Define la funci贸n get_y_fn si se utiliza en tu modelo
7
- def get_y_fn(x):
 
 
 
 
 
 
 
 
 
 
 
 
8
  return Path(str(x).replace("Images","Labels").replace("color","gt").replace(".jpg",".png"))
9
 
10
- # Define la clase TargetMaskConvertTransform
11
- class TargetMaskConvertTransform(ItemTransform):
12
- def __init__(self):
13
- pass
 
14
 
15
  def encodes(self, x):
16
- img, mask = x
17
- mask = np.array(mask)
18
- mask[mask == 255] = 1
19
- mask[mask == 150] = 2
20
- mask[mask == 74] = 3
21
- mask[mask == 76] = 3
22
- mask[mask == 29] = 4
23
- mask[mask == 25] = 4
24
- mask = PILMask.create(mask)
25
- return img, mask
26
 
27
- # Define la clase SegmentationAlbumentationsTransform
28
- class SegmentationAlbumentationsTransform(ItemTransform):
29
  def __init__(self):
30
  pass
31
-
32
  def encodes(self, x):
33
- img, mask = x
34
- # Aqu铆 deber铆as definir tu transformaci贸n de Albumentations
35
- # Por ejemplo, podr铆as tener algo como:
36
- # transformed = my_albumentations_function(image=img, mask=mask)
37
- # return transformed['image'], transformed['mask']
 
 
 
 
 
 
 
 
 
 
 
 
38
  return img, mask
39
 
40
  # Carga el modelo despu茅s de definir la clase
@@ -56,7 +72,11 @@ def transform_image(image):
56
  image_aux = image
57
  return my_transforms(image_aux).unsqueeze(0).to(device)
58
 
 
 
 
59
  def prediccion(img):
 
60
  image = transforms.Resize((480,640))(img)
61
  tensor = transform_image(image=image)
62
 
@@ -86,5 +106,4 @@ gr.Interface(prediccion,
86
  title="Grape Segmentation",
87
  description="Segment grapes in the image.",
88
  theme="compact",
89
- allow_flagging=False).launch()
90
-
 
3
  from fastai.learner import load_learner
4
  from PIL import Image
5
 
6
+ from albumentations import (
7
+ Compose,
8
+ OneOf,
9
+ ElasticTransform,
10
+ GridDistortion,
11
+ OpticalDistortion,
12
+ HorizontalFlip,
13
+ Rotate,
14
+ Transpose,
15
+ CLAHE,
16
+ ShiftScaleRotate
17
+ )
18
+
19
+ def get_y_fn (x):
20
  return Path(str(x).replace("Images","Labels").replace("color","gt").replace(".jpg",".png"))
21
 
22
+ class SegmentationAlbumentationsTransform(ItemTransform):
23
+ split_idx = 0
24
+
25
+ def __init__(self, aug):
26
+ self.aug = aug
27
 
28
  def encodes(self, x):
29
+ img,mask = x
30
+ aug = self.aug(image=np.array(img), mask=np.array(mask))
31
+ return PILImage.create(aug["image"]), PILMask.create(aug["mask"])
 
 
 
 
 
 
 
32
 
33
+ class TargetMaskConvertTransform(ItemTransform):
 
34
  def __init__(self):
35
  pass
 
36
  def encodes(self, x):
37
+ img,mask = x
38
+
39
+ #Convert to array
40
+ mask = np.array(mask)
41
+
42
+ # mask[mask!=255]=0
43
+ # Change 255 for 1
44
+ mask[mask==255]=1
45
+ mask[mask==150]=2
46
+ mask[mask==74]=3
47
+ mask[mask==76]=3
48
+ mask[mask==29]=4
49
+ mask[mask==25]=4
50
+ # mask[mask==255]=1
51
+
52
+ # Back to PILMask
53
+ mask = PILMask.create(mask)
54
  return img, mask
55
 
56
  # Carga el modelo despu茅s de definir la clase
 
72
  image_aux = image
73
  return my_transforms(image_aux).unsqueeze(0).to(device)
74
 
75
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
76
+
77
+
78
  def prediccion(img):
79
+ img = Image.fromarray(img)
80
  image = transforms.Resize((480,640))(img)
81
  tensor = transform_image(image=image)
82
 
 
106
  title="Grape Segmentation",
107
  description="Segment grapes in the image.",
108
  theme="compact",
109
+ allow_flagging=False).launch(debug=True)