File size: 911 Bytes
da79423
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

import tensorflow as tf
from preprocessing import preprocess_image
import matplotlib.pyplot as plt

# Example function to run prediction
def predict_xray(image_path):
    # Load model
    model = tf.keras.models.load_model("model.keras")
    
    # Preprocess image
    img = preprocess_image(image_path)
    
    # Get raw image for display
    display_img = plt.imread(image_path)
    
    # Run prediction
    prediction = model.predict(img)
    prob = prediction[0][0]
    
    # Determine class
    if prob > 0.5:
        result = "PNEUMONIA"
        confidence = prob
    else:
        result = "NORMAL"
        confidence = 1 - prob
    
    # Display results
    plt.figure(figsize=(6, 6))
    plt.imshow(display_img, cmap="gray")
    plt.title(f"Prediction: {result}\nConfidence: {confidence:.2%}")
    plt.axis("off")
    plt.show()
    
    return {"class": result, "confidence": float(confidence)}