rttr1 commited on
Commit
3301b3c
·
verified ·
1 Parent(s): 29be643

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -18
app.py CHANGED
@@ -1,31 +1,33 @@
1
  import gradio as gr
2
  import numpy as np
3
  import tensorflow as tf
4
- from tensorflow.keras.preprocessing import image
5
- import os
6
 
7
  # Load the trained model
8
- model = tf.keras.models.load_model("BRAIINTUMORMODEL.h5") # Ensure model path is correct
9
 
10
  # Define class labels
11
- class_labels = ["glioma_tumor", "meningioma_tumor", "no_tumor", "pituitary_tumor"]
12
 
13
- # Function to make predictions
14
  def predict_brain_tumor(img):
15
- img = img.resize((224, 224)) # Resize to match model input
16
- img_array = np.array(img) / 255.0 # Normalize
17
- img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
 
 
 
 
 
18
 
 
19
  prediction = model.predict(img_array)[0]
 
 
20
 
21
- # Create the formatted output string
22
- output_str = ""
23
- for label, confidence in zip(class_labels, prediction):
24
- output_str += f"{label}\n{'‾'*50}\n{confidence*100:.0f}%\n\n\n"
25
-
26
- return output_str
27
 
28
- # Example images (replace these paths with your actual example image paths)
29
  example_images = [
30
  ["example_glioma.jpg"],
31
  ["example_meningioma.jpeg"],
@@ -37,11 +39,10 @@ example_images = [
37
  iface = gr.Interface(
38
  fn=predict_brain_tumor,
39
  inputs=gr.Image(type="pil"),
40
- outputs=gr.Textbox(label="Tumor Prediction Probabilities"),
41
  title="🧠 Brain Tumor Classification",
42
- description="Upload an MRI image to classify whether it has a tumor. You can also click on the example images below.",
43
  examples=example_images
44
  )
45
 
46
- # Launch Gradio App
47
  iface.launch()
 
1
  import gradio as gr
2
  import numpy as np
3
  import tensorflow as tf
4
+ from PIL import Image
 
5
 
6
  # Load the trained model
7
+ model = tf.keras.models.load_model("BRAIINTUMORMODEL.h5")
8
 
9
  # Define class labels
10
+ class_labels = ["Glioma", "Meningioma", "No Tumor", "Pituitary"]
11
 
 
12
  def predict_brain_tumor(img):
13
+ # Convert to RGB if needed
14
+ if img.mode != 'RGB':
15
+ img = img.convert('RGB')
16
+
17
+ # Preprocess image
18
+ img = img.resize((224, 224))
19
+ img_array = np.array(img) / 255.0
20
+ img_array = np.expand_dims(img_array, axis=0)
21
 
22
+ # Make prediction
23
  prediction = model.predict(img_array)[0]
24
+ predicted_class = np.argmax(prediction)
25
+ confidence = np.max(prediction) * 100
26
 
27
+ # Format output
28
+ return f"Predicted Tumor Type: {class_labels[predicted_class]} (Confidence: {confidence:.2f}%)"
 
 
 
 
29
 
30
+ # Example images
31
  example_images = [
32
  ["example_glioma.jpg"],
33
  ["example_meningioma.jpeg"],
 
39
  iface = gr.Interface(
40
  fn=predict_brain_tumor,
41
  inputs=gr.Image(type="pil"),
42
+ outputs=gr.Textbox(label="Prediction Result"),
43
  title="🧠 Brain Tumor Classification",
44
+ description="Upload an MRI image to classify the tumor type. You can also click on the example images below.",
45
  examples=example_images
46
  )
47
 
 
48
  iface.launch()