-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
125 lines (113 loc) · 3.95 KB
/
app.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
import streamlit as st
from pydub import AudioSegment
import numpy as np
import helper
import librosa
import librosa.display as idp
import matplotlib.pyplot as plt
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
st.sidebar.title("UrbanSound8k Audio Classifier")
Uploaded_file = st.sidebar.file_uploader("Insert File", type=['.wav','.mp3'])
if Uploaded_file is not None:
# st.write(Uploaded_file)
# files
if Uploaded_file.name.endswith('.mp3'):
# convert wav to mp3
sound = AudioSegment.from_mp3(Uploaded_file)
sound.export("audio.wav", format="wav")
Uploaded_file = "audio.wav"
raw, sample_rate = helper.preprocess(Uploaded_file)
# Play Audio File
st.title("Play Uploaded File & Waveplot")
out = helper.play_audio(raw, sample_rate)
fig, ax = plt.subplots()
ax = idp.waveshow(raw, sr=sample_rate)
st.pyplot(fig)
st.audio(Uploaded_file, format='audio/wav')
# Printing the results
st.title("predicted Class")
prediction = helper.audio_to_result(Uploaded_file)
le.classes_ = np.load('classes.npy')
pred_class = le.inverse_transform(prediction)
class_ = "Predicted Class : "+ str(pred_class[0])
st.success(class_)
# STFT
st.title('spectrogram')
X_db = helper.Spectrogram(raw)
fig, ax = plt.subplots()
ax = idp.specshow(X_db, sr=sample_rate, x_axis="time", y_axis="hz")
plt.title("Input Audio's Spectrogram")
st.pyplot(fig)
# Harmonic-Percussive Separation (HPS)
st.title('Harmonic Percussive Separation')
h, p = helper.HPS(raw, sample_rate)
col1, col2 = st.columns(2)
with col1:
st.header("Harmonics")
fig, ax = plt.subplots()
ax = librosa.display.specshow(
h, y_axis='mel', x_axis='s', sr=sample_rate)
plt.title("Harmonic Mel Spectogram")
st.pyplot(fig)
with col2:
st.header("Percussion")
fig, ax = plt.subplots()
ax = librosa.display.specshow(
p, y_axis='mel', x_axis='s', sr=sample_rate)
plt.title("Percuisive Mel Spectogram")
st.pyplot(fig)
# MFCC
st.title('Mel-Frequency Cepstral Coefficients (MFCC)')
mfcc = helper.MFCC(raw, sample_rate)
fig, ax = plt.subplots()
ax = idp.specshow(mfcc, x_axis="s")
plt.title("Mel-Frequency Cepstral Coefficients")
st.pyplot(fig)
# Zero-Crossing
st.title('Zero Crossing Rate')
zero_crossing = helper.ZCR(raw)
fig, ax = plt.subplots()
ax.plot(raw[4700:5500])
plt.title("Zero Crossing Rate")
st.pyplot(fig)
st.write("Total Number of Zero Crossing is: ", str(sum(zero_crossing)))
# Spectral Centroid & Roll-Off
st.title('Spectral Centroid & Roll-Off')
C, R = helper.Spectral(raw, sample_rate)
col1, col2 = st.columns(2)
with col1:
fig, ax = plt.subplots()
ax = plt.semilogy(C.T, "r")
plt.ylabel("Hz")
plt.title("Spectral Centroid")
st.pyplot(fig)
with col2:
fig, ax = plt.subplots()
ax = plt.semilogy(R.T, "r")
plt.ylabel("Hz")
plt.title("Spectral Roll-Off")
st.pyplot(fig)
# Chroma Feature
st.title('Chroma Feature')
chroma = helper.Chroma(raw, sample_rate)
fig, ax = plt.subplots()
ax = librosa.display.specshow(chroma, y_axis='chroma', x_axis='time')
plt.colorbar()
plt.title("Chromagram")
st.pyplot(fig)
# RMSE & log power spectrogram
st.title('RMSE & log power spectrogram')
S, RMSEn, times = helper.RMSE(raw)
col1, col2 = st.columns(2)
with col1:
fig, ax = plt.subplots()
ax = plt.semilogy(times, RMSEn[0])
plt.title("Root Mean Squared Energy")
st.pyplot(fig)
with col2:
fig, ax = plt.subplots()
ax = librosa.display.specshow(librosa.amplitude_to_db(
S, ref=np.max), y_axis='log', x_axis='time')
plt.title("log Power Spectrogram")
st.pyplot(fig)