kkhushisaid's picture
Update app.py
7e12558 verified
import gradio as gr
import tensorflow as tf
from PIL import Image, ImageOps
import numpy as np
# Load the model
model = tf.keras.models.load_model("pneumonia_cnn_model.h5")
# Prediction function
def predict(image):
try:
# Convert to grayscale
img = ImageOps.grayscale(image)
# Resize image to match the model's expected input size (299x299)
img = img.resize((299, 299)) # Resize to 299x299
# Convert to numpy array and normalize the pixel values
img_array = np.array(img).reshape(1, 299, 299, 1) / 255.0 # Reshape to (1, 299, 299, 1)
# Make prediction
prediction = model.predict(img_array)
# Interpret prediction
if prediction >= 0.5:
return "Pneumonia detected"
else:
return "No pneumonia detected"
except Exception as e:
return f"Error during prediction: {str(e)}"
# Create the Gradio interface
iface = gr.Interface(
fn=predict, # Function to call on image input
inputs=gr.Image(type="pil", label="Upload Chest X-ray Image"),
outputs="text", # Output is the prediction result (text)
live=True # Optional: set to False if you don't want to update results live
)
# Launch the app
iface.launch()