DHEIVER commited on
Commit
e0ee69d
1 Parent(s): 7a80d19

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -14
app.py CHANGED
@@ -13,25 +13,31 @@ model = tf.keras.models.load_model("best_model_weights.h5") # Replace with the
13
 
14
  # Define the image classification function
15
  def classify_image(input_image):
16
- # Preprocess the input image
17
- input_image = Image.open(BytesIO(input_image))
18
- input_image = input_image.resize((img_width, img_height))
19
- input_image = np.array(input_image) / 255.0 # Normalize pixel values
 
20
 
21
- # Make a prediction using the model
22
- predictions = model.predict(np.expand_dims(input_image, axis=0))
23
 
24
- # Get the class label with the highest probability
25
- class_index = np.argmax(predictions)
26
- class_prob = predictions[0][class_index]
27
 
28
- # Define class labels (you can replace these with your actual class labels)
29
- class_labels = ["Normal", "Cataract"]
30
 
31
- # Get the class label
32
- class_label = class_labels[class_index]
 
 
 
 
 
 
33
 
34
- return f"Predicted Class: {class_label} (Probability: {class_prob:.2f})"
35
 
36
  # Define the Gradio interface
37
  iface = gr.Interface(
 
13
 
14
  # Define the image classification function
15
  def classify_image(input_image):
16
+ try:
17
+ # Preprocess the input image
18
+ input_image = Image.open(BytesIO(input_image))
19
+ input_image = input_image.resize((img_width, img_height))
20
+ input_image = np.array(input_image) / 255.0 # Normalize pixel values
21
 
22
+ # Make a prediction using the model
23
+ predictions = model.predict(np.expand_dims(input_image, axis=0))
24
 
25
+ # Get the class label with the highest probability
26
+ class_index = np.argmax(predictions)
27
+ class_prob = predictions[0][class_index]
28
 
29
+ # Define class labels (you can replace these with your actual class labels)
30
+ class_labels = ["Normal", "Cataract"]
31
 
32
+ # Get the class label
33
+ class_label = class_labels[class_index]
34
+
35
+ return f"Predicted Class: {class_label} (Probability: {class_prob:.2f})"
36
+ except Exception as e:
37
+ return "Error: Invalid image file"
38
+
39
+ # ...
40
 
 
41
 
42
  # Define the Gradio interface
43
  iface = gr.Interface(