Spaces:
Runtime error
Runtime error
datasciencedojo
commited on
Commit
·
215728f
1
Parent(s):
1b0d9e3
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
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_pneumonia(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 == "Normal":
|
48 |
+
pneumonia_prediction = "Chest XRay is normal no signs of pneumonia"
|
49 |
+
other_class = "Pneumonia"
|
50 |
+
else:
|
51 |
+
other_class = "Pneumonia"
|
52 |
+
pneumonia_prediction = "Chest XRay shows signs of pneumonia"
|
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 |
+
pneumonia_conf_plt = create_plot(data_for_plot)
|
58 |
+
return pneumonia_prediction,pneumonia_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 |
+
pneumonia = gr.Textbox(label='Presence of pneumonia')
|
69 |
+
plot = gr.Plot(label="Plot")
|
70 |
+
|
71 |
+
submit_button = gr.Button(value="Submit")
|
72 |
+
submit_button.click(fn=predict_pneumonia, inputs=[imgInput], outputs=[pneumonia,plot])
|
73 |
+
|
74 |
+
gr.Examples(
|
75 |
+
examples=["normal_Sample.jpg","pneumonia_sample.jpg"],
|
76 |
+
inputs=imgInput,
|
77 |
+
outputs=[pneumonia,plot],
|
78 |
+
fn=predict_pneumonia,
|
79 |
+
cache_examples=True,
|
80 |
+
)
|
81 |
+
|
82 |
+
demo.launch()
|