| import gradio as gr | |
| import tensorflow as tf | |
| import numpy as np | |
| from PIL import Image | |
| # Initial parameters for pretrained model | |
| IMG_SIZE = 300 | |
| labelOcular = { | |
| 'normal': 0, | |
| 'diabetes': 1, | |
| 'glaucoma': 2, | |
| 'cataract': 3, | |
| 'amd': 4, | |
| 'hypertension': 5, | |
| 'myopia': 6, | |
| 'other': 7 | |
| } | |
| # Load the model from the H5 file | |
| model = tf.keras.models.load_model('model/Ocular.h5') | |
| # Define the prediction function | |
| def predict(img): | |
| img_height = 224 | |
| img_width = 224 | |
| # Convert the NumPy array to a PIL Image object | |
| pil_img = Image.fromarray(img) | |
| # Resize the image using the PIL Image object | |
| pil_img = pil_img.resize((img_height, img_width)) | |
| # Convert the PIL Image object to a NumPy array | |
| x = tf.keras.preprocessing.image.img_to_array(pil_img) | |
| x = x.reshape(1, img_height, img_width, 3) | |
| np.set_printoptions(formatter={'float': '{: 0.3f}'.format}) | |
| predi = model.predict(x) | |
| accuracy_of_class = '{:.1f}'.format(predi[0][np.argmax(predi)] * 100) + "%" | |
| classes = list(labelOcular.keys())[np.argmax(predi)] | |
| context = { | |
| 'predictedLabel': classes, | |
| # 'y_class': y_class, | |
| # 'z_class': z_class, | |
| 'accuracy_of_class': accuracy_of_class | |
| } | |
| return context | |
| demo = gr.Interface(fn=predict, inputs="image", outputs="text" , examples=[["O1.jpg"],["O2.jpg"],["O2.jpg"]],) | |
| demo.launch() | |