AbdullaShafeeg commited on
Commit
ab9c217
·
1 Parent(s): 28cc6c6

big update

Browse files
Files changed (1) hide show
  1. app.py +47 -82
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
- upload_file = st.file_uploader("Upload wav file", type=["wav"])
36
- if upload_file is not None:
37
- file_details = {
38
- "Filename": upload_file.name,
39
- "Filesize":f"{upload_file.size / 1024:.2f} KB",
40
- "File Type": upload_file.type,
41
- }
42
- st.write("File Details:", file_details)
43
-
44
-
45
- # wav_audio_data = None
46
- # if wav_audio_data is not None:
47
- # data = np.frombuffer(wav_audio_data, dtype=np.int16)
48
- # st.write(len(data))
49
- # duration = len(data)//110000
50
- # num_of_samples = len(data)
51
- # sample_rate = num_of_samples // duration
52
- # # data = np.array(wav_audio_data, dtype=float)
53
- # max_abs_value = np.max(np.abs(data))
54
- # np_array = (data/max_abs_value) * 32767
55
- # scaled_data = np_array.astype(np.int16).tobytes()
56
- # with io.BytesIO() as fp, wave.open(fp, mode="wb") as waveobj:
57
- # waveobj.setnchannels(1)
58
- # waveobj.setframerate(96000)
59
- # waveobj.setsampwidth(2)
60
- # waveobj.setcomptype("NONE", "NONE")
61
- # waveobj.writeframes(scaled_data)
62
- # wav_make = fp.getvalue()
63
-
64
- # with open("output.wav", 'wb') as wav_file:
65
- # wav_file.write(wav_make)
66
- file_uploaded = False
67
- if file_uploaded == False:
68
- st.write("Uploading File.....")
69
- audio = pydub.AudioSegment.from_wav(upload_file)
70
- file_uploaded = True
71
- time.sleep(2)
72
- st.write("File Uploaded!")
73
-
74
- if file_uploaded == True:
75
- st.write("Analysing...")
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