Vinit710 commited on
Commit
d5400ea
·
verified ·
1 Parent(s): 7698d13

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -14
app.py CHANGED
@@ -2,12 +2,12 @@ import gradio as gr
2
  import numpy as np
3
  import cv2
4
  from tensorflow.keras.models import load_model
 
5
 
6
  # Custom object scope to handle InputLayer configuration
7
- from tensorflow.keras.layers import InputLayer
8
  custom_objects = {'InputLayer': InputLayer}
9
 
10
- # Load the model
11
  def load_ocular_model():
12
  return load_model('odir_cnn_model.h5', custom_objects=custom_objects)
13
 
@@ -19,13 +19,13 @@ LABELS = ['Normal (N)', 'Diabetes (D)', 'Glaucoma (G)', 'Cataract (C)',
19
  'Pathological Myopia (M)', 'Other diseases/abnormalities (O)']
20
 
21
  # Preprocess the image
22
- def preprocess_image(image, img_size=128):
23
  img = cv2.resize(image, (img_size, img_size))
24
  img = img / 255.0 # Normalize
25
  img = np.expand_dims(img, axis=0) # Add batch dimension
26
  return img
27
 
28
- # Prediction function
29
  def predict_diseases(left_image, right_image):
30
  left_img = preprocess_image(left_image)
31
  right_img = preprocess_image(right_image)
@@ -39,14 +39,24 @@ def predict_diseases(left_image, right_image):
39
 
40
  return pred_labels
41
 
 
 
 
 
 
 
 
 
42
  # Gradio interface
43
- iface = gr.Interface(
44
- fn=predict_diseases,
45
- inputs=[gr.inputs.Image(type="numpy"), gr.inputs.Image(type="numpy")],
46
- outputs="json",
47
- title="Ocular Disease Prediction",
48
- description="Upload left and right eye images to predict ocular diseases."
49
- )
50
-
51
- # Launch the interface
52
- iface.launch()
 
 
 
2
  import numpy as np
3
  import cv2
4
  from tensorflow.keras.models import load_model
5
+ from tensorflow.keras.layers import InputLayer
6
 
7
  # Custom object scope to handle InputLayer configuration
 
8
  custom_objects = {'InputLayer': InputLayer}
9
 
10
+ # Load model
11
  def load_ocular_model():
12
  return load_model('odir_cnn_model.h5', custom_objects=custom_objects)
13
 
 
19
  'Pathological Myopia (M)', 'Other diseases/abnormalities (O)']
20
 
21
  # Preprocess the image
22
+ def preprocess_image(image, img_size=128): # Change img_size to 128
23
  img = cv2.resize(image, (img_size, img_size))
24
  img = img / 255.0 # Normalize
25
  img = np.expand_dims(img, axis=0) # Add batch dimension
26
  return img
27
 
28
+ # Predict diseases based on left and right images
29
  def predict_diseases(left_image, right_image):
30
  left_img = preprocess_image(left_image)
31
  right_img = preprocess_image(right_image)
 
39
 
40
  return pred_labels
41
 
42
+ # Define the Gradio interface
43
+ def predict_from_images(left_image, right_image):
44
+ left_image = cv2.cvtColor(np.array(left_image), cv2.COLOR_RGB2BGR) # Convert Gradio's image to OpenCV format
45
+ right_image = cv2.cvtColor(np.array(right_image), cv2.COLOR_RGB2BGR) # Convert Gradio's image to OpenCV format
46
+ predictions = predict_diseases(left_image, right_image)
47
+
48
+ return predictions
49
+
50
  # Gradio interface
51
+ input_images = [gr.Image(type="pil", label="Left Eye Image"), gr.Image(type="pil", label="Right Eye Image")]
52
+ output_labels = gr.Label(num_top_classes=len(LABELS), label="Ocular Disease Predictions")
53
+
54
+ gr_interface = gr.Interface(fn=predict_from_images,
55
+ inputs=input_images,
56
+ outputs=output_labels,
57
+ title="Ocular Disease Prediction",
58
+ description="Upload left and right eye images to predict ocular diseases.",
59
+ live=False)
60
+
61
+ # Launch the Gradio app
62
+ gr_interface.launch()