Update app.py
Browse files
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
|
7 |
model = load_model('brain_tumor_model.h5')
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
-
|
12 |
-
img = image.resize((128, 128))
|
13 |
-
|
14 |
-
# Convert the image to a NumPy array
|
15 |
-
img = np.array(img)
|
16 |
|
17 |
-
|
18 |
-
if img.shape == (128, 128): # If grayscale, convert to RGB
|
19 |
-
img = np.stack((img,) * 3, axis=-1)
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
|
27 |
-
|
28 |
-
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
return f'Tumor detected. Confidence: {confidence:.2f}'
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
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",
|
56 |
-
flagging_options=["Incorrect Diagnosis", "Image Not Clear", "Model Error"],
|
57 |
-
examples=examples
|
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
|