Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -9,7 +9,15 @@ import tempfile
|
|
9 |
learn = load_learner('model.pkl')
|
10 |
labels = learn.dls.vocab
|
11 |
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
if audio_file.endswith('.mp3'):
|
14 |
with tempfile.NamedTemporaryFile(suffix='.wav') as temp_wav:
|
15 |
audio = AudioSegment.from_mp3(audio_file)
|
@@ -29,25 +37,6 @@ def audio_to_spectrogram(audio_file, sr):
|
|
29 |
plt.close()
|
30 |
return spectrogram_file
|
31 |
|
32 |
-
def record_audio(duration=3, sr=44100, channels=1):
|
33 |
-
print("Recording...")
|
34 |
-
audio = sd.rec(int(duration * sr), samplerate=sr, channels=channels, dtype='float32')
|
35 |
-
sd.wait()
|
36 |
-
print("Recording stopped.")
|
37 |
-
return audio, sr
|
38 |
-
|
39 |
-
def audio_to_spectrogram(audio, sr):
|
40 |
-
S = librosa.feature.melspectrogram(y=audio[:, 0], sr=sr, n_mels=128, fmax=8000)
|
41 |
-
S_dB = librosa.power_to_db(S, ref=np.max)
|
42 |
-
fig, ax = plt.subplots()
|
43 |
-
img = librosa.display.specshow(S_dB, x_axis='time', y_axis='mel', sr=sr, fmax=8000, ax=ax)
|
44 |
-
fig.colorbar(img, ax=ax, format='%+2.0f dB')
|
45 |
-
ax.set(title='Mel-frequency spectrogram')
|
46 |
-
spectrogram_file = "spectrogram.png"
|
47 |
-
plt.savefig(spectrogram_file)
|
48 |
-
plt.close()
|
49 |
-
return spectrogram_file
|
50 |
-
|
51 |
def predict(audio):
|
52 |
spectrogram_file = audio_to_spectrogram(audio)
|
53 |
img = PILImage.create(spectrogram_file)
|
|
|
9 |
learn = load_learner('model.pkl')
|
10 |
labels = learn.dls.vocab
|
11 |
|
12 |
+
|
13 |
+
def record_audio(duration=3, sr=44100, channels=1):
|
14 |
+
print("Recording...")
|
15 |
+
audio = sd.rec(int(duration * sr), samplerate=sr, channels=channels, dtype='float32')
|
16 |
+
sd.wait()
|
17 |
+
print("Recording stopped.")
|
18 |
+
return audio, sr
|
19 |
+
|
20 |
+
def audio_to_spectrogram(audio_file,):
|
21 |
if audio_file.endswith('.mp3'):
|
22 |
with tempfile.NamedTemporaryFile(suffix='.wav') as temp_wav:
|
23 |
audio = AudioSegment.from_mp3(audio_file)
|
|
|
37 |
plt.close()
|
38 |
return spectrogram_file
|
39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
def predict(audio):
|
41 |
spectrogram_file = audio_to_spectrogram(audio)
|
42 |
img = PILImage.create(spectrogram_file)
|