tmafantiri commited on
Commit
d9130c8
·
verified ·
1 Parent(s): 9521549

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -16
app.py CHANGED
@@ -8,17 +8,20 @@ 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
12
  img = image.resize((128, 128))
13
 
14
  # Convert the image to a NumPy array
15
  img = np.array(img)
16
 
17
- # Check if the image has 3 color channels
18
  if img.shape == (128, 128): # If grayscale, convert to RGB
19
  img = np.stack((img,) * 3, axis=-1)
20
 
21
- # Add a batch dimension
 
 
 
22
  img = np.expand_dims(img, axis=0)
23
 
24
  # Make the prediction
@@ -28,22 +31,15 @@ def predict_image(image):
28
  predicted_class = np.argmax(prediction)
29
  confidence = np.max(prediction)
30
 
31
- # Return the results
32
  if predicted_class == 0:
33
  return f'No tumor detected. Confidence: {confidence:.2f}'
34
  else:
35
  return f'Tumor detected. Confidence: {confidence:.2f}'
36
 
37
- # Create custom CSS for background color
38
- #css = """
39
- #body {
40
- # background-color: dark;
41
- #}
42
- #"""
43
-
44
  # Provide example images (these should be paths to valid images)
45
  examples = [
46
- ["https://huggingface.co/spaces/tmafantiri/braintumourdetector/blob/main/images/no/1%20no.jpeg"], # Replace with actual example image paths
47
  ["https://huggingface.co/spaces/tmafantiri/braintumourdetector/blob/main/images/yes/Y1.jpg"],
48
  ["https://huggingface.co/spaces/tmafantiri/braintumourdetector/blob/main/images/no/15%20no.jpg"],
49
  ["https://huggingface.co/spaces/tmafantiri/braintumourdetector/blob/main/images/yes/Y104.jpg"]
@@ -56,12 +52,10 @@ iface = gr.Interface(
56
  outputs=gr.Textbox(),
57
  title="Brain Tumor Detection AI App",
58
  description="Upload an image to detect brain tumors.",
59
- #css=css, # Apply the custom background color
60
  theme="monochrome", # Apply a dark theme to the interface
61
  flagging_options=["Incorrect Diagnosis", "Image Not Clear", "Model Error"], # Add flagging options
62
- examples=examples # Include 4 example images
63
  )
64
 
65
  # Launch the interface
66
- iface.launch(share=True,debug=True)
67
-
 
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
 
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"]
 
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
61
+ iface.launch(share=True, debug=True)