| 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)} | |