-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredict.py
40 lines (29 loc) · 1.05 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
import numpy as np
import tensorflow
import librosa
import numpy as np
import pickle
# Change the path
model = tensorflow.keras.models.load_model('weights.best.basic_mlp.hdf5', compile=False)
def extract_feature(file_name):
try:
audio_data, sample_rate = librosa.load(file_name)
mfccs = librosa.feature.mfcc(y=audio_data, sr=sample_rate, n_mfcc=40)
mfccsscaled = np.mean(mfccs.T,axis=0)
except Exception as e:
print("Error encountered while parsing file: ")
return None, None
return np.array([mfccsscaled])
def print_prediction(file_name):
prediction_feature = extract_feature(file_name)
predicted_vector = model.predict(prediction_feature)
array=predicted_vector[0]
m=np.amax(array)
if(m==array[3]):
print("Accuracy:",array[3])
print("Dog Detected")
else:
print("Accuracy:",1-array[3])
print("No Dog Detected")
filename = input("Enter full path of audio(.wav) file: ")
print_prediction(filename)