kkhushisaid commited on
Commit
6f49626
·
verified ·
1 Parent(s): 7e12558

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -14
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
- # Convert to grayscale
13
- img = ImageOps.grayscale(image)
14
-
15
- # Resize image to match the model's expected input size (299x299)
16
- img = img.resize((299, 299)) # Resize to 299x299
17
-
18
- # Convert to numpy array and normalize the pixel values
19
- img_array = np.array(img).reshape(1, 299, 299, 1) / 255.0 # Reshape to (1, 299, 299, 1)
20
-
 
 
21
  # Make prediction
22
- prediction = model.predict(img_array)
23
-
24
  # Interpret prediction
25
- if prediction >= 0.5:
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