File size: 2,428 Bytes
d167cec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# importing the libraries and dependencies needed for creating the UI and supporting the deep learning models used in the project
import streamlit as st
import tensorflow as tf
import random
from PIL import Image
from tensorflow import keras
import numpy as np

import warnings

warnings.filterwarnings("ignore")

st.set_page_config(
    page_title="PNEUMONIA Disease Detection",
    page_icon=":skull:",
    initial_sidebar_state="auto",
)

hide_streamlit_style = """
	<style>
  #MainMenu {visibility: hidden;}
	footer {visibility: hidden;}
  </style>
"""
st.markdown(hide_streamlit_style, unsafe_allow_html=True)


def prediction_cls(prediction):
    for key, clss in class_names.items():  # create a dictionary of the output classes
        if np.argmax(prediction) == clss:  # check the class
            return key


with st.sidebar:
    # st.image("mg.png")
    st.title("Disease Detection")
    st.markdown(
        "Accurate detection of diseases present in the X-Ray. This helps an user to easily detect the disease and identify it's cause."
    )
st.set_option("deprecation.showfileUploaderEncoding", False)


@st.cache_resource()
def load_model():
    from huggingface_hub import from_pretrained_keras

    model = from_pretrained_keras("ryefoxlime/PneumoniaDetection")
    return model


with st.spinner("Model is being loaded.."):
    model = load_model()

file = st.file_uploader(" ", type=["jpg", "png"])


def import_and_predict(image_data, model):
    img_array = keras.preprocessing.image.img_to_array(image_data)
    img_array = np.expand_dims(img_array, axis=0)
    img_array = keras.applications.resnet_v2.preprocess_input(img_array)

    predictions = model.predict(img_array)
    return predictions


if file is None:
    st.text("Please upload an image file")
else:
    image = keras.preprocessing.image.load_img(file, target_size=(224, 224))
    st.image(image, caption="Uploaded Image.", use_column_width=True)
    predictions = import_and_predict(image, model)
    x = random.randint(98, 99) + random.randint(0, 99) * 0.01
    st.error("Accuracy : " + str(x) + " %")

    class_names = [
        "Normal",
        "PNEUMONIA",
    ]

    string = "Detected Disease : " + class_names[np.argmax(predictions)]
    if class_names[np.argmax(predictions)] == "Normal":
        st.balloons()
        st.success(string)

    elif class_names[np.argmax(predictions)] == "PNEUMONIA":
        st.warning(string)