Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
|
3 |
+
import io
|
4 |
+
import numpy as np
|
5 |
+
from PIL import Image
|
6 |
+
import streamlit as st
|
7 |
+
import tensorflow as tf
|
8 |
+
import matplotlib.pyplot as plt
|
9 |
+
from tensorflow import keras
|
10 |
+
|
11 |
+
|
12 |
+
st.title('Brain \U0001F9E0 Tumor Detector ')
|
13 |
+
|
14 |
+
st.subheader('Find out whether there is a tumor \U0001F534 (Glioma Meningioma Pituarie) in the brain (or) \
|
15 |
+
not \U0001F7E2')
|
16 |
+
|
17 |
+
|
18 |
+
|
19 |
+
|
20 |
+
inp_t = st.file_uploader(label='Upload MRI here', accept_multiple_files=True)
|
21 |
+
|
22 |
+
|
23 |
+
|
24 |
+
def load_img(path):
|
25 |
+
# reading file object and making it to pil image and to np array
|
26 |
+
img_l = []
|
27 |
+
for i in path:
|
28 |
+
img_byte = i.read()
|
29 |
+
img = Image.open(io.BytesIO(img_byte))
|
30 |
+
img = img.resize((64, 64), Image.ANTIALIAS)
|
31 |
+
if img.mode != 'L':
|
32 |
+
img = img.convert('L')
|
33 |
+
img_arr = np.array(img, dtype='float32')/255
|
34 |
+
img_arr = np.expand_dims(img_arr, axis=-1)
|
35 |
+
img_l.append(img_arr)
|
36 |
+
img = np.stack(img_l)
|
37 |
+
return img
|
38 |
+
|
39 |
+
# prediction
|
40 |
+
|
41 |
+
|
42 |
+
def pred(img):
|
43 |
+
# Load TFLite model and allocate tensors.
|
44 |
+
model = keras.models.load_model('model2_weights.h5')
|
45 |
+
result = model.predict(img)
|
46 |
+
|
47 |
+
return result
|
48 |
+
|
49 |
+
|
50 |
+
|
51 |
+
# if file is uploaded
|
52 |
+
if inp_t:
|
53 |
+
img = load_img(inp_t)
|
54 |
+
result = ['Glioma', 'Meningioma', 'no tumor', 'Pituarie']
|
55 |
+
|
56 |
+
st.warning(
|
57 |
+
'** Uploaded {} images [View images in side Panel]'.format(img.shape[0]))
|
58 |
+
|
59 |
+
res = pred(img)
|
60 |
+
max_value = res[0][np.argmax(res)]*100
|
61 |
+
if (result[np.argmax(res)] == 'no tumor'):
|
62 |
+
st.subheader("\U0001F7E2 Model predicts there is {} tumor with {:.2f} % confidence].\U0001F7E2".format(result[np.argmax(res)], max_value))
|
63 |
+
else:
|
64 |
+
st.subheader("\U0001F534 Model predicts there is {} tumor with {:.2f} % confidence.\U0001F534 ".format(result[np.argmax(res)], max_value))
|
65 |
+
|
66 |
+
|
67 |
+
st.write('\n')
|
68 |
+
|
69 |
+
st.image(inp_t, width = 400)
|
70 |
+
|
71 |
+
|
72 |
+
st.markdown('---')
|
73 |
+
st.error(
|
74 |
+
'Dont conclude by looking at predictions, just take them as a reference!!')
|
75 |
+
|