tmafantiri commited on
Commit
eac8db7
·
verified ·
1 Parent(s): 2e437bc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -22
app.py CHANGED
@@ -1,23 +1,58 @@
 
 
 
 
 
 
 
 
 
1
  def predict_image(image):
2
- try:
3
- # Resize the image
4
- img = image.resize((128, 128))
5
- # Convert the image to a NumPy array
6
- img = np.array(img)
7
- # Check if the image has 3 color channels
8
- if img.shape == (128, 128): # If grayscale, convert to RGB
9
- img = np.stack((img,) * 3, axis=-1)
10
- # Add a batch dimension
11
- img = np.expand_dims(img, axis=0) / 255.0 # Normalize the image
12
- # Make the prediction
13
- prediction = model.predict(img)
14
- # Get the predicted class and confidence level
15
- predicted_class = np.argmax(prediction)
16
- confidence = np.max(prediction) * 100 # Convert to percentage
17
- # Return the results
18
- if predicted_class == 0:
19
- return f'No tumor detected. Confidence: {confidence:.2f}%'
20
- else:
21
- return f'Tumor detected. Confidence: {confidence:.2f}%'
22
- except Exception as e:
23
- return f'Error processing image: {str(e)}'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 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
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
25
+ prediction = model.predict(img)
26
+
27
+ # Get the predicted class and confidence level
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: #f0f4f7;
41
+ }
42
+ """
43
+
44
+
45
+ # Create the Gradio interface
46
+ iface = gr.Interface(
47
+ fn=predict_image,
48
+ inputs=gr.Image(type="pil"),
49
+ outputs=gr.Textbox(),
50
+ title="Brain Tumor Detection AI App",
51
+ description="Upload an image to detect brain tumors.",
52
+ css=css, # Apply the custom background color
53
+ theme="dark", # Apply a dark theme to the interface
54
+ flagging_options=["Incorrect Diagnosis", "Image Not Clear", "Model Error"], # Add flagging options
55
+ )
56
+
57
+ # Launch the interface
58
+ iface.launch()