-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_svm.py
More file actions
36 lines (29 loc) · 1.46 KB
/
Copy pathtest_svm.py
File metadata and controls
36 lines (29 loc) · 1.46 KB
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
import joblib
import numpy as np
import librosa
from manage_models_v2 import ml_model_extract_features, convert_wav_audio
def ml_model_extract_features_trimmed(file_path):
y, sr = librosa.load(file_path, sr=None)
# Trim silence
y, _ = librosa.effects.trim(y, top_db=30)
mfcc = np.mean(librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13).T, axis=0)
chroma = np.mean(librosa.feature.chroma_stft(y=y, sr=sr).T, axis=0)
contrast = np.mean(librosa.feature.spectral_contrast(y=y, sr=sr).T, axis=0)
zcr = np.mean(librosa.feature.zero_crossing_rate(y).T, axis=0)
rms = np.mean(librosa.feature.rms(y=y).T, axis=0)
return np.hstack([mfcc, chroma, contrast, zcr, rms])
model_path = 'SVM/svm_model.joblib'
svm = joblib.load(model_path)
le = joblib.load('SVM/label_encoder.joblib')
file_path = 'test/youre-funny-1.wav'
_, _, norm_path = convert_wav_audio(file_path)
features = ml_model_extract_features_trimmed(norm_path)
print('youre-funny-1 Trimmed SVM:', le.inverse_transform(svm.predict([features])))
file_path = 'test/open-the-door.wav'
_, _, norm_path = convert_wav_audio(file_path)
features = ml_model_extract_features_trimmed(norm_path)
print('open-the-door Trimmed SVM:', le.inverse_transform(svm.predict([features])))
file_path = 'test/thats-the-loveliest-thing.wav'
_, _, norm_path = convert_wav_audio(file_path)
features = ml_model_extract_features_trimmed(norm_path)
print('loveliest-thing Trimmed SVM:', le.inverse_transform(svm.predict([features])))