|
import gradio as gr |
|
|
|
from timeit import default_timer as timer |
|
from typing import Tuple , Dict |
|
import tensorflow as tf |
|
import numpy as np |
|
import cv2 |
|
from tensorflow.keras.models import load_model |
|
import matplotlib.pyplot as plt |
|
import matplotlib.patches as mpatches |
|
|
|
from PIL import Image |
|
import os |
|
|
|
|
|
IMG_H = 320 |
|
IMG_W = 416 |
|
NUM_CLASSES = 8 |
|
input_shape = (IMG_H,IMG_W,3) |
|
|
|
|
|
|
|
|
|
|
|
|
|
model = "AROI_image_segmentation.keras" |
|
|
|
|
|
model = load_model(model) |
|
print(f"The model loaded successfully") |
|
model.compile( |
|
loss = 'categorical_crossentropy', |
|
optimizer = tf.keras.optimizers.Adam(1e-4), |
|
) |
|
|
|
|
|
|
|
|
|
|
|
def load_and_prep_imgg(image_path , input_shape=[IMG_H,IMG_W], scale=True): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
image = cv2.imread(image_path) |
|
if image is None: |
|
print(f"Error: Cannot load image from {image_path}") |
|
return |
|
image = cv2.resize(image, (input_shape[1], input_shape[0])) |
|
image = image / 255.0 |
|
image = image.astype(np.float32) |
|
|
|
def predict(img) -> Tuple[Dict,float,float] : |
|
|
|
start_time = timer() |
|
|
|
image = load_and_prep_imgg(img) |
|
pred_mask = model.predict(np.expand_dims(image, axis=0)) |
|
pred_mask = np.argmax(pred_mask, axis=-1)[0] |
|
|
|
|
|
plt.figure(figsize=(10, 5)) |
|
|
|
|
|
plt.subplot(1, 2, 1) |
|
plt.imshow(image) |
|
plt.title("Original Image") |
|
plt.axis("off") |
|
|
|
|
|
plt.subplot(1, 2, 2) |
|
plt.imshow(pred_mask, cmap='jet') |
|
plt.title("Predicted Mask") |
|
plt.axis("off") |
|
|
|
|
|
legend_patches = get_legend_patches(colormap, class_names) |
|
plt.legend(handles=legend_patches, bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0.) |
|
|
|
|
|
plt.tight_layout() |
|
plt.show() |
|
|
|
|
|
|
|
pred_probbb = pred_img.max() * 100 |
|
|
|
|
|
end_time = timer() |
|
pred_time = round(end_time - start_time , 4) |
|
|
|
return pred_class , pred_probbb , pred_time |
|
|
|
|
|
|
|
title = 'Macular Disease Classification' |
|
description = 'Feature Extraction VGG model to classify Macular Diseases by OCT' |
|
article = 'Created with TensorFlow Model Deployment' |
|
|
|
|
|
example_list = [['examples/'+ example] for example in os.listdir('examples')] |
|
example_list |
|
|
|
|
|
demo = gr.Interface(fn=predict , |
|
inputs=gr.Image(type='pil'), |
|
outputs=[gr.Label(num_top_classes = 3 , label= 'prediction'), |
|
gr.Number(label= 'Prediction Probabilities'), |
|
gr.Number(label= 'Prediction time (s)')], |
|
examples = example_list, |
|
title = title, |
|
description = description, |
|
article= article) |
|
|
|
|
|
demo.launch(debug= False) |