Elegbede commited on
Commit
c662a40
1 Parent(s): db8bbbc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -28
app.py CHANGED
@@ -1,48 +1,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
  img = tf.image.resize(img, [224,224])
31
  img = np.expand_dims(img, axis = 0)
32
-
33
- # Load the pre-trained model
34
- model = tf.keras.models.load_model('Tumor_Model.h5')
35
-
36
  # Make predictions
 
37
  prediction = model.predict(img)
38
- result = 'No Tumor Detected' if prediction[0][0] > 0.5 else 'Tumor detected'
39
 
40
  return f"Prediction: {result}"
41
 
 
 
42
  # Define Gradio interface
43
  iface = gr.Interface(
44
  fn=predict_input_image,
45
- inputs=gr.Image(type="numpy"),
46
  outputs="text",
47
  )
48
 
 
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.4 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