Spaces:
Runtime error
Runtime error
AbdullaShafeeg
commited on
Commit
·
cf4974d
1
Parent(s):
75b78d9
update
Browse files
app.py
CHANGED
@@ -13,103 +13,94 @@ from scipy.io import wavfile
|
|
13 |
model = torch.jit.load("snorenetv1_small.ptl")
|
14 |
model.eval()
|
15 |
|
16 |
-
# Session state
|
17 |
-
if 'text' not in st.session_state:
|
18 |
-
st.session_state['text'] = 'Listening...'
|
19 |
-
st.session_state['run'] = False
|
20 |
|
21 |
# Audio parameters
|
22 |
-
st.sidebar.header('Audio Parameters')
|
23 |
-
|
24 |
-
FRAMES_PER_BUFFER = int(st.sidebar.text_input('Frames per buffer', 3200))
|
25 |
-
FORMAT = 'audio/wav'
|
26 |
-
CHANNELS = 1
|
27 |
-
RATE = int(st.sidebar.text_input('Rate', 16000))
|
28 |
-
|
29 |
-
# Open an audio stream
|
30 |
-
|
31 |
-
monitoring = False
|
32 |
-
audio_data = []
|
33 |
-
|
34 |
-
def start_monitoring():
|
35 |
-
global monitoring
|
36 |
-
st.session_state['run'] = True
|
37 |
-
monitoring = True
|
38 |
-
|
39 |
-
def stop_monitoring():
|
40 |
-
global monitoring
|
41 |
-
st.session_state['run'] = False
|
42 |
-
monitoring = False
|
43 |
-
|
44 |
-
st.title('🎙️ Real-Time Snore Detection App')
|
45 |
-
|
46 |
-
with st.expander('About this App'):
|
47 |
-
st.markdown('''
|
48 |
-
This streamlit app from Hypermind Labs Helps users detect
|
49 |
-
how much they are snoring during their sleep.
|
50 |
-
''')
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
wav_audio_data = st_audiorec()
|
55 |
-
if wav_audio_data is not None:
|
56 |
-
data = np.frombuffer(wav_audio_data, dtype=np.int16)
|
57 |
-
st.write(len(data))
|
58 |
-
duration = len(data)//110000
|
59 |
-
num_of_samples = len(data)
|
60 |
-
sample_rate = num_of_samples // duration
|
61 |
-
# data = np.array(wav_audio_data, dtype=float)
|
62 |
-
max_abs_value = np.max(np.abs(data))
|
63 |
-
np_array = (data/max_abs_value) * 32767
|
64 |
-
scaled_data = np_array.astype(np.int16).tobytes()
|
65 |
-
with io.BytesIO() as fp, wave.open(fp, mode="wb") as waveobj:
|
66 |
-
waveobj.setnchannels(1)
|
67 |
-
waveobj.setframerate(96000)
|
68 |
-
waveobj.setsampwidth(2)
|
69 |
-
waveobj.setcomptype("NONE", "NONE")
|
70 |
-
waveobj.writeframes(scaled_data)
|
71 |
-
wav_make = fp.getvalue()
|
72 |
-
|
73 |
-
with open("output.wav", 'wb') as wav_file:
|
74 |
-
wav_file.write(wav_make)
|
75 |
-
sr, waveform = wavfile.read('output.wav')
|
76 |
-
snore = 0
|
77 |
-
other = 0
|
78 |
-
s=0
|
79 |
-
n=16000
|
80 |
-
endReached = False
|
81 |
-
|
82 |
-
while(endReached==False):
|
83 |
-
input_tensor = torch.tensor(waveform[s:n]).unsqueeze(0).to(torch.float32)
|
84 |
-
result = model(input_tensor)
|
85 |
-
if np.abs(result[0][0]) > np.abs(result[0][1]):
|
86 |
-
other += 1
|
87 |
-
else:
|
88 |
-
snore += 1
|
89 |
-
s += 16000
|
90 |
-
n += 16000
|
91 |
-
if(n >= len(waveform)):
|
92 |
-
endReached = True
|
93 |
-
|
94 |
-
# PERCENTAGE OF SNORING PLOT
|
95 |
-
|
96 |
-
total = snore + other
|
97 |
-
snore_percentage = (snore / total) * 100
|
98 |
-
other_percentage = (other / total) * 100
|
99 |
-
|
100 |
-
categories = ["Snore", "Other"]
|
101 |
-
percentages = [snore_percentage, other_percentage]
|
102 |
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
|
114 |
|
115 |
|
|
|
13 |
model = torch.jit.load("snorenetv1_small.ptl")
|
14 |
model.eval()
|
15 |
|
|
|
|
|
|
|
|
|
16 |
|
17 |
# Audio parameters
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
st.sidebar.markdown(
|
20 |
+
"""
|
21 |
+
<div align="justify">
|
22 |
+
<h4>ABOUT</h4>
|
23 |
+
<p>Transform your sleep experience with the cutting-edge Snore Detector by Hypermind Labs!
|
24 |
+
Discover the power to monitor and understand your nighttime sounds like never before.
|
25 |
+
Take control of your sleep quality and uncover the secrets of your peaceful slumber with our innovative app.</p>
|
26 |
+
</div>
|
27 |
+
""",
|
28 |
+
unsafe_allow_html=True,
|
29 |
+
)
|
30 |
+
st.title('Real-Time Snore Detection App 😴')
|
31 |
+
|
32 |
+
upload_file = st.file_uploader("Upload wav file", type=["wav"])
|
33 |
+
if upload_file is not None:
|
34 |
+
file_details = {
|
35 |
+
"Filename": upload_file.name,
|
36 |
+
"Filesize":f"{upload_file.size / 1024:.2f} KB",
|
37 |
+
"File Type": upload_file.type,
|
38 |
+
}
|
39 |
+
st.write("File Details:", file_details)
|
40 |
+
|
41 |
+
with open("saved_file.wav", "wb") as f:
|
42 |
+
f.write(upload_file.getvalue())
|
43 |
+
|
44 |
+
# wav_audio_data = None
|
45 |
+
# if wav_audio_data is not None:
|
46 |
+
# data = np.frombuffer(wav_audio_data, dtype=np.int16)
|
47 |
+
# st.write(len(data))
|
48 |
+
# duration = len(data)//110000
|
49 |
+
# num_of_samples = len(data)
|
50 |
+
# sample_rate = num_of_samples // duration
|
51 |
+
# # data = np.array(wav_audio_data, dtype=float)
|
52 |
+
# max_abs_value = np.max(np.abs(data))
|
53 |
+
# np_array = (data/max_abs_value) * 32767
|
54 |
+
# scaled_data = np_array.astype(np.int16).tobytes()
|
55 |
+
# with io.BytesIO() as fp, wave.open(fp, mode="wb") as waveobj:
|
56 |
+
# waveobj.setnchannels(1)
|
57 |
+
# waveobj.setframerate(96000)
|
58 |
+
# waveobj.setsampwidth(2)
|
59 |
+
# waveobj.setcomptype("NONE", "NONE")
|
60 |
+
# waveobj.writeframes(scaled_data)
|
61 |
+
# wav_make = fp.getvalue()
|
62 |
+
|
63 |
+
# with open("output.wav", 'wb') as wav_file:
|
64 |
+
# wav_file.write(wav_make)
|
65 |
+
|
66 |
+
sr, waveform = wavfile.read('saved_file.wav')
|
67 |
+
snore = 0
|
68 |
+
other = 0
|
69 |
+
s=0
|
70 |
+
n=16000
|
71 |
+
endReached = False
|
72 |
+
|
73 |
+
while(endReached==False):
|
74 |
+
input_tensor = torch.tensor(waveform[s:n]).unsqueeze(0).to(torch.float32)
|
75 |
+
result = model(input_tensor)
|
76 |
+
if np.abs(result[0][0]) > np.abs(result[0][1]):
|
77 |
+
other += 1
|
78 |
+
else:
|
79 |
+
snore += 1
|
80 |
+
s += 16000
|
81 |
+
n += 16000
|
82 |
+
if(n >= len(waveform)):
|
83 |
+
endReached = True
|
84 |
+
|
85 |
+
# PERCENTAGE OF SNORING PLOT
|
86 |
+
|
87 |
+
total = snore + other
|
88 |
+
snore_percentage = (snore / total) * 100
|
89 |
+
other_percentage = (other / total) * 100
|
90 |
+
|
91 |
+
categories = ["Snore", "Other"]
|
92 |
+
percentages = [snore_percentage, other_percentage]
|
93 |
+
|
94 |
+
plt.figure(figsize=(8, 4))
|
95 |
+
plt.barh(categories, percentages, color=['#ff0033', '#00ffee'])
|
96 |
+
plt.xlabel('Percentage')
|
97 |
+
plt.title('Percentage of Snoring')
|
98 |
+
plt.xlim(0, 100)
|
99 |
+
|
100 |
+
for i, percentage in enumerate(percentages):
|
101 |
+
plt.text(percentage, i, f' {percentage:.2f}%', va='center')
|
102 |
+
|
103 |
+
st.pyplot(plt)
|
104 |
|
105 |
|
106 |
|