File size: 1,323 Bytes
5bede1f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | import gradio as gr
import tensorflow as tf
import numpy as np
from PIL import Image
# Initial parameters for pretrained model
IMG_SIZE = 300
labelCOVID = {
'COVID-19': 0,
'Normal': 1,
'Viral Pneumonia': 2,
'Lung_Opacity': 3
}
# Load the model from the H5 file
model = tf.keras.models.load_model('model/covid.h5')
# Define the prediction function
def predict(img):
img_height = 150
img_width = 150
# 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(labelCOVID.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=[["c1.png"],["c2.jpeg"]],)
demo.launch()
|