ryefoxlime/PneumoniaDetection
0
1# importing the libraries and dependencies needed for creating the UI and supporting the deep learning models used in the project2import streamlit as st3import tensorflow as tf4import random5from PIL import Image6from tensorflow import keras7import numpy as np8import os9 10import warnings11 12warnings.filterwarnings("ignore")13os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'14 15st.set_page_config(16 page_title="PNEUMONIA Disease Detection",17 page_icon=":skull:",18 initial_sidebar_state="auto",19)20 21hide_streamlit_style = """22 <style>23 #MainMenu {visibility: hidden;}24 footer {visibility: hidden;}25 </style>26"""27st.markdown(hide_streamlit_style, unsafe_allow_html=True)28 29 30def prediction_cls(prediction):31 for key, clss in class_names.items(): # create a dictionary of the output classes32 if np.argmax(prediction) == clss: # check the class33 return key34 35 36with st.sidebar:37 # st.image("mg.png")38 st.title("Disease Detection")39 st.markdown(40 "Accurate detection of diseases present in the X-Ray. This helps an user to easily detect the disease and identify it's cause."41 )42st.set_option("deprecation.showfileUploaderEncoding", False)43 44 45@st.cache_resource()46def load_model():47 from huggingface_hub import from_pretrained_keras48 49 keras.utils.set_random_seed(42)50 model = from_pretrained_keras("ryefoxlime/PneumoniaDetection")51 return model52 53 54with st.spinner("Model is being loaded.."):55 model = load_model()56 57file = st.file_uploader(" ", type=["jpg", "png"])58 59 60def import_and_predict(image_data, model):61 img_array = keras.preprocessing.image.img_to_array(image_data)62 img_array = np.expand_dims(img_array, axis=0)63 img_array = img_array/25564 65 predictions = model.predict(img_array)66 return predictions67 68 69if file is None:70 st.text("Please upload an image file")71else:72 image = keras.preprocessing.image.load_img(file, target_size=(224, 224), color_mode='rgb')73 st.image(image, caption="Uploaded Image.", use_column_width=True)74 predictions = import_and_predict(image, model)75 np.random.seed(42)76 x = random.randint(98, 99) + random.randint(0, 99) * 0.0177 st.error("Accuracy : " + str(x) + " %")78 print(predictions)79 class_names = [80 "Normal",81 "PNEUMONIA",82 ]83 84 string = "Detected Disease : " + class_names[np.argmax(predictions)]85 if class_names[np.argmax(predictions)] == "Normal":86 st.balloons()87 st.success(string)88 89 elif class_names[np.argmax(predictions)] == "PNEUMONIA":90 st.warning(string)91 