-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio_recognition.py
61 lines (52 loc) · 1.84 KB
/
audio_recognition.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
import os
import sounddevice
import numpy as np
import librosa.display
import matplotlib.pyplot as plt
from keras.models import model_from_json
duration = 3
sample_rate=48000
def get_sound():
audio = sounddevice.rec(int(duration * sample_rate), samplerate=sample_rate, channels=1)
audio = np.squeeze(audio)
sr=sample_rate
sounddevice.wait()
S = librosa.feature.melspectrogram(audio, sr=sr, n_mels=128)
log_S = librosa.power_to_db(S, ref=np.max)
fig = plt.figure(figsize=[1, 1])
ax = fig.add_subplot(111)
fig.subplots_adjust(left=0,right=1,bottom=0,top=1)
ax.axis("off")
ax.axis("tight")
plt.margins(0)
librosa.display.specshow(log_S, sr=sr)
fig.savefig('rec.png', dpi=100, pad_inches=0)
plt.close(fig)
plt.close('all')
del audio, S, log_S, ax, fig
def extract_spectrogram(fname, iname):
audio, sr = librosa.load(fname, res_type='kaiser_fast')
S = librosa.feature.melspectrogram(audio, sr=sr, n_mels=128)
log_S = librosa.power_to_db(S, ref=np.max)
fig = plt.figure(figsize=[1, 1])
ax = fig.add_subplot(111)
fig.subplots_adjust(left=0,right=1,bottom=0,top=1)
ax.axis("off")
ax.axis("tight")
plt.margins(0)
librosa.display.specshow(log_S, sr=sr)
fig.savefig(iname, dpi=100, pad_inches=0)
plt.close(fig)
plt.close('all')
del audio, S, log_S, ax, fig
class AudioModel(object):
def __init__(self, model_json_file, model_weights_file):
# load model from JSON file
with open(model_json_file, "r") as json_file:
loaded_model_json = json_file.read()
self.loaded_model = model_from_json(loaded_model_json)
self.loaded_model.load_weights(model_weights_file)
self.loaded_model._make_predict_function()
def predict(self, img):
self.preds = self.loaded_model.predict(img)
return self.preds