Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
|
| 6 |
+
IMAGE_SIZE = 128
|
| 7 |
+
NUM_CLASSES = 3
|
| 8 |
+
|
| 9 |
+
def preprocess_image(image, target_size, add_clahe=True, clip_limit=4, tile_grid_size=(40,40), all_clahe=True):
|
| 10 |
+
"""
|
| 11 |
+
Preprocess the images to remove black borders
|
| 12 |
+
and improve contrast using Y channel and CLAHE
|
| 13 |
+
"""
|
| 14 |
+
try:
|
| 15 |
+
# Crop the image to remove black borders
|
| 16 |
+
cropped_rgb_image = remove_black_borders(image)
|
| 17 |
+
|
| 18 |
+
if add_clahe:
|
| 19 |
+
equalised_image = apply_clahe(
|
| 20 |
+
cropped_rgb_image, clip_limit, tile_grid_size, all_clahe)
|
| 21 |
+
|
| 22 |
+
# Resize the image to target size
|
| 23 |
+
resized_image = cv2.resize(equalised_image, (target_size, target_size))
|
| 24 |
+
else:
|
| 25 |
+
# Resize the image to target size
|
| 26 |
+
resized_image = cv2.resize(cropped_rgb_image, (target_size, target_size))
|
| 27 |
+
|
| 28 |
+
return resized_image
|
| 29 |
+
|
| 30 |
+
except Exception as e:
|
| 31 |
+
print("Error processing image: ")
|
| 32 |
+
print(e)
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def remove_black_borders(image):
|
| 36 |
+
"""
|
| 37 |
+
Crop the image to remove black borders
|
| 38 |
+
"""
|
| 39 |
+
green_channel_image = image[:, :, 1]
|
| 40 |
+
|
| 41 |
+
# Find the contours in the green channel
|
| 42 |
+
contours, _ = cv2.findContours(
|
| 43 |
+
green_channel_image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
| 44 |
+
|
| 45 |
+
# Find the largest contour
|
| 46 |
+
largest_contour = max(contours, key=cv2.contourArea)
|
| 47 |
+
|
| 48 |
+
# Get the bounding rectangle of the largest contour
|
| 49 |
+
x, y, w, h = cv2.boundingRect(largest_contour)
|
| 50 |
+
|
| 51 |
+
# Create a mask with the same size as the bounding rectangle
|
| 52 |
+
mask = np.zeros((h, w), np.uint8)
|
| 53 |
+
|
| 54 |
+
# Draw the largest contour on the mask
|
| 55 |
+
cv2.drawContours(mask, [largest_contour - [x, y]], 0, 255, -1)
|
| 56 |
+
|
| 57 |
+
# Convert the mask to a 3 channel image
|
| 58 |
+
mask_3_channel = cv2.merge([mask, mask, mask])
|
| 59 |
+
|
| 60 |
+
# Crop the image using the mask
|
| 61 |
+
cropped_image = cv2.bitwise_and(image[y:y+h, x:x+w], mask_3_channel)
|
| 62 |
+
|
| 63 |
+
return cropped_image
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def apply_clahe(image, clip_limit=4, tile_grid_size=(40,40), all_clahe=False):
|
| 67 |
+
"""
|
| 68 |
+
Preprocess the image
|
| 69 |
+
"""
|
| 70 |
+
try:
|
| 71 |
+
# Extract the y channel from the cropped rgb image
|
| 72 |
+
yuv_image = cv2.cvtColor(image, cv2.COLOR_RGB2YUV)
|
| 73 |
+
cropped_y_channel_image = yuv_image[:, :, 0]
|
| 74 |
+
|
| 75 |
+
# Apply CLAHE to the y channel of the cropped image
|
| 76 |
+
clahe = cv2.createCLAHE(clipLimit=clip_limit, tileGridSize=tile_grid_size)
|
| 77 |
+
enhanced_y_channel = clahe.apply(cropped_y_channel_image)
|
| 78 |
+
|
| 79 |
+
if all_clahe:
|
| 80 |
+
cropped_u_channel = yuv_image[:, :, 1]
|
| 81 |
+
enhanced_u_channel = clahe.apply(cropped_u_channel)
|
| 82 |
+
cropped_v_channel = yuv_image[:, :, 2]
|
| 83 |
+
enhanced_v_channel = clahe.apply(cropped_v_channel)
|
| 84 |
+
|
| 85 |
+
enhanced_yuv_image = np.stack([enhanced_y_channel, enhanced_u_channel, enhanced_v_channel], axis=-1)
|
| 86 |
+
|
| 87 |
+
# Convert YUV to RGB
|
| 88 |
+
enhanced_rgb_image = cv2.cvtColor(enhanced_yuv_image, cv2.COLOR_YUV2RGB)
|
| 89 |
+
|
| 90 |
+
return enhanced_rgb_image
|
| 91 |
+
else:
|
| 92 |
+
# Convert the y channel image to grayscale
|
| 93 |
+
enhanced_grayscale_image = cv2.convertScaleAbs(enhanced_y_channel, alpha=(255/219))
|
| 94 |
+
|
| 95 |
+
# Repeat the equalised grayscale for all 3-channels
|
| 96 |
+
enhanced_grayscale_3_channels = np.stack((enhanced_grayscale_image,) * 3, axis=-1)
|
| 97 |
+
|
| 98 |
+
return enhanced_grayscale_3_channels
|
| 99 |
+
|
| 100 |
+
except Exception as e:
|
| 101 |
+
print("Error processing image: ", e)
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
def load_vgg16_model():
|
| 105 |
+
vgg16_bs32_img128_CLAHE4 = tf.keras.models.load_model('./VGG16_32_128_CLAHE_4_40_trainableFalse.h5')
|
| 106 |
+
return vgg16_bs32_img128_CLAHE4
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
def predict_input_image(img):
|
| 110 |
+
# img = preprocess_image(img, IMAGE_SIZE, add_clahe=True, clip_limit=4, tile_grid_size=(40,40), all_clahe=True)
|
| 111 |
+
# img_4d=img.reshape(-1, IMAGE_SIZE, IMAGE_SIZE, 3)
|
| 112 |
+
# img_4d_ensemble = [img_4d for _ in range(len(ensemble_model.input))]
|
| 113 |
+
# prediction=ensemble_model.predict([img_4d_ensemble])[0]
|
| 114 |
+
|
| 115 |
+
img = preprocess_image(img, IMAGE_SIZE, add_clahe=True, clip_limit=4, tile_grid_size=(40,40), all_clahe=False)
|
| 116 |
+
img_4d=img.reshape(-1, IMAGE_SIZE, IMAGE_SIZE, 3)
|
| 117 |
+
model = load_vgg16_model()
|
| 118 |
+
prediction=model.predict([img_4d])[0]
|
| 119 |
+
|
| 120 |
+
dr_classes = ['No DR', 'Mild DR', 'Referable DR']
|
| 121 |
+
return {dr_classes[i]: float(prediction[i]) for i in range(3)}
|
| 122 |
+
|
| 123 |
+
image = gr.inputs.Image(shape=(IMAGE_SIZE, IMAGE_SIZE))
|
| 124 |
+
label = gr.outputs.Label(num_top_classes=NUM_CLASSES)
|
| 125 |
+
iface = gr.Interface(fn=predict_input_image, inputs=image, outputs=label).launch(share=True)
|
| 126 |
+
iface.launch()
|