Update app.py
Browse files
app.py
CHANGED
|
@@ -9,23 +9,26 @@ model = tf.keras.models.load_model("pneumonia_cnn_model.h5")
|
|
| 9 |
# Prediction function
|
| 10 |
def predict(image):
|
| 11 |
try:
|
| 12 |
-
#
|
| 13 |
-
img =
|
| 14 |
-
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
# Make prediction
|
| 22 |
-
|
| 23 |
-
|
| 24 |
# Interpret prediction
|
| 25 |
-
if
|
| 26 |
-
return "Pneumonia detected"
|
| 27 |
else:
|
| 28 |
-
return "No pneumonia detected"
|
|
|
|
| 29 |
except Exception as e:
|
| 30 |
return f"Error during prediction: {str(e)}"
|
| 31 |
|
|
|
|
| 9 |
# Prediction function
|
| 10 |
def predict(image):
|
| 11 |
try:
|
| 12 |
+
# Resize image to match the model's expected input size
|
| 13 |
+
img = image.resize((299, 299))
|
| 14 |
+
|
| 15 |
+
# Handle grayscale vs RGB based on model input shape
|
| 16 |
+
if model.input_shape[-1] == 1: # Model trained on grayscale
|
| 17 |
+
img = ImageOps.grayscale(img)
|
| 18 |
+
img_array = np.array(img).reshape(1, 299, 299, 1) / 255.0
|
| 19 |
+
else: # Model trained on RGB
|
| 20 |
+
img_array = np.array(img) / 255.0
|
| 21 |
+
img_array = img_array.reshape(1, 299, 299, 3)
|
| 22 |
+
|
| 23 |
# Make prediction
|
| 24 |
+
pred_prob = model.predict(img_array)[0][0] # Extract float value
|
| 25 |
+
|
| 26 |
# Interpret prediction
|
| 27 |
+
if pred_prob >= 0.5:
|
| 28 |
+
return f"Pneumonia detected (confidence: {pred_prob:.2f})"
|
| 29 |
else:
|
| 30 |
+
return f"No pneumonia detected (confidence: {pred_prob:.2f})"
|
| 31 |
+
|
| 32 |
except Exception as e:
|
| 33 |
return f"Error during prediction: {str(e)}"
|
| 34 |
|