Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,133 +1,79 @@
|
|
1 |
from PIL import Image, ImageOps
|
2 |
import numpy as np
|
3 |
-
from collections import OrderedDict
|
4 |
import seaborn as sns
|
5 |
import matplotlib.pyplot as plt
|
6 |
import pandas as pd
|
7 |
from keras.models import load_model
|
8 |
import gradio as gr
|
9 |
|
|
|
|
|
|
|
10 |
|
11 |
def create_plot(data):
|
12 |
-
|
13 |
|
14 |
-
|
15 |
|
16 |
-
|
17 |
-
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
ax.legend(ncol=2, loc="lower right", frameon=True)
|
23 |
-
sns.despine(left=True, bottom=True)
|
24 |
-
return f
|
25 |
|
|
|
|
|
|
|
26 |
|
27 |
def predict_tumor(img):
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
|
32 |
-
|
33 |
-
# image = Image.open(img).convert('RGB')
|
34 |
-
image = img
|
35 |
-
size = (224, 224)
|
36 |
-
image_PIL = Image.fromarray(image)
|
37 |
-
image = ImageOps.fit(image_PIL, size, Image.LANCZOS)
|
38 |
-
image_array = np.asarray(image)
|
39 |
-
normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
|
40 |
-
data[0] = normalized_image_array
|
41 |
-
prediction = model.predict(data)
|
42 |
-
index = np.argmax(prediction)
|
43 |
-
class_name = class_names[index]
|
44 |
-
confidence_score = prediction[0][index]
|
45 |
-
|
46 |
-
c_name = (class_name[2:])[:-1]
|
47 |
-
if c_name == "Yes":
|
48 |
-
tumor_prediction = "Model detected Tumor"
|
49 |
-
other_class = "No"
|
50 |
-
else:
|
51 |
-
other_class = "Yes"
|
52 |
-
tumor_prediction = "Model did not detect Tumor"
|
53 |
-
|
54 |
-
res = {"Labels":[c_name,other_class], "Confidence Score":[(confidence_score*100),(1-confidence_score)*100],"Total":100}
|
55 |
-
data_for_plot = pd.DataFrame.from_dict(res)
|
56 |
-
|
57 |
-
tumor_conf_plt = create_plot(data_for_plot)
|
58 |
-
return tumor_prediction,tumor_conf_plt
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
.
|
63 |
-
|
64 |
-
.
|
65 |
-
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
}
|
73 |
|
74 |
-
.gr-button-lg {
|
75 |
-
z-index: 14;
|
76 |
-
width: 113px;
|
77 |
-
height: 30px;
|
78 |
-
left: 0px;
|
79 |
-
top: 0px;
|
80 |
-
padding: 0px;
|
81 |
-
cursor: pointer !important;
|
82 |
-
background: none rgb(17, 20, 45) !important;
|
83 |
-
border: none !important;
|
84 |
-
text-align: center !important;
|
85 |
-
font-size: 14px !important;
|
86 |
-
font-weight: 500 !important;
|
87 |
-
color: rgb(255, 255, 255) !important;
|
88 |
-
line-height: 1 !important;
|
89 |
-
border-radius: 6px !important;
|
90 |
-
transition: box-shadow 200ms ease 0s, background 200ms ease 0s !important;
|
91 |
-
box-shadow: none !important;
|
92 |
-
}
|
93 |
-
.gr-button-lg:hover{
|
94 |
-
z-index: 14;
|
95 |
-
width: 113px;
|
96 |
-
height: 30px;
|
97 |
-
left: 0px;
|
98 |
-
top: 0px;
|
99 |
-
padding: 0px;
|
100 |
-
cursor: pointer !important;
|
101 |
-
background: none rgb(66, 133, 244) !important;
|
102 |
-
border: none !important;
|
103 |
-
text-align: center !important;
|
104 |
-
font-size: 14px !important;
|
105 |
-
font-weight: 500 !important;
|
106 |
-
color: rgb(255, 255, 255) !important;
|
107 |
-
line-height: 1 !important;
|
108 |
-
border-radius: 6px !important;
|
109 |
-
transition: box-shadow 200ms ease 0s, background 200ms ease 0s !important;
|
110 |
-
box-shadow: rgb(0 0 0 / 23%) 0px 1px 7px 0px !important;
|
111 |
-
}
|
112 |
|
113 |
-
|
114 |
-
|
115 |
-
with gr.
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
submit_button.click(fn=predict_tumor, inputs=[imgInput], outputs=[tumor,plot])
|
126 |
|
127 |
-
|
128 |
-
examples=["pred2.jpg","pred3.jpg"],
|
129 |
inputs=imgInput,
|
130 |
-
outputs=[tumor,plot],
|
131 |
fn=predict_tumor,
|
132 |
cache_examples=True,
|
133 |
)
|
|
|
1 |
from PIL import Image, ImageOps
|
2 |
import numpy as np
|
|
|
3 |
import seaborn as sns
|
4 |
import matplotlib.pyplot as plt
|
5 |
import pandas as pd
|
6 |
from keras.models import load_model
|
7 |
import gradio as gr
|
8 |
|
9 |
+
# Load the model and class names outside the prediction function
|
10 |
+
model = load_model('keras_model.h5', compile=False)
|
11 |
+
class_names = [line.strip() for line in open('labels.txt', 'r')]
|
12 |
|
13 |
def create_plot(data):
|
14 |
+
sns.set_theme(style="whitegrid")
|
15 |
|
16 |
+
f, ax = plt.subplots(figsize=(5, 5))
|
17 |
|
18 |
+
sns.set_color_codes("pastel")
|
19 |
+
sns.barplot(x="Total", y="Labels", data=data, label="Total", color="b")
|
20 |
|
21 |
+
sns.set_color_codes("muted")
|
22 |
+
sns.barplot(x="Confidence Score", y="Labels", data=data, label="Conficence Score", color="b")
|
|
|
|
|
|
|
|
|
23 |
|
24 |
+
ax.legend(ncol=2, loc="lower right", frameon=True)
|
25 |
+
sns.despine(left=True, bottom=True)
|
26 |
+
return f
|
27 |
|
28 |
def predict_tumor(img):
|
29 |
+
np.set_printoptions(suppress=True)
|
30 |
+
|
31 |
+
data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
+
# Resize and preprocess the input image
|
34 |
+
size = (224, 224)
|
35 |
+
image_PIL = Image.fromarray(img)
|
36 |
+
image = ImageOps.fit(image_PIL, size, Image.LANCZOS)
|
37 |
+
image_array = np.asarray(image)
|
38 |
+
normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
|
39 |
+
data[0] = normalized_image_array
|
40 |
+
|
41 |
+
# Make a prediction
|
42 |
+
prediction = model.predict(data)
|
43 |
+
index = np.argmax(prediction)
|
44 |
+
class_name = class_names[index]
|
45 |
+
confidence_score = prediction[0][index]
|
46 |
+
|
47 |
+
c_name = class_name.strip()
|
48 |
+
tumor_prediction = f"Model {'detected' if c_name == 'Yes' else 'did not detect'} Tumor"
|
49 |
+
other_class = 'No' if c_name == 'Yes' else 'Yes'
|
50 |
+
|
51 |
+
# Prepare data for plotting
|
52 |
+
res = {"Labels": [c_name, other_class], "Confidence Score": [(confidence_score * 100), (1 - confidence_score) * 100], "Total": 100}
|
53 |
+
data_for_plot = pd.DataFrame.from_dict(res)
|
54 |
|
55 |
+
tumor_conf_plt = create_plot(data_for_plot)
|
56 |
+
|
57 |
+
return tumor_prediction, tumor_conf_plt
|
|
|
58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
+
# Gradio Interface
|
61 |
+
with gr.Blocks(title="Brain Tumor Detection | Data Science Dojo", css="styles.css") as demo:
|
62 |
+
with gr.Row():
|
63 |
+
with gr.Column(scale=4):
|
64 |
+
with gr.Row():
|
65 |
+
imgInput = gr.Image()
|
66 |
+
with gr.Column(scale=1):
|
67 |
+
tumor = gr.Textbox(label='Presence of Tumor')
|
68 |
+
plot = gr.Plot(label="Plot")
|
69 |
+
|
70 |
+
submit_button = gr.Button(value="Submit")
|
71 |
+
submit_button.click(fn=predict_tumor, inputs=[imgInput], outputs=[tumor, plot])
|
|
|
72 |
|
73 |
+
gr.Examples(
|
74 |
+
examples=["pred2.jpg", "pred3.jpg"],
|
75 |
inputs=imgInput,
|
76 |
+
outputs=[tumor, plot],
|
77 |
fn=predict_tumor,
|
78 |
cache_examples=True,
|
79 |
)
|