Spaces:
Runtime error
Runtime error
Commit ·
e007f6f
1
Parent(s): a152f54
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from keras.models import load_model
|
| 2 |
+
from PIL import Image, ImageOps
|
| 3 |
+
import numpy as np
|
| 4 |
+
from collections import OrderedDict
|
| 5 |
+
import seaborn as sns
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
+
import pandas as pd
|
| 8 |
+
import gradio as gr
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def create_plot(data):
|
| 12 |
+
sns.set_theme(style="whitegrid")
|
| 13 |
+
|
| 14 |
+
f, ax = plt.subplots(figsize=(5, 5))
|
| 15 |
+
|
| 16 |
+
sns.set_color_codes("pastel")
|
| 17 |
+
sns.barplot(x="Total", y="Labels", data=data,label="Total", color="b")
|
| 18 |
+
|
| 19 |
+
sns.set_color_codes("muted")
|
| 20 |
+
sns.barplot(x="Confidence Score", y="Labels", data=data,label="Conficence Score", color="b")
|
| 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 |
+
np.set_printoptions(suppress=True)
|
| 29 |
+
model = load_model('keras_model.h5', compile=False)
|
| 30 |
+
class_names = open('labels.txt', 'r').readlines()
|
| 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 |
+
with gr.Blocks() as demo:
|
| 63 |
+
with gr.Row():
|
| 64 |
+
with gr.Column(scale=4):
|
| 65 |
+
with gr.Row():
|
| 66 |
+
imgInput = gr.Image()
|
| 67 |
+
with gr.Column(scale=1):
|
| 68 |
+
tumor = gr.Textbox(label='Presence of Tumor')
|
| 69 |
+
plot = gr.Plot(label="Plot")
|
| 70 |
+
|
| 71 |
+
submit_button = gr.Button(value="Submit")
|
| 72 |
+
submit_button.click(fn=predict_tumor, inputs=[imgInput], outputs=[tumor,plot])
|
| 73 |
+
|
| 74 |
+
gr.Examples(
|
| 75 |
+
examples=["pred2.jpg","pred3.jpg"],
|
| 76 |
+
inputs=imgInput,
|
| 77 |
+
outputs=[tumor,plot],
|
| 78 |
+
fn=predict_tumor,
|
| 79 |
+
cache_examples=True,
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
demo.launch()
|