|
import gradio as gr |
|
import tensorflow as tf |
|
import numpy as np |
|
from gradio import components |
|
|
|
|
|
model = tf.keras.models.load_model('stomach.h5') |
|
|
|
|
|
class_names = { |
|
0: 'Esophagitis', |
|
1: 'Dyed lifted polyps' |
|
} |
|
|
|
|
|
def classify_image(image): |
|
|
|
img_array = tf.image.resize(image, [256, 256]) |
|
img_array = tf.expand_dims(img_array, 0) / 255.0 |
|
|
|
|
|
prediction = model.predict(img_array) |
|
predicted_class = tf.argmax(prediction[0], axis=-1) |
|
confidence = np.max(prediction[0]) |
|
|
|
if confidence < 0.6: |
|
result = "Unable to detect" |
|
details = "The model was unable to confidently classify the image." |
|
else: |
|
class_name = class_names[predicted_class.numpy()] |
|
result = class_name |
|
details = f"The image is classified as {class_name} with a confidence of {confidence:.2f}." |
|
|
|
return result, confidence, details |
|
|
|
|
|
iface = gr.Interface( |
|
fn=classify_image, |
|
inputs=components.Image(shape=(256, 256)), |
|
outputs=[ |
|
components.Textbox(label="Result"), |
|
components.Number(label="Confidence"), |
|
components.Textbox(label="Details") |
|
], |
|
examples=[ |
|
['examples/0.jpg'], |
|
['examples/1.jpg'], |
|
['examples/2.jpg'], |
|
['examples/3.jpg'] |
|
] |
|
) |
|
|
|
iface.launch() |
|
|