| import gradio as gr | |
| import tensorflow as tf | |
| import numpy as np | |
| from PIL import Image | |
| # Initial parameters for pretrained model | |
| IMG_SIZE = 300 | |
| labelsBreast = {'Benign':0, | |
| 'Malignant':1, | |
| 'Normal':2} | |
| # Load the model from the H5 file | |
| model = tf.keras.models.load_model('model/BreastCancer.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(labelsBreast.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" ) | |
| demo.launch() | |