-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpredict.py
52 lines (34 loc) · 1.27 KB
/
predict.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
import os
import pickle
import numpy as np
from tqdm import tqdm
from scipy.io import wavfile
from python_speech_features import mfcc
from tensorflow.keras.models import load_model
import pandas as pd
from sklearn.metrics import accuracy_score
def build_predictions(audio_dir):
# y_pred = []
print('Extracting features from the data')
for fn in tqdm(os.listdir(audio_dir)):
rate, wav = wavfile.read(os.path.join(audio_dir, fn))
for i in range(0, wav.shape[0]-config.step, config.step):
sample = wav[i:i+config.step]
x = mfcc(sample, rate, numcep=config.nfeat,
nfilt=config.nfilt, nfft=config.nfft)
x = (x - config.min) / (config.max - config.min)
if config.mode == 'conv':
x = x.reshape(1, x.shape[0], x.shape[1], 1)
elif config.mode == 'rec':
x = np.expand_dims(x, axis=0)
# if x.shape != (19 , 13 , 1):
# continue
y_hat = model.predict(x)
y_pred = np.argmax(y_hat)
return y_pred
p_path = os.path.join('pickles', 'rec.p')
with open(p_path, 'rb') as handle:
config = pickle.load(handle)
model = load_model(config.model_path)
# y_predd = build_predictions('testdir')
# print(y_predd)