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