This repository has been archived by the owner on Aug 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
79 lines (55 loc) · 1.56 KB
/
test.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""
Run Instruction:
python3 test.py ~/path/to/your/song/file
"""
import os
import sys
from sklearn.externals import joblib
import glob
import pathlib
import scipy
import scipy.io.wavfile
import numpy as np
import warnings
warnings.filterwarnings('ignore')
import librosa
X = []
y = []
genres = os.listdir('./gtzan')
test_file = sys.argv[1]
pathlib.Path(f'./testing{test_file[:-4]}').mkdir(parents=True, exist_ok=True)
os.system("ffmpeg -t 30 -i " + test_file + f' ./testing{test_file[:-4]}/song.wav')
sample_rate, song_array = scipy.io.wavfile.read(f'./testing{test_file[:-4]}/song.wav')
fft_features = abs(scipy.fft.fft(song_array[:10000]))
#np.save("testing.fft",fft_features)
#np.save(f'./test{test_file[:-4]}/song.fft', fft_features)
#fft_feature = np.load("testing.fft.npy")
#X,y = read_fft(genres,'./test')
for label,genre in enumerate(genres):
y.append(label)
X.append(fft_features)
X = np.array(X)
#print(X.ndim)
#print(X.shape)
#X = X.reshape((2,10000))
if X.ndim == 3:
X = X.reshape((X.shape[0]*X.shape[1]), X.shape[2])
X = X.transpose()
#print(X.ndim)
#print(X.shape)
#for CNN only
X = X.reshape((-1,100,100,1))
clf = joblib.load('./model.pkl')
probs = clf.predict_proba(X)
print("\t".join(str(x) for x in genres))
print("\t".join(str("%.3f" % x) for x in probs[0]))
print(probs)
probs=probs[0]
max_prob = max(probs)
for i,j in enumerate(probs):
if probs[i] == max_prob:
max_prob_index=i
print(max_prob_index)
predicted_genre = genres[max_prob_index]
print("\n\npredicted genre = ",predicted_genre)
os.system("rm -r "+ f'testing*')