Commit ·
14b027c
1
Parent(s): c7ebb7c
Added app.py for Pneumonia Detection
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
from tensorflow.keras.preprocessing import image
|
| 5 |
+
|
| 6 |
+
# Load the trained model
|
| 7 |
+
model = tf.keras.models.load_model("pneumonia_model.keras")
|
| 8 |
+
|
| 9 |
+
# Define the class labels
|
| 10 |
+
class_labels = ["Normal", "Pneumonia"]
|
| 11 |
+
|
| 12 |
+
# Function to preprocess image and make a prediction
|
| 13 |
+
def predict_pneumonia(img):
|
| 14 |
+
img = img.resize((150, 150)) # Resize to match model input size
|
| 15 |
+
img = image.img_to_array(img)
|
| 16 |
+
img = np.expand_dims(img, axis=0) # Add batch dimension
|
| 17 |
+
img = img / 255.0 # Normalize pixel values
|
| 18 |
+
|
| 19 |
+
prediction = model.predict(img)[0]
|
| 20 |
+
result = class_labels[int(prediction > 0.5)] # Classify based on threshold
|
| 21 |
+
|
| 22 |
+
return f"Prediction: {result}"
|
| 23 |
+
|
| 24 |
+
# Create a Gradio interface
|
| 25 |
+
interface = gr.Interface(
|
| 26 |
+
fn=predict_pneumonia,
|
| 27 |
+
inputs=gr.Image(type="pil"),
|
| 28 |
+
outputs="text",
|
| 29 |
+
title="Pneumonia Detection from Chest X-ray",
|
| 30 |
+
description="Upload a chest X-ray image to check for pneumonia.",
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
# Launch the Gradio app
|
| 34 |
+
if __name__ == "__main__":
|
| 35 |
+
interface.launch()
|
| 36 |
+
|