File size: 1,156 Bytes
249f7d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import cv2
import numpy as np
import tensorflow as tf
from PIL import Image
import gradio as gr

# Load the trained model
model = tf.keras.models.load_model("pneumonia_model.h5")

# Define the prediction function
def predict_xray(image):
    # Convert PIL image to OpenCV format (numpy array)
    image = np.array(image)
    
    # Resize image to 150x150 (as per your training)
    image = cv2.resize(image, (150, 150))
    
    # Reshape and normalize
    image = image.reshape(1, 150, 150, 3) / 255.0  # Normalization (if used in training)
    
    # Make prediction
    prediction = model.predict(image)
    prediction = prediction.argmax()  # Get class with highest probability
    
    # Class labels (adjust based on your dataset)
    labels = ["Normal", "Pneumonia"]
    
    return labels[prediction]

# Create Gradio UI
iface = gr.Interface(
    fn=predict_xray,
    inputs=gr.Image(type="pil"),  # Accepts image input
    outputs="text",  # Returns class label
    title="Pneumonia Detection",
    description="Upload a chest X-ray image, and the model will predict if the patient has pneumonia or is normal."
)

# Launch the app
iface.launch()