forked from arpy8/Omdena_Myanmar_Streamlit_App
-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.py
136 lines (118 loc) · 6.31 KB
/
model.py
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import numpy as np
import streamlit as st
import tensorflow as tf
from tensorflow.keras.preprocessing import image
# Constants for model paths
CANCER_MODEL_PATH = 'models/cancer_model.h5'
TUBERCULOSIS_MODEL_PATH = 'models/tuberculosis_model.h5'
PNEUMONIA_MODEL_PATH = 'models/pneumonia_model.h5'
COVID_MODEL_PATH = 'models/covid_model.h5'
# Constants for class labels
CANCER_CLASS_LABELS = ['Benign', 'Malignant', 'Normal']
TUBERCULOSIS_CLASS_LABELS = ['Normal', 'Tuberculosis']
PNEUMONIA_CLASS_LABELS = ['Normal', 'Pneumonia']
COVID_CLASS_LABELS = ['Covid', 'Normal']
ALZ_CLASS_LABELS = ['Alzheimer','Normal']
# Common preprocessing and prediction function
def preprocess_and_predict(model, class_labels, image_file, target_size, color_mode=None, scale_factor=1.0):
try:
img = image.load_img(image_file, target_size=target_size, color_mode=color_mode)
img_array = image.img_to_array(img)
img_array = np.expand_dims(img_array, axis=0)
img_array = img_array / scale_factor
pred = model.predict(img_array)
predicted_class = class_labels[np.argmax(pred)]
confidence = round(100 * np.max(pred), 2)
return predicted_class, confidence
except ValueError:
img = image.load_img(image_file, target_size=(150, 150))
img = image.img_to_array(img)
img = img / 255.0
img = np.expand_dims(img, axis=0)
prediction = model.predict(img)
predicted_class = class_labels[int(np.round(prediction[0][0]))]
confidence = prediction[0][0]
return predicted_class, confidence
def cancer_page():
st.title("Cancer Detection System")
uploaded_file = st.file_uploader("Upload chest x-ray image here...", type=["jpg", "png", "jpeg"])
if uploaded_file:
model = tf.keras.models.load_model(CANCER_MODEL_PATH)
predict_button = st.button("ㅤㅤPredictㅤㅤ")
if predict_button:
predicted_class, confidence = preprocess_and_predict(model, CANCER_CLASS_LABELS, uploaded_file, (256, 256),
'grayscale', 255.0)
if predicted_class == "Normal":
st.success(f"""##### Predicted Class: **{predicted_class}**""")
elif predicted_class == "Benign":
st.warning(f"""##### Predicted Class: **{predicted_class}**""")
elif predicted_class == "Malignant":
st.error(f"""##### Predicted Class: **{predicted_class}**""")
st.info(f"""##### Confidence: **{confidence} %**""")
def covid_page():
st.title("Covid Detection System")
uploaded_file = st.file_uploader("Upload chest x-ray image here...", type=["jpg", "png", "jpeg"])
if uploaded_file:
model = tf.keras.models.load_model(COVID_MODEL_PATH)
predict_button = st.button("ㅤㅤPredictㅤㅤ")
if predict_button:
predicted_class, confidence = preprocess_and_predict(model, COVID_CLASS_LABELS, uploaded_file, (150, 150),
'grayscale', 255.0)
if predicted_class == "Normal":
st.success(f"""##### Predicted Class: **{predicted_class}**""")
elif predicted_class == "Covid":
st.error(f"""##### Predicted Class: **{predicted_class}**""")
st.info(f"""##### Confidence: **{100 - confidence} %**""")
def pneumonia_page():
st.title("Pneumonia Detection System")
uploaded_file = st.file_uploader("Upload chest x-ray image here...", type=["jpg", "png", "jpeg"])
if uploaded_file:
model = tf.keras.models.load_model(PNEUMONIA_MODEL_PATH)
predict_button = st.button("ㅤㅤPredictㅤㅤ")
if predict_button:
predicted_class, confidence = preprocess_and_predict(model, PNEUMONIA_CLASS_LABELS, uploaded_file,
(256, 256), 'grayscale', 259.0)
if predicted_class == "Normal":
st.success(f"""##### Predicted Class: **{predicted_class}**""")
elif predicted_class == "Pneumonia":
st.error(f"""##### Predicted Class: **{predicted_class}**""")
# st.info(f"""##### Confidence: **{confidence} %**""")
def tuberculosis_page():
def preprocess_and_predict(image_file):
try:
img = image.load_img(image_file, target_size=(28, 28), color_mode="rgb")
img_array = image.img_to_array(img)
img_array = img_array / 255
img_array = np.expand_dims(img_array, axis=0)
pred = model.predict(img_array)
predicted_class = TUBERCULOSIS_CLASS_LABELS[np.argmax(pred)]
return predicted_class
except Exception as e:
st.error(f"Error processing and predicting: {e}")
return "Error"
st.title("Tuberculosis Detection System")
uploaded_file = st.file_uploader("Upload chest x-ray image here...", type=["jpg", "png", "jpeg"])
if uploaded_file:
model = tf.keras.models.load_model(TUBERCULOSIS_MODEL_PATH)
predict_button = st.button("ㅤㅤPredictㅤㅤ")
if predict_button:
predicted_class = preprocess_and_predict(uploaded_file)
if predicted_class == "Normal":
st.success(f"""##### Predicted Class: **{predicted_class}**""")
elif predicted_class == "Tuberculosis":
st.error(f"""##### Predicted Class: **{predicted_class}**""")
# st.info(f"""##### Confidence: **{confidence}**""")
def alzheimer_page():
st.title("Alzheimer Detection System")
uploaded_file = st.file_uploader("Upload Brain Scan image here...", type=["jpg", "png", "jpeg"])
if uploaded_file:
model = tf.keras.models.load_model(PNEUMONIA_MODEL_PATH)
predict_button = st.button("ㅤㅤPredictㅤㅤ")
if predict_button:
predicted_class, confidence = preprocess_and_predict(model, PNEUMONIA_CLASS_LABELS, uploaded_file,
(256, 256), 'grayscale', 259.0)
if predicted_class == "Normal":
st.success(f"""##### Predicted Class: **{predicted_class}**""")
elif predicted_class == "Alzheimer":
st.error(f"""##### Predicted Class: **{predicted_class}**""")
# st.info(f"""##### Confidence: **{confidence} %**""")