IA / a.py
0xgaryy's picture
Upload a.py
675fa95
raw
history blame
2.07 kB
import io
import numpy as np
from PIL import Image
import streamlit as st
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow import keras
st.title('Brain \U0001F9E0 Tumor Detector ')
st.subheader('Find out whether there is a tumor \U0001F534 (Glioma Meningioma Pituarie) in the brain (or) \
not \U0001F7E2')
inp_t = st.file_uploader(label='Upload MRI here', accept_multiple_files=True)
def load_img(path):
# reading file object and making it to pil image and to np array
img_l = []
for i in path:
img_byte = i.read()
img = Image.open(io.BytesIO(img_byte))
img = img.resize((64, 64), Image.ANTIALIAS)
if img.mode != 'L':
img = img.convert('L')
img_arr = np.array(img, dtype='float32')/255
img_arr = np.expand_dims(img_arr, axis=-1)
img_l.append(img_arr)
img = np.stack(img_l)
return img
# prediction
def pred(img):
# Load TFLite model and allocate tensors.
model = keras.models.load_model('model2_weights.h5')
result = model.predict(img)
return result
# if file is uploaded
if inp_t:
img = load_img(inp_t)
result = ['Glioma', 'Meningioma', 'no tumor', 'Pituarie']
st.warning(
'** Uploaded {} images [View images in side Panel]'.format(img.shape[0]))
res = pred(img)
max_value = res[0][np.argmax(res)]*100
if (result[np.argmax(res)] == 'no tumor'):
st.subheader("\U0001F7E2 Model predicts there is {} tumor with {:.2f} % confidence].\U0001F7E2".format(result[np.argmax(res)], max_value))
else:
st.subheader("\U0001F534 Model predicts there is {} tumor with {:.2f} % confidence.\U0001F534 ".format(result[np.argmax(res)], max_value))
st.write('\n')
st.image(inp_t, width = 400)
st.markdown('---')
st.error(
'Dont conclude by looking at predictions, just take them as a reference!!')