|
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 = ImageOps.grayscale(image) |
|
|
|
|
|
img = img.resize((299, 299)) |
|
|
|
|
|
img_array = np.array(img).reshape(1, 299, 299, 1) / 255.0 |
|
|
|
|
|
prediction = model.predict(img_array) |
|
|
|
|
|
if prediction >= 0.5: |
|
return "Pneumonia detected" |
|
else: |
|
return "No pneumonia detected" |
|
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() |
|
|