Upload 3 files
Browse files- Heart_ResNet.h5 +3 -0
- app (16).py +172 -0
- requirements (8).txt +6 -0
Heart_ResNet.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:e8ef3302e466550066f7941f280856761c006505cadb69ae3ea2a55713056495
|
3 |
+
size 12056984
|
app (16).py
ADDED
@@ -0,0 +1,172 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import librosa
|
3 |
+
import numpy as np
|
4 |
+
import tensorflow as tf
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
+
from datetime import datetime
|
7 |
+
|
8 |
+
# Load the pre-trained ResNet model
|
9 |
+
@st.cache_resource
|
10 |
+
def load_model():
|
11 |
+
model = tf.keras.models.load_model('Heart_ResNet.h5')
|
12 |
+
return model
|
13 |
+
|
14 |
+
model = load_model()
|
15 |
+
|
16 |
+
# Initialize session state
|
17 |
+
if 'page' not in st.session_state:
|
18 |
+
st.session_state.page = 'π Home'
|
19 |
+
if 'theme' not in st.session_state:
|
20 |
+
st.session_state.theme = 'Light Green'
|
21 |
+
if 'history' not in st.session_state:
|
22 |
+
st.session_state.history = []
|
23 |
+
|
24 |
+
# Custom CSS for theme
|
25 |
+
def apply_theme():
|
26 |
+
if st.session_state.theme == "Light Green":
|
27 |
+
st.markdown("""
|
28 |
+
<style>
|
29 |
+
body, .stApp { background-color: #e8f5e9; }
|
30 |
+
.stApp { color: #004d40; }
|
31 |
+
.stButton > button, .stFileUpload > div {
|
32 |
+
background-color: #004d40;
|
33 |
+
color: white;
|
34 |
+
}
|
35 |
+
.stButton > button:hover, .stFileUpload > div:hover {
|
36 |
+
background-color: #00332c;
|
37 |
+
}
|
38 |
+
</style>
|
39 |
+
""", unsafe_allow_html=True)
|
40 |
+
else:
|
41 |
+
st.markdown("""
|
42 |
+
<style>
|
43 |
+
body, .stApp { background-color: #e0f7fa; }
|
44 |
+
.stApp { color: #006064; }
|
45 |
+
.stButton > button, .stFileUpload > div {
|
46 |
+
background-color: #006064;
|
47 |
+
color: white;
|
48 |
+
}
|
49 |
+
.stButton > button:hover, .stFileUpload > div:hover {
|
50 |
+
background-color: #004d40;
|
51 |
+
}
|
52 |
+
</style>
|
53 |
+
""", unsafe_allow_html=True)
|
54 |
+
|
55 |
+
# Sidebar navigation
|
56 |
+
with st.sidebar:
|
57 |
+
st.title("Heartbeat Analysis π©Ί")
|
58 |
+
st.session_state.page = st.radio(
|
59 |
+
"Navigation",
|
60 |
+
["π Home", "βοΈ Settings", "π€ Profile"],
|
61 |
+
index=["π Home", "βοΈ Settings", "π€ Profile"].index(st.session_state.page)
|
62 |
+
)
|
63 |
+
|
64 |
+
# Audio processing function
|
65 |
+
def process_audio(file_path):
|
66 |
+
SAMPLE_RATE = 22050
|
67 |
+
DURATION = 10
|
68 |
+
input_length = int(SAMPLE_RATE * DURATION)
|
69 |
+
|
70 |
+
X, sr = librosa.load(file_path, sr=SAMPLE_RATE, duration=DURATION)
|
71 |
+
|
72 |
+
if len(X) < input_length:
|
73 |
+
pad_width = input_length - len(X)
|
74 |
+
X = np.pad(X, (0, pad_width), mode='constant')
|
75 |
+
|
76 |
+
mfccs = np.mean(librosa.feature.mfcc(y=X, sr=sr, n_mfcc=52,
|
77 |
+
n_fft=512, hop_length=256).T, axis=0)
|
78 |
+
return mfccs, X, sr
|
79 |
+
|
80 |
+
def classify_audio(filepath):
|
81 |
+
mfccs, waveform, sr = process_audio(filepath)
|
82 |
+
features = mfccs.reshape(1, 52, 1)
|
83 |
+
preds = model.predict(features)
|
84 |
+
class_names = ["artifact", "murmur", "normal"]
|
85 |
+
result = {name: float(preds[0][i]) for i, name in enumerate(class_names)}
|
86 |
+
|
87 |
+
# Store in history
|
88 |
+
st.session_state.history.append({
|
89 |
+
'date': datetime.now().strftime("%Y-%m-%d %H:%M"),
|
90 |
+
'file': filepath,
|
91 |
+
'result': result
|
92 |
+
})
|
93 |
+
|
94 |
+
return result, waveform, sr
|
95 |
+
|
96 |
+
# Page rendering functions
|
97 |
+
def home_page():
|
98 |
+
st.title("Heartbeat Analysis")
|
99 |
+
uploaded_file = st.file_uploader("Upload your heartbeat audio", type=["wav", "mp3"])
|
100 |
+
|
101 |
+
if uploaded_file is not None:
|
102 |
+
st.audio(uploaded_file.read(), format='audio/wav')
|
103 |
+
uploaded_file.seek(0)
|
104 |
+
|
105 |
+
if st.button("Analyze Now"):
|
106 |
+
with st.spinner('Analyzing...'):
|
107 |
+
with open("temp.wav", "wb") as f:
|
108 |
+
f.write(uploaded_file.getbuffer())
|
109 |
+
|
110 |
+
results, waveform, sr = classify_audio("temp.wav")
|
111 |
+
|
112 |
+
st.subheader("Analysis Results")
|
113 |
+
cols = st.columns(3)
|
114 |
+
labels = {
|
115 |
+
'artifact': "π¨ Artifact",
|
116 |
+
'murmur': "π Murmur",
|
117 |
+
'normal': "β€οΈ Normal"
|
118 |
+
}
|
119 |
+
|
120 |
+
for (label, value), col in zip(results.items(), cols):
|
121 |
+
with col:
|
122 |
+
st.metric(labels[label], f"{value*100:.2f}%")
|
123 |
+
|
124 |
+
st.subheader("Heartbeat Waveform")
|
125 |
+
fig, ax = plt.subplots(figsize=(10, 3))
|
126 |
+
librosa.display.waveshow(waveform, sr=sr, ax=ax)
|
127 |
+
ax.set_title("Audio Waveform Analysis")
|
128 |
+
st.pyplot(fig)
|
129 |
+
|
130 |
+
def settings_page():
|
131 |
+
st.title("Settings")
|
132 |
+
new_theme = st.selectbox(
|
133 |
+
"Select Theme",
|
134 |
+
["Light Green", "Light Blue"],
|
135 |
+
index=0 if st.session_state.theme == "Light Green" else 1
|
136 |
+
)
|
137 |
+
|
138 |
+
if new_theme != st.session_state.theme:
|
139 |
+
st.session_state.theme = new_theme
|
140 |
+
st.experimental_rerun()
|
141 |
+
|
142 |
+
def profile_page():
|
143 |
+
st.title("Medical Profile")
|
144 |
+
with st.expander("Personal Information", expanded=True):
|
145 |
+
col1, col2 = st.columns(2)
|
146 |
+
with col1:
|
147 |
+
st.write("**Name:** Kpetaa Patrick")
|
148 |
+
st.write("**Age:** 35")
|
149 |
+
with col2:
|
150 |
+
st.write("**Blood Type:** O+")
|
151 |
+
st.write("**Last Checkup:** 2025-06-17")
|
152 |
+
|
153 |
+
st.subheader("Analysis History")
|
154 |
+
if not st.session_state.history:
|
155 |
+
st.write("No previous analyses found")
|
156 |
+
else:
|
157 |
+
for analysis in reversed(st.session_state.history):
|
158 |
+
with st.expander(f"Analysis from {analysis['date']}"):
|
159 |
+
st.write(f"File: {analysis['file']}")
|
160 |
+
st.write("Results:")
|
161 |
+
for label, value in analysis['result'].items():
|
162 |
+
st.progress(value, text=f"{label.capitalize()}: {value*100:.2f}%")
|
163 |
+
|
164 |
+
# Main app logic
|
165 |
+
apply_theme()
|
166 |
+
|
167 |
+
if st.session_state.page == "π Home":
|
168 |
+
home_page()
|
169 |
+
elif st.session_state.page == "βοΈ Settings":
|
170 |
+
settings_page()
|
171 |
+
elif st.session_state.page == "π€ Profile":
|
172 |
+
profile_page()
|
requirements (8).txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
librosa
|
3 |
+
numpy
|
4 |
+
tensorflow
|
5 |
+
matplotlib
|
6 |
+
datetime
|