Spaces:
Sleeping
Sleeping
Commit
·
e59318c
1
Parent(s):
0ffc0f7
Update app.py
Browse files
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.
|
| 11 |
-
|
| 12 |
-
model = tensorflow.keras.models.load_model('
|
| 13 |
-
|
| 14 |
-
def
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
def get_image(path):
|
| 24 |
image = cv2.imread(path)
|
| 25 |
-
return
|
| 26 |
-
|
| 27 |
|
| 28 |
def predict(path):
|
| 29 |
-
image = get_image(path)
|
| 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 |
|