tmafantiri commited on
Commit
26360ef
·
verified ·
1 Parent(s): 86b8043

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -36
app.py CHANGED
@@ -2,48 +2,40 @@ import gradio as gr
2
  import numpy as np
3
  from PIL import Image
4
  from keras.models import load_model
 
5
 
6
- # Load your pre-trained model (make sure the model file is in the same directory)
7
  model = load_model('brain_tumor_model.h5')
8
 
9
- # Function to process image and make predictions
10
- def predict_image(image):
11
- # Resize the image to match model input
12
- img = image.resize((128, 128))
13
-
14
- # Convert the image to a NumPy array
15
- img = np.array(img)
16
 
17
- # Ensure the image has 3 color channels (RGB)
18
- if img.shape == (128, 128): # If grayscale, convert to RGB
19
- img = np.stack((img,) * 3, axis=-1)
20
 
21
- # Normalize the image (if your model requires normalization)
22
- img = img / 255.0
23
-
24
- # Add a batch dimension (for model input)
25
- img = np.expand_dims(img, axis=0)
26
 
27
- # Make the prediction
28
- prediction = model.predict(img)
 
29
 
30
- # Get the predicted class and confidence level
31
- predicted_class = np.argmax(prediction)
32
- confidence = np.max(prediction)
33
 
34
- # Return the result with confidence level
35
- if predicted_class == 0:
36
- return f'No tumor detected. Confidence: {confidence:.2f}'
37
- else:
38
- return f'Tumor detected. Confidence: {confidence:.2f}'
39
 
40
- # Provide example images (these should be paths to valid images)
41
- examples = [
42
- ["https://huggingface.co/spaces/tmafantiri/braintumourdetector/blob/main/images/no/1%20no.jpeg"],
43
- ["https://huggingface.co/spaces/tmafantiri/braintumourdetector/blob/main/images/yes/Y1.jpg"],
44
- ["https://huggingface.co/spaces/tmafantiri/braintumourdetector/blob/main/images/no/15%20no.jpg"],
45
- ["https://huggingface.co/spaces/tmafantiri/braintumourdetector/blob/main/images/yes/Y104.jpg"]
46
- ]
47
 
48
  # Create the Gradio interface
49
  iface = gr.Interface(
@@ -52,9 +44,9 @@ iface = gr.Interface(
52
  outputs=gr.Textbox(),
53
  title="Brain Tumor Detection AI App",
54
  description="Upload an image to detect brain tumors.",
55
- theme="monochrome", # Apply a dark theme to the interface
56
- flagging_options=["Incorrect Diagnosis", "Image Not Clear", "Model Error"], # Add flagging options
57
- examples=examples # Include example images
58
  )
59
 
60
  # Launch the interface
 
2
  import numpy as np
3
  from PIL import Image
4
  from keras.models import load_model
5
+ import json
6
 
7
+ # Load your pre-trained model
8
  model = load_model('brain_tumor_model.h5')
9
 
10
+ # Load examples from JSON file
11
+ with open('examples.json', 'r') as f:
12
+ examples_data = json.load(f)
 
 
 
 
13
 
14
+ examples = [[example['image']] for example in examples_data]
 
 
15
 
16
+ def predict_image(image):
17
+ try:
18
+ # Resize and preprocess the image
19
+ img = image.resize((128, 128))
20
+ img = np.array(img)
21
 
22
+ # Check and convert grayscale to RGB
23
+ if img.shape == (128, 128): # Grayscale
24
+ img = np.stack((img,) * 3, axis=-1)
25
 
26
+ # Normalize the image
27
+ img = img / 255.0
28
+ img = np.expand_dims(img, axis=0)
29
 
30
+ # Make the prediction
31
+ prediction = model.predict(img)
32
+ predicted_class = np.argmax(prediction)
33
+ confidence = np.max(prediction)
 
34
 
35
+ return f'{"No tumor detected" if predicted_class == 0 else "Tumor detected"}. Confidence: {confidence:.2f}'
36
+
37
+ except Exception as e:
38
+ return f"Error: {str(e)}"
 
 
 
39
 
40
  # Create the Gradio interface
41
  iface = gr.Interface(
 
44
  outputs=gr.Textbox(),
45
  title="Brain Tumor Detection AI App",
46
  description="Upload an image to detect brain tumors.",
47
+ theme="monochrome",
48
+ flagging_options=["Incorrect Diagnosis", "Image Not Clear", "Model Error"],
49
+ examples=examples
50
  )
51
 
52
  # Launch the interface