|
import streamlit as st |
|
st.set_page_config( |
|
page_title="Brain Tumor Prediction", |
|
page_icon="π¦ ", |
|
layout="centered", |
|
initial_sidebar_state="expanded", |
|
menu_items={ |
|
'Get Help': 'https://iot.neu.edu.tr', |
|
'Report a bug': "https://iot.neu.edu.tr", |
|
'About': "# This is a Brain Tumor Predition App using *CNN* Deep Learning Technique." |
|
} |
|
) |
|
import json |
|
import requests |
|
from PIL import Image |
|
import os |
|
|
|
|
|
import numpy as np |
|
from tensorflow.keras.models import load_model |
|
import cv2 |
|
|
|
|
|
|
|
def load_image(image): |
|
img = Image.open(image) |
|
return img |
|
|
|
def save_uploadedfile(uploadedfile): |
|
with open(os.path.join("images/img",uploadedfile.name),"wb") as f: |
|
f.write(uploadedfile.getbuffer()) |
|
uploaded_location = os.path.join("images/img",uploadedfile.name) |
|
return uploaded_location |
|
|
|
def image_predict (image_file): |
|
model_path = 'application/models/multi_3_brainTumor89.h5' |
|
h5_model = load_model(model_path) |
|
image = cv2.imread(image_file) |
|
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) |
|
image = cv2.resize(image, (512, 512)) |
|
image = np.array(image) / 255 |
|
image = np.expand_dims(image, axis=0) |
|
h5_prediction = h5_model.predict(image) |
|
answer = np.argmax(h5_prediction) |
|
classes = ['Meningioma tumor', 'Glioma tumor', 'Pituitary tumor'] |
|
prediction =classes[int(answer)-1] |
|
print ('Prediction is ', prediction ) |
|
print('Prediction from h5 model: {}'.format(prediction)) |
|
return prediction |
|
|
|
|
|
st.title("π¦ Brain Tumor Prediction App from MRI Images") |
|
|
|
|
|
|
|
col1, spacer, col2 = st.columns([6,1,3], gap="medium") |
|
|
|
with col1: |
|
|
|
image = st.file_uploader("Upload CT Scan", type=["png","jpg","jpeg"]) |
|
|
|
|
|
|
|
if image is not None: |
|
|
|
file_details = {"filename":image.name, "filetype":image.type, |
|
"filesize":image.size} |
|
st.write(file_details) |
|
|
|
|
|
st.image(load_image(image),width=160) |
|
|
|
saved = save_uploadedfile(image) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if st.button ('Analyze'): |
|
with st.spinner('Analyzing...'): |
|
prediction = image_predict(saved) |
|
|
|
st. subheader (f"Image Prediction = {prediction}") |
|
st.success(f"Image Prediction = {prediction}", icon="β
") |
|
|
|
with col2: |
|
|
|
st.write("Developed by AI & IOT Lab https://iot.neu.edu.tr by Olusegun Odewole (oooladeleodewole(at)gmail) ") |
|
|
|
st.image( "https://res.cloudinary.com/segestic/image/upload/v1670497190/covid/images/Y99_ntvrog.jpg", width=200, caption='Sample Brain MRI Image') |
|
|
|
hide_menu_and_footer_style = """ |
|
<style> |
|
#MainMenu {visibility: hidden;} |
|
footer {visibility: hidden;} |
|
|
|
footer:after { |
|
content:'AI IOT app'; |
|
visibility: visible; |
|
display: block; |
|
position: relative; |
|
#background-color: red; |
|
color: blue; |
|
padding: 5px; |
|
top: 2px; |
|
|
|
</style> |
|
""" |
|
st.markdown(hide_menu_and_footer_style, unsafe_allow_html=True) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|