Add mel spec visulaization
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ from transformers import AutoFeatureExtractor, AutoModelForAudioClassification
|
|
| 6 |
import logging
|
| 7 |
import sys
|
| 8 |
import librosa
|
|
|
|
| 9 |
import os
|
| 10 |
|
| 11 |
logging.basicConfig(
|
|
@@ -83,10 +84,26 @@ with gr.Blocks(title="AST Model") as demo:
|
|
| 83 |
def wrapper(audio_path):
|
| 84 |
waveform, probs, id2label = predict_audio(audio_path)
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
label_dict = {
|
| 91 |
id2label[i]: float(probs[i])
|
| 92 |
for i in range(len(probs))
|
|
|
|
| 6 |
import logging
|
| 7 |
import sys
|
| 8 |
import librosa
|
| 9 |
+
import librosa.display
|
| 10 |
import os
|
| 11 |
|
| 12 |
logging.basicConfig(
|
|
|
|
| 84 |
def wrapper(audio_path):
|
| 85 |
waveform, probs, id2label = predict_audio(audio_path)
|
| 86 |
|
| 87 |
+
mel_spec = librosa.feature.melspectrogram(
|
| 88 |
+
y = waveform, sr = AST_SR, n_mels = 128
|
| 89 |
+
)
|
| 90 |
|
| 91 |
+
mel_db = librosa.power_to_db(mel_spec, ref=np.max)
|
| 92 |
+
|
| 93 |
+
fig, ax = plt.subplots(1, 2, figsize=(10, 3))
|
| 94 |
+
ax[0].plot(waveform)
|
| 95 |
+
ax[0].set_title("Waveform")
|
| 96 |
+
|
| 97 |
+
# mel spectrogram
|
| 98 |
+
img = librosa.display.specshow(
|
| 99 |
+
mel_db, sr = AST_SR,
|
| 100 |
+
x_axis = 'time', y_axis = 'mel',
|
| 101 |
+
ax = ax[1]
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
ax[1].set_title("Mel Spectrogram")
|
| 105 |
+
fig.colorbar(img, ax=ax[1], format="%+2.0f dB")
|
| 106 |
+
|
| 107 |
label_dict = {
|
| 108 |
id2label[i]: float(probs[i])
|
| 109 |
for i in range(len(probs))
|