|
import cv2 |
|
import numpy as np |
|
import tensorflow as tf |
|
from PIL import Image |
|
import gradio as gr |
|
|
|
|
|
model = tf.keras.models.load_model("pneumonia_detection.h5") |
|
|
|
|
|
def predict_xray(image): |
|
|
|
image = np.array(image) |
|
|
|
|
|
image = cv2.resize(image, (150, 150)) |
|
|
|
|
|
image = image.reshape(1, 150, 150, 3) / 255.0 |
|
|
|
|
|
prediction = model.predict(image)[0] |
|
|
|
|
|
labels = ["The Patient is Normal.", "The Patient has Pneumonia."] |
|
|
|
|
|
predicted_class = np.argmax(prediction) |
|
confidence = prediction[predicted_class] * 100 |
|
|
|
return f"{labels[predicted_class]} ({confidence:.2f}% confidence)" |
|
|
|
|
|
iface = gr.Interface( |
|
fn=predict_xray, |
|
inputs=gr.Image(type="pil"), |
|
outputs="text", |
|
title="Pneumonia Detection", |
|
description="Upload a chest X-ray image, and the model will predict if the patient has pneumonia or is normal, along with confidence scores." |
|
) |
|
|
|
|
|
iface.launch() |
|
|