Spaces:
Runtime error
Runtime error
AbdullaShafeeg
commited on
Commit
·
ab9c217
1
Parent(s):
28cc6c6
big update
Browse files
app.py
CHANGED
@@ -5,7 +5,7 @@ import sounddevice as sd
|
|
5 |
import numpy as np
|
6 |
import pandas as pd
|
7 |
import torch
|
8 |
-
import torchaudio
|
9 |
import wave
|
10 |
import io
|
11 |
from scipy.io import wavfile
|
@@ -15,7 +15,11 @@ import time
|
|
15 |
# MODEL LOADING and INITIALISATION
|
16 |
model = torch.jit.load("snorenetv1_small.ptl")
|
17 |
model.eval()
|
18 |
-
|
|
|
|
|
|
|
|
|
19 |
|
20 |
# Audio parameters
|
21 |
|
@@ -32,86 +36,47 @@ st.sidebar.markdown(
|
|
32 |
)
|
33 |
st.title('Real-Time Snore Detection App 😴')
|
34 |
|
35 |
-
|
36 |
-
if
|
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 |
-
audio.export(upload_file.name, format='wav')
|
77 |
-
sr, waveform = wavfile.read(upload_file.name)
|
78 |
-
snore = 0
|
79 |
-
other = 0
|
80 |
-
s=0
|
81 |
-
n=16000
|
82 |
-
endReached = False
|
83 |
-
|
84 |
-
while(endReached==False):
|
85 |
-
input_tensor = torch.tensor(waveform[s:n]).unsqueeze(0).to(torch.float32)
|
86 |
-
result = model(input_tensor)
|
87 |
-
if np.abs(result[0][0]) > np.abs(result[0][1]):
|
88 |
-
other += 1
|
89 |
-
else:
|
90 |
-
snore += 1
|
91 |
-
s += 16000
|
92 |
-
n += 16000
|
93 |
-
if(n >= len(waveform)):
|
94 |
-
endReached = True
|
95 |
-
|
96 |
-
# PERCENTAGE OF SNORING PLOT
|
97 |
-
|
98 |
-
total = snore + other
|
99 |
-
snore_percentage = (snore / total) * 100
|
100 |
-
other_percentage = (other / total) * 100
|
101 |
-
|
102 |
-
categories = ["Snore", "Other"]
|
103 |
-
percentages = [snore_percentage, other_percentage]
|
104 |
-
|
105 |
-
plt.figure(figsize=(8, 4))
|
106 |
-
plt.barh(categories, percentages, color=['#ff0033', '#00ffee'])
|
107 |
-
plt.xlabel('Percentage')
|
108 |
-
plt.title('Percentage of Snoring')
|
109 |
-
plt.xlim(0, 100)
|
110 |
-
|
111 |
-
for i, percentage in enumerate(percentages):
|
112 |
-
plt.text(percentage, i, f' {percentage:.2f}%', va='center')
|
113 |
-
st.write("DONE")
|
114 |
-
st.pyplot(plt)
|
115 |
|
116 |
|
117 |
|
|
|
5 |
import numpy as np
|
6 |
import pandas as pd
|
7 |
import torch
|
8 |
+
# import torchaudio
|
9 |
import wave
|
10 |
import io
|
11 |
from scipy.io import wavfile
|
|
|
15 |
# MODEL LOADING and INITIALISATION
|
16 |
model = torch.jit.load("snorenetv1_small.ptl")
|
17 |
model.eval()
|
18 |
+
endReached = False
|
19 |
+
snore = 0
|
20 |
+
other = 0
|
21 |
+
s=0
|
22 |
+
n=16000
|
23 |
|
24 |
# Audio parameters
|
25 |
|
|
|
36 |
)
|
37 |
st.title('Real-Time Snore Detection App 😴')
|
38 |
|
39 |
+
uploaded_file = st.file_uploader("Upload Sample", type=["wav"])
|
40 |
+
if uploaded_file is not None:
|
41 |
+
st.write("Saving File.....")
|
42 |
+
audio = pydub.AudioSegment.from_wav(uploaded_file)
|
43 |
+
audio.export(uploaded_file.name, format='wav')
|
44 |
+
time.sleep(2)
|
45 |
+
st.write("File Saved!")
|
46 |
+
|
47 |
+
st.write("Analysing...")
|
48 |
+
while(endReached==False):
|
49 |
+
sr, waveform = wavfile.read(uploaded_file.name)
|
50 |
+
input_tensor = torch.tensor(waveform[s:n]).unsqueeze(0).to(torch.float32)
|
51 |
+
result = model(input_tensor)
|
52 |
+
if np.abs(result[0][0]) > np.abs(result[0][1]):
|
53 |
+
other += 1
|
54 |
+
else:
|
55 |
+
snore += 1
|
56 |
+
s += 16000
|
57 |
+
n += 16000
|
58 |
+
if(n >= len(waveform)):
|
59 |
+
endReached = True
|
60 |
+
|
61 |
+
# PERCENTAGE OF SNORING PLOT
|
62 |
+
if endReached == True:
|
63 |
+
total = snore + other
|
64 |
+
snore_percentage = (snore / total) * 100
|
65 |
+
other_percentage = (other / total) * 100
|
66 |
+
|
67 |
+
categories = ["Snore", "Other"]
|
68 |
+
percentages = [snore_percentage, other_percentage]
|
69 |
+
|
70 |
+
plt.figure(figsize=(8, 4))
|
71 |
+
plt.barh(categories, percentages, color=['#ff0033', '#00ffee'])
|
72 |
+
plt.xlabel('Percentage')
|
73 |
+
plt.title('Percentage of Snoring')
|
74 |
+
plt.xlim(0, 100)
|
75 |
+
|
76 |
+
for i, percentage in enumerate(percentages):
|
77 |
+
plt.text(percentage, i, f' {percentage:.2f}%', va='center')
|
78 |
+
st.write("DONE")
|
79 |
+
st.pyplot(plt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
|
81 |
|
82 |
|