|
|
import gradio as gr |
|
|
import tensorflow as tf |
|
|
from PIL import Image, ImageOps |
|
|
import numpy as np |
|
|
|
|
|
|
|
|
model = tf.keras.models.load_model("pneumonia_cnn_model.h5") |
|
|
|
|
|
|
|
|
def predict(image): |
|
|
try: |
|
|
|
|
|
img = image.resize((299, 299)) |
|
|
|
|
|
|
|
|
if model.input_shape[-1] == 1: |
|
|
img = ImageOps.grayscale(img) |
|
|
img_array = np.array(img).reshape(1, 299, 299, 1) / 255.0 |
|
|
else: |
|
|
img_array = np.array(img) / 255.0 |
|
|
img_array = img_array.reshape(1, 299, 299, 3) |
|
|
|
|
|
|
|
|
pred_prob = model.predict(img_array)[0][0] |
|
|
|
|
|
|
|
|
if pred_prob >= 0.5: |
|
|
return f"Pneumonia detected (confidence: {pred_prob:.2f})" |
|
|
else: |
|
|
return f"No pneumonia detected (confidence: {pred_prob:.2f})" |
|
|
|
|
|
except Exception as e: |
|
|
return f"Error during prediction: {str(e)}" |
|
|
|
|
|
|
|
|
iface = gr.Interface( |
|
|
fn=predict, |
|
|
inputs=gr.Image(type="pil", label="Upload Chest X-ray Image"), |
|
|
outputs="text", |
|
|
live=True |
|
|
) |
|
|
|
|
|
|
|
|
iface.launch() |
|
|
|