marcossalinas commited on
Commit
e59318c
·
1 Parent(s): 0ffc0f7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -15
app.py CHANGED
@@ -7,26 +7,38 @@ import gradio as gr
7
  SIDE_IMG = 320
8
  BLACK_TOLERANCE = 20
9
 
10
- LIMIT_FOR_DISEASE = 0.8
11
-
12
- model = tensorflow.keras.models.load_model('model_1651184375_mejor.h5')
13
-
14
- def remove_borders_color(image):
15
- image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
16
- image_gray[image_gray < BLACK_TOLERANCE] = 0
17
- y_nonzero, x_nonzero = np.nonzero(image_gray)
18
- return image[np.min(y_nonzero):np.max(y_nonzero), np.min(x_nonzero): np.max(x_nonzero), ]
19
-
20
- def resize_image_square(image, side):
21
- return cv2.resize(image, (side, side), interpolation = cv2.INTER_LINEAR)
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
  def get_image(path):
24
  image = cv2.imread(path)
25
- return resize_image_square(remove_borders_color(image), SIDE_IMG)
26
-
27
 
28
  def predict(path):
29
- image = get_image(path) / 255
30
  result = model.predict(np.array([ image ]))[0]
31
  return 'Retina sana' if result < LIMIT_FOR_DISEASE else 'Retina enferma'
32
 
 
7
  SIDE_IMG = 320
8
  BLACK_TOLERANCE = 20
9
 
10
+ LIMIT_FOR_DISEASE = 0.5
11
+
12
+ model = tensorflow.keras.models.load_model('predictor_Disease_Risk.h5')
13
+
14
+ def cut_and_resize(image):
15
+ LOW_TOL = 20
16
+ img_bw = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
17
+ img_bw[img_bw<=LOW_TOL] = 0
18
+ y_nonzero, x_nonzero = np.nonzero(img_bw)
19
+ image = image[np.min(y_nonzero):np.max(y_nonzero), np.min(x_nonzero): np.max(x_nonzero), ] / 255.0
20
+ return cv2.resize(image, SHAPE[:2], interpolation = cv2.INTER_LINEAR)
21
+
22
+ def z_score(image):
23
+ img = np.copy(image) / 255.0
24
+ img_nan = np.copy(img)
25
+ img_nan[img_nan<0.05] = np.nan
26
+ _means = np.nanmean(img_nan, axis=(0,1))
27
+ _stds = np.nanstd(img_nan, axis=(0,1))
28
+ for i in range(3):
29
+ if _stds[i] > 0.0:
30
+ img[:,:,i] = (img[:,:,i] - _means[i]) / _stds[i]
31
+ img[:,:,i][img[:,:,i]<=img[0,0,i]] = -3.0
32
+ img[img>3.0] = 3.0
33
+ img[img<-3.0] = -3.0
34
+ return (img + 3.0) / 6.0
35
 
36
  def get_image(path):
37
  image = cv2.imread(path)
38
+ return z_score(cut_and_resize(image))
 
39
 
40
  def predict(path):
41
+ image = get_image(path)
42
  result = model.predict(np.array([ image ]))[0]
43
  return 'Retina sana' if result < LIMIT_FOR_DISEASE else 'Retina enferma'
44