Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
import tensorflow as tf
|
4 |
+
from PIL import Image
|
5 |
+
import gradio as gr
|
6 |
+
|
7 |
+
# Load the trained model
|
8 |
+
model = tf.keras.models.load_model("pneumonia_model.h5")
|
9 |
+
|
10 |
+
# Define the prediction function
|
11 |
+
def predict_xray(image):
|
12 |
+
# Convert PIL image to OpenCV format (numpy array)
|
13 |
+
image = np.array(image)
|
14 |
+
|
15 |
+
# Resize image to 150x150 (as per your training)
|
16 |
+
image = cv2.resize(image, (150, 150))
|
17 |
+
|
18 |
+
# Reshape and normalize
|
19 |
+
image = image.reshape(1, 150, 150, 3) / 255.0 # Normalization (if used in training)
|
20 |
+
|
21 |
+
# Make prediction
|
22 |
+
prediction = model.predict(image)
|
23 |
+
prediction = prediction.argmax() # Get class with highest probability
|
24 |
+
|
25 |
+
# Class labels (adjust based on your dataset)
|
26 |
+
labels = ["Normal", "Pneumonia"]
|
27 |
+
|
28 |
+
return labels[prediction]
|
29 |
+
|
30 |
+
# Create Gradio UI
|
31 |
+
iface = gr.Interface(
|
32 |
+
fn=predict_xray,
|
33 |
+
inputs=gr.Image(type="pil"), # Accepts image input
|
34 |
+
outputs="text", # Returns class label
|
35 |
+
title="Pneumonia Detection",
|
36 |
+
description="Upload a chest X-ray image, and the model will predict if the patient has pneumonia or is normal."
|
37 |
+
)
|
38 |
+
|
39 |
+
# Launch the app
|
40 |
+
iface.launch()
|