Elegbede commited on
Commit
78d872d
1 Parent(s): dc73b93

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -10
app.py CHANGED
@@ -1,28 +1,46 @@
1
  import gradio as gr
2
  import tensorflow as tf
3
- from tensorflow.keras.models import load_model
4
  import numpy as np
5
- from tensorflow.keras.preprocessing import image
6
 
 
 
 
 
 
 
 
 
 
7
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  def predict_input_image(img):
10
- img = tf.image.resize(img, [224,224])
11
- img = np.expand_dims(img, axis = 0)
12
-
13
- # Make predictions
14
  model = tf.keras.models.load_model('Tumor_Model.h5')
 
 
15
  prediction = model.predict(img)
16
- result = 'No Tumor Detected' if prediction[0][0] > 0.6 else 'Tumor detected'
17
 
18
  return f"Prediction: {result}"
19
 
20
-
21
-
22
  # Define Gradio interface
23
  iface = gr.Interface(
24
  fn=predict_input_image,
25
- inputs= 'image',
26
  outputs="text",
27
  )
28
 
 
1
  import gradio as gr
2
  import tensorflow as tf
3
+ import cv2
4
  import numpy as np
 
5
 
6
+ def preprocess_image(img):
7
+ # Resize the image to the target size (256x256)
8
+ img = cv2.resize(img, (256, 256))
9
+
10
+ # Center crop to 224x224
11
+ h, w, _ = img.shape
12
+ crop_start_x = (w - 224) // 2
13
+ crop_start_y = (h - 224) // 2
14
+ img = img[crop_start_y:crop_start_y + 224, crop_start_x:crop_start_x + 224]
15
 
16
+ # Normalize the image
17
+ img = img / 255.0
18
+
19
+ # Convert BGR to RGB
20
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
21
+
22
+ # Expand dimensions to match model input shape
23
+ img = np.expand_dims(img, axis=0)
24
+
25
+ return img
26
 
27
  def predict_input_image(img):
28
+ # Preprocess the input image
29
+ img = preprocess_image(img)
30
+
31
+ # Load the pre-trained model
32
  model = tf.keras.models.load_model('Tumor_Model.h5')
33
+
34
+ # Make predictions
35
  prediction = model.predict(img)
36
+ result = 'No Tumor Detected' if prediction[0][0] > 0.5 else 'Tumor detected'
37
 
38
  return f"Prediction: {result}"
39
 
 
 
40
  # Define Gradio interface
41
  iface = gr.Interface(
42
  fn=predict_input_image,
43
+ inputs=gr.Image(type="numpy", preprocess=preprocess_image),
44
  outputs="text",
45
  )
46