-
Notifications
You must be signed in to change notification settings - Fork 1
/
querying_genre.py
executable file
·138 lines (110 loc) · 5.06 KB
/
querying_genre.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/local/bin/python3
import numpy as np
np.random.seed(1337) # for reproducibility
from keras.models import model_from_json, Sequential
import json
from extract_features import extract_features
from split_30_seconds import batch_thirty_seconds, thirty_seconds
import os
import re
import sys
from numpy import genfromtxt
from keras.preprocessing import sequence
def saveToFile(genreResult):
#if has another id save to file
if len(sys.argv) > 2:
id = sys.argv[2]
if not os.path.exists("results"):
os.makedirs("results")
f = open('results/'+id+".txt", 'w')
f.write(genreResult)
f.close()
if not os.path.exists("model_weights/super_awesome_merged_model_weights.hdf5"):
print("No model weights found in path 'model_weights/merged_model_weights.hdf5'")
else:
json_string = json.load(open("model_architecture/merged_model_architecture.json","r"))
model = model_from_json(json_string)
model.load_weights("model_weights/super_awesome_merged_model_weights.hdf5")
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy']
)
# Parse song
if len(sys.argv) < 2:
print("missing parameter")
else:
filename = sys.argv[1]
song_folder = os.path.dirname(os.path.realpath(filename))#should get the directory to the file
if os.path.isdir(filename):
batch_thirty_seconds(song_folder)
extract_features(song_folder)
else:
thirty_seconds(filename)
print("File split. Now extracting features.")
extract_features(song_folder)
print("Extracted features.")
keyword_2 = "mfcc_coefficients"
x2 = []
for root, dirs, files in os.walk(song_folder, topdown=False):
for name in files:
if re.search(keyword_2+".csv",name):
song_path = (os.path.join(root,name))
song_features = genfromtxt(song_path, delimiter=",")
if len(song_features.shape) is 2:
song_features = np.array([ _line[1:] for _line in song_features])
elif len(song_features.shape) is 1:
song_features = np.array([song_features[1:]])
x2.append(song_features)
mfcc_max_len = 0
with( open("maxlen_mfcc_coefficients","r") ) as _f:
mfcc_max_len = int(_f.read())
x2 = sequence.pad_sequences(x2, maxlen=mfcc_max_len,dtype='float32')
keyword_3 = "spectral-contrast_peaks"
x3 = []
for root, dirs, files in os.walk(song_folder, topdown=False):
for name in files:
if re.search(keyword_3+".csv",name):
song_path = (os.path.join(root,name))
song_features = genfromtxt(song_path, delimiter=",")
if len(song_features.shape) is 2:
song_features = np.array([ _line[1:] for _line in song_features])
elif len(song_features.shape) is 1:
song_features = np.array([song_features[1:]])
x3.append(song_features)
spectral_max_len = 0
with( open("maxlen_spectral-contrast_peaks","r") ) as _f:
spectral_max_len = int(_f.read())
x3 = sequence.pad_sequences(x3, maxlen=spectral_max_len,dtype='float32')
keyword_4 = "spectral-contrast_valleys"
x4 = []
for root, dirs, files in os.walk(song_folder, topdown=False):
for name in files:
if re.search(keyword_4+".csv",name):
song_path = (os.path.join(root,name))
song_features = genfromtxt(song_path, delimiter=",")
if len(song_features.shape) is 2:
song_features = np.array([ _line[1:] for _line in song_features])
elif len(song_features.shape) is 1:
song_features = np.array([song_features[1:]])
x4.append(song_features)
spectral_max_len = 0
with( open("maxlen_spectral-contrast_peaks","r") ) as _f:
spectral_max_len = int(_f.read())
x4 = sequence.pad_sequences(x4, maxlen=spectral_max_len,dtype='float32')
predictions = model.predict_classes([x2,x3,x4])
genredict = ["hiphop","pop", "rock"]
genredict.sort()#make sure that it is alphabetically sorted
#make a list of result strings
resultsstringified = []
for p in predictions:#p is digit
resultsstringified.append(genredict[p])
mode = max(set(resultsstringified), key=resultsstringified.count);
resultstring = ""
modeCounter=0
for p in resultsstringified:
if mode==p:
modeCounter+=1
resultstring += p+" "
print("Detected "+resultstring)
print("The song is "+str(modeCounter*100/len(resultsstringified))+" % "+mode)
saveToFile(resultstring)