File size: 1,284 Bytes
a379dfa 16a92ad a379dfa f8b5711 16a92ad 1d4913b 1d9d109 de2b260 16a92ad 7e12558 16a92ad 7e12558 16a92ad de2b260 a7548e4 a379dfa 16a92ad 1d9d109 9569297 16a92ad |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
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()
|