-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
645 lines (597 loc) · 24.7 KB
/
app.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
from flask import Flask,request, url_for, redirect, render_template
import pickle
import numpy as np
import pandas as pd
from bisect import bisect_left, bisect_right
import sklearn
import os
import scipy
import math
import librosa
from pydub import AudioSegment
from scipy.io import wavfile
from sklearn.decomposition import PCA
from sklearn.feature_selection import RFE
import xgboost as xgb
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from werkzeug.utils import secure_filename
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler
from matplotlib import pyplot as plt
app = Flask(__name__)
# model=pickle.load(open('model.pkl','rb'))
def cancel_zeros(str_array):
ret_arr = []
for i in range(0, len(str_array)):
ret_arr.append(str(int(str_array[i])))
return ret_arr
def get_usr_pref(temp_arr, all_feature_array):
usr_pref_array = []
for i in range(0,len(temp_arr)):
for j in range(0, len(all_feature_array)):
if (temp_arr[i] == str(all_feature_array[j][0])):
usr_pref_array.append(all_feature_array[j])
return usr_pref_array
def get_recommendation(usr_pref_array, all_feature_array):
print("USR", usr_pref_array)
X = np.array(usr_pref_array)
print("X=", X)
X = X[:, 1:4].astype(float)
X = np.array(X)
RADIUS_THRESH = 2
max_radius = RADIUS_THRESH + 1
n_clusters = 0
# Increase the number of clusters till the largest cluster radius < RADIUS_THRESH
while (max_radius > RADIUS_THRESH):
n_clusters = n_clusters + 1
# Use the k-means classifier
kmeans = KMeans(n_clusters, random_state=0).fit(X)
max_radius = 0
# Compute the max_radius
for i in range(0, len(X)):
centre_no = kmeans.labels_[i]
centre_coor = kmeans.cluster_centers_[centre_no]
dist = (X[i][0]-centre_coor[0])**2 + (X[i][1]-centre_coor[1])**2 + (X[i][2]-centre_coor[2])**2
dist = math.sqrt(dist) # Euclidean distance between data-point in X and its cluster centre
if dist > max_radius:
max_radius = dist
print("Final number of clusters formed are: ", n_clusters)
print("Cluster allocation is: ", kmeans.labels_, "respectively")
BOX_SIZE_FACTOR = 50 # Length of side of sq = factor*RADIUS_THRESH
start_index = []
end_index = []
for i in range(0, n_clusters):
x_min = kmeans.cluster_centers_[i][0]-((BOX_SIZE_FACTOR/2)*RADIUS_THRESH)
x_max = kmeans.cluster_centers_[i][0]+((BOX_SIZE_FACTOR/2)*RADIUS_THRESH)
start_index.append(bisect_left(all_feature_array[:,1].astype(float), x_min))
end_index.append(bisect_right(all_feature_array[:,1].astype(float), x_max))
print(start_index)
print(end_index)
accepted_song = []
for i in range(0, n_clusters):
y_min = kmeans.cluster_centers_[i][1]-((BOX_SIZE_FACTOR/2)*RADIUS_THRESH)
y_max = kmeans.cluster_centers_[i][1]+((BOX_SIZE_FACTOR/2)*RADIUS_THRESH)
z_min = kmeans.cluster_centers_[i][2]-((BOX_SIZE_FACTOR/2)*RADIUS_THRESH)
z_max = kmeans.cluster_centers_[i][2]+((BOX_SIZE_FACTOR/2)*RADIUS_THRESH)
for j in range(start_index[i], end_index[i]):
if(all_feature_array[j][2]<= y_max and all_feature_array[j][2] >= y_min):
if(all_feature_array[j][3] <= z_max and all_feature_array[j][3]>= z_min):
accepted_song.append(all_feature_array[j])
new_recommendation = []
for i in range(0, len(accepted_song)):
usr_pref = False
for j in range(0, len(usr_pref_array)):
if(accepted_song[i][0]==usr_pref_array[j][0]):
usr_pref = True
if (usr_pref == False):
new_recommendation.append(accepted_song[i])
print("No of new recommendations found=", len(new_recommendation))
score = []
new_recommendation_with_score = []
for i in range(0, len(new_recommendation)):
score.append(0)
for j in range(0, n_clusters):
centre_coor = kmeans.cluster_centers_[j]
dist = (new_recommendation[i][1]-centre_coor[0])**2 + (new_recommendation[i][2]-centre_coor[1])**2 + (new_recommendation[i][3]-centre_coor[2])**2
dist = math.sqrt(dist) # Euclidean distance between song in new_recommendation and its cluster centre
temp = kmeans.labels_.tolist()
n_cluster_points = temp.count(j)
score[i] = score[i] + (1/(dist+0.000001))*(n_cluster_points/len(usr_pref_array))
new_recommendation_with_score.append([new_recommendation[i][0], score[i]])
new_recommendation_with_score = sorted(new_recommendation_with_score, key=lambda a:a[1], reverse=True)
print("Your top recommendations are:")
recommended_songs = []
for i in range(0, len(new_recommendation_with_score)):
print(i+1, ") Song Name.", new_recommendation_with_score[i][0])
recommended_songs.append(str(i+1) + ") Song Name: " + str(new_recommendation_with_score[i][0]) + "_____________" +new_recommendation[i][4])
return recommended_songs
@app.route('/get_recommendation/predict',methods=['POST','GET'])
def predict_recommendation():
df = pd.read_csv('k_mean_feat.csv')
all_feature_array = df.to_numpy()
print(all_feature_array)
temp = str([(x) for x in request.form.values()][0])
temp_arr = cancel_zeros((temp.split(",")))
usr_pref_array = get_usr_pref(temp_arr, all_feature_array)
recommended_songs = get_recommendation(usr_pref_array, all_feature_array)
print(temp)
return render_template('result.html', ans1 = '{}'.format(recommended_songs[0]),
ans2 = '{}'.format(recommended_songs[1]),
ans3 = '{}'.format(recommended_songs[2]),
ans4 = '{}'.format(recommended_songs[3]),
ans5 = '{}'.format(recommended_songs[4]),
ans6 = '{}'.format(recommended_songs[5]),
ans7 = '{}'.format(recommended_songs[6]),
ans8 = '{}'.format(recommended_songs[7]),
ans9 = '{}'.format(recommended_songs[8]),
ans10 = '{}'.format(recommended_songs[9]))
# int_features=[int(x) for x in request.form.values()]
# final=[np.array(int_features)]
# print(int_features)
# print(final)
# prediction=model.predict_proba(final)
# output='{0:.{1}f}'.format(prediction[0][1], 2)
# if output>str(0.5):
# return render_template('forest_fire.html',pred='Your Forest is in Danger.\nProbability of fire occuring is {}'.format(output),bhai="kuch karna hain iska ab?")
# else:
# return render_template('forest_fire.html',pred='Your Forest is safe.\n Probability of fire occuring is {}'.format(output),bhai="Your Forest is Safe for now")
def spectral_flux(music_wave_data):
# obtain the stft of the music_wave_data
spectrum = librosa.core.stft(music_wave_data)
N = spectrum.shape[0]
# calculating the spectral flux
sf = np.sqrt(np.sum((np.diff(np.abs(spectrum)))**2, axis=0)) / N
return sf
def feature_extraction(audio_data):
feature_list_all = []
for i in range(0, len(audio_data)):
feature_list = [audio_data[i][2]]
y = audio_data[i][0]
sr = audio_data[i][1]
feature_list.append(np.mean(abs(y)))
feature_list.append(np.std(y))
feature_list.append(scipy.stats.skew(abs(y)))
feature_list.append(scipy.stats.kurtosis(y))
# ZCR
zcr = librosa.feature.zero_crossing_rate(y + 0.0001, frame_length=2048, hop_length=512)[0]
feature_list.append(np.mean(zcr))
feature_list.append(np.std(zcr))
# RMSE
rmse = librosa.feature.rmse(y + 0.0001)[0]
feature_list.append(np.mean(rmse))
feature_list.append(np.std(rmse))
# Tempo
tempo = librosa.beat.tempo(y, sr=sr)
feature_list.extend(tempo)
# Spectral Centroids
spectral_centroids = librosa.feature.spectral_centroid(y+0.01, sr=sr)[0]
feature_list.append(np.mean(spectral_centroids))
feature_list.append(np.std(spectral_centroids))
# Spectral Bandwidth
spectral_bandwidth_2 = librosa.feature.spectral_bandwidth(y+0.01, sr=sr, p=2)[0]
spectral_bandwidth_3 = librosa.feature.spectral_bandwidth(y+0.01, sr=sr, p=3)[0]
spectral_bandwidth_4 = librosa.feature.spectral_bandwidth(y+0.01, sr=sr, p=4)[0]
feature_list.append(np.mean(spectral_bandwidth_2))
feature_list.append(np.std(spectral_bandwidth_2))
feature_list.append(np.mean(spectral_bandwidth_3))
feature_list.append(np.std(spectral_bandwidth_3))
feature_list.append(np.mean(spectral_bandwidth_3))
feature_list.append(np.std(spectral_bandwidth_3))
# Spectral Flux
sf = spectral_flux(y)
sf_num = np.mean(sf)
feature_list.append(sf_num)
# Spectral Contrast
spectral_contrast = librosa.feature.spectral_contrast(y, sr=sr, n_bands = 6, fmin = 200.0)
feature_list.extend(np.mean(spectral_contrast, axis=1))
feature_list.extend(np.std(spectral_contrast, axis=1))
# Spectral Rolloff
spectral_rolloff = librosa.feature.spectral_rolloff(y+0.01, sr=sr, roll_percent = 0.85)[0]
feature_list.append(np.mean(spectral_rolloff))
feature_list.append(np.std(spectral_rolloff))
# MFCC
mfccs = librosa.feature.mfcc(y, sr=sr, n_mfcc=20)
feature_list.extend(np.mean(mfccs, axis=1))
feature_list.extend(np.std(mfccs, axis=1))
# STFT
chroma_stft = librosa.feature.chroma_stft(y, sr=sr, hop_length=1024)
feature_list.extend(np.mean(chroma_stft, axis=1))
feature_list.extend(np.std(chroma_stft, axis=1))
# Round off
feature_list[1:] = np.round(feature_list[1:], decimals=3)
feature_list_all.append(feature_list)
return feature_list_all
def pca_extraction(X_data):
X_data = StandardScaler().fit_transform(X_data)
pca = pickle.load(open("models/pca.pkl",'rb'))
principalComponents = pca.transform(X_data)
return principalComponents
def load_model(filename):
path = "models"
path = os.path.join(path,filename)
loaded_model = pickle.load(open(path,"rb"))
return loaded_model
def save_song(song):
song.save(os.path.join("./songs/", str(song.filename)))
def load_song():
audio_data = []
path = "songs"
count = 0
audio_data = []
for r, d, f in os.walk(path):
for file in f:
if file.endswith('.mp3'):
filepath = str(r)+ '/' + str(file)
count += 1
file_name = str(r)+ '/' + str(count) + ".wav"
print(file_name)
sound = AudioSegment.from_mp3(filepath)
sound.export(file_name, format="wav")
fs, data = wavfile.read(file_name)
print (data)
if (data.shape[1] == 2):
data = data[:,0]
data = data.astype(float)
audio_data.append([data, fs, filepath])
# for r, d, f in os.walk(path):
# for file in f:
# if file.endswith('.mp3'):
# filepath = str(r)+ '/' + str(file)
# print(filepath)
# # try:
# y, sr = librosa.load(filepath, sr = 22050)
# audio_data.append([y, sr, filepath])
# # except:
# # continue
return audio_data
def get_genre(k):
thisdict = {
0: "International",
1: "Instrumental",
2: "Pop",
3: "Folk",
4: "Hip-Hop",
5: "Experimental",
6: "Rock",
7: "Electronic"
}
x = thisdict.get(k)
return x
@app.route('/')
def home():
return render_template("home.html")
@app.route('/get_recommendation', methods=['POST','GET'])
def get_recommendation():
return render_template("get_recommendation.html")
@app.route('/genre_prediction', methods=['POST','GET'])
def genre_prediction():
if request.method == 'POST':
f = request.files['file']
print(f)
save_song(f)
return render_template("genre_prediction.html")
@app.route('/rf', methods=['POST','GET'])
def rf():
audio_songs = load_song()
if (audio_songs != []):
feature__list_all = feature_extraction(audio_songs)
loaded_model = load_model("rf.pkl")
feature__list_all = np.array(feature__list_all)
feature__list_all.reshape(-1,1)
print(feature__list_all)
pred_probs = loaded_model.predict_proba(feature__list_all[:,1:])
pred_probs = np.array(pred_probs[0])
result = np.where(pred_probs == np.amax(pred_probs))
print(int(result[0]))
genre = get_genre(int(result[0]))
print("G=",genre)
return render_template('output.html', genre = '{}'.format(genre))
else:
return "Could not open the song"
#give output
@app.route('/rf_pca', methods=['POST','GET'])
def rf_pca():
audio_songs = load_song()
if (audio_songs != []):
feature__list_all = feature_extraction(audio_songs)
loaded_model = load_model("rf_pca.pkl")
feature__list_all = np.array(feature__list_all)
feature__list_all = pca_extraction(feature__list_all[:,1:])
feature__list_all.reshape(-1,1)
print(feature__list_all)
pred_probs = loaded_model.predict_proba(feature__list_all)
print (pred_probs)
pred_probs = np.array(pred_probs[0])
result = np.where(pred_probs == np.amax(pred_probs))
print(int(result[0]))
genre = get_genre(int(result[0]))
print("G=",genre)
return render_template('output.html', genre = '{}'.format(genre))
else:
return "Could not open the song"
#give output
@app.route('/rf_rfe', methods=['POST','GET'])
def rf_rfe():
audio_songs = load_song()
if (audio_songs != []):
feature__list_all = feature_extraction(audio_songs)
loaded_model = load_model("rf_rfe.pkl")
feature__list_all = np.array(feature__list_all)
feature__list_all.reshape(-1,1)
print(feature__list_all)
pred_probs = loaded_model.predict_proba(feature__list_all[:,1:])
print (pred_probs)
pred_probs = np.array(pred_probs[0])
result = np.where(pred_probs == np.amax(pred_probs))
print(int(result[0]))
genre = get_genre(int(result[0]))
print("G=",genre)
return render_template('output.html', genre = '{}'.format(genre))
else:
return "Could not open the song"
#give output
@app.route('/rf_pca_rfe', methods=['POST','GET'])
def rf_pca_rfe():
audio_songs = load_song()
if (audio_songs != []):
feature__list_all = feature_extraction(audio_songs)
loaded_model = load_model("rf_pca_rfe.pkl")
feature__list_all = np.array(feature__list_all)
feature__list_all = pca_extraction(feature__list_all[:,1:])
feature__list_all.reshape(-1,1)
print(feature__list_all)
pred_probs = loaded_model.predict_proba(feature__list_all)
print (pred_probs)
pred_probs = np.array(pred_probs[0])
result = np.where(pred_probs == np.amax(pred_probs))
print(int(result[0]))
genre = get_genre(int(result[0]))
print("G=",genre)
return render_template('output.html', genre = '{}'.format(genre))
else:
return "Could not open the song"
#give output
@app.route('/xgb', methods=['POST','GET'])
def xgb():
audio_songs = load_song()
if (audio_songs != []):
feature__list_all = feature_extraction(audio_songs)
loaded_model = load_model("xgb.pkl")
feature__list_all = np.array(feature__list_all)
feature__list_all.reshape(-1,1)
print(feature__list_all)
pred_probs = loaded_model.predict_proba(feature__list_all[:,1:])
print (pred_probs)
pred_probs = np.array(pred_probs[0])
result = np.where(pred_probs == np.amax(pred_probs))
print(int(result[0]))
genre = get_genre(int(result[0]))
print("G=",genre)
return render_template('output.html', genre = '{}'.format(genre))
else:
return "Could not open the song"
#give output
@app.route('/xgb_pca', methods=['POST','GET'])
def xgb_pca():
audio_songs = load_song()
if (audio_songs != []):
feature__list_all = feature_extraction(audio_songs)
loaded_model = load_model("xgb_pca.pkl")
feature__list_all = np.array(feature__list_all)
feature__list_all = pca_extraction(feature__list_all)
feature__list_all.reshape(-1,1)
print(feature__list_all)
pred_probs = loaded_model.predict_proba(feature__list_all)
print (pred_probs)
pred_probs = np.array(pred_probs[0])
result = np.where(pred_probs == np.amax(pred_probs))
print(int(result[0]))
genre = get_genre(int(result[0]))
print("G=",genre)
return render_template('output.html', genre = '{}'.format(genre))
else:
return "Could not open the song"
#give output
@app.route('/xgb_rfe', methods=['POST','GET'])
def xgb_rfe():
audio_songs = load_song()
if (audio_songs != []):
feature__list_all = feature_extraction(audio_songs)
loaded_model = load_model("xgb_rfe.pkl")
feature__list_all = np.array(feature__list_all)
feature__list_all.reshape(-1,1)
print(feature__list_all)
pred_probs = loaded_model.predict_proba(feature__list_all[:,1:])
print (pred_probs)
pred_probs = np.array(pred_probs[0])
result = np.where(pred_probs == np.amax(pred_probs))
print(int(result[0]))
genre = get_genre(int(result[0]))
print("G=",genre)
return render_template('output.html', genre = '{}'.format(genre))
else:
return "Could not open the song"
#give output
@app.route('/xgb_pca_rfe', methods=['POST','GET'])
def xgb_pca_rfe():
audio_songs = load_song()
if (audio_songs != []):
feature__list_all = feature_extraction(audio_songs)
loaded_model = load_model("xgb_pca_rfe.pkl")
feature__list_all = np.array(feature__list_all)
feature__list_all = pca_extraction(feature__list_all)
feature__list_all.reshape(-1,1)
print(feature__list_all)
pred_probs = loaded_model.predict_proba(feature__list_all[:,1:])
print (pred_probs)
else:
return "Could not open the song"
#give output
@app.route('/svm', methods=['POST','GET'])
def svm():
audio_songs = load_song()
if (audio_songs != []):
feature__list_all = feature_extraction(audio_songs)
loaded_model = load_model("svm.pkl")
feature__list_all = np.array(feature__list_all)
feature__list_all.reshape(-1,1)
print(feature__list_all)
pred_probs = loaded_model.predict_proba(feature__list_all[:,1:])
print (pred_probs)
pred_probs = np.array(pred_probs[0])
result = np.where(pred_probs == np.amax(pred_probs))
print(int(result[0]))
genre = get_genre(int(result[0]))
print("G=",genre)
return render_template('output.html', genre = '{}'.format(genre))
else:
return "Could not open the song"
#give output
@app.route('/svm_pca', methods=['POST','GET'])
def svm_pca():
audio_songs = load_song()
if (audio_songs != []):
feature__list_all = feature_extraction(audio_songs)
loaded_model = load_model("svm_pca.pkl")
feature__list_all = np.array(feature__list_all)
feature__list_all = pca_extraction(feature__list_all[:,1:])
feature__list_all.reshape(-1,1)
print(feature__list_all)
pred_probs = loaded_model.predict_proba(feature__list_all)
print (pred_probs)
pred_probs = np.array(pred_probs[0])
result = np.where(pred_probs == np.amax(pred_probs))
print(int(result[0]))
genre = get_genre(int(result[0]))
print("G=",genre)
return render_template('output.html', genre = '{}'.format(genre))
else:
return "Could not open the song"
#give output
@app.route('/nn', methods=['POST','GET'])
def nn():
audio_songs = load_song()
if (audio_songs != []):
feature__list_all = feature_extraction(audio_songs)
loaded_model = load_model("nn.pickle")
feature__list_all = np.array(feature__list_all)
feature__list_all.reshape(-1,1)
print(feature__list_all)
pred_probs = loaded_model.predict_proba(feature__list_all[:,1:])
print (pred_probs)
pred_probs = np.array(pred_probs[0])
result = np.where(pred_probs == np.amax(pred_probs))
print(int(result[0]))
genre = get_genre(int(result[0]))
print("G=",genre)
return render_template('output.html', genre = '{}'.format(genre))
else:
return "Could not open the song"
#give output
@app.route('/nn_pca', methods=['POST','GET'])
def nn_pca():
audio_songs = load_song()
if (audio_songs != []):
feature__list_all = feature_extraction(audio_songs)
loaded_model = load_model("nn_pca.pickle")
feature__list_all = np.array(feature__list_all)
feature__list_all = pca_extraction(feature__list_all)
feature__list_all.reshape(-1,1)
print(feature__list_all)
pred_probs = loaded_model.predict_proba(feature__list_all[:,1:])
print (pred_probs)
pred_probs = np.array(pred_probs[0])
result = np.where(pred_probs == np.amax(pred_probs))
print(int(result[0]))
genre = get_genre(int(result[0]))
print("G=",genre)
return render_template('output.html', genre = '{}'.format(genre))
else:
return "Could not open the song"
#give output
@app.route('/cnn', methods=['POST','GET'])
def cnn():
audio_songs = load_song()
if (audio_songs != []):
feature__list_all = feature_extraction(audio_songs)
loaded_model = load_model("cnn.pkl")
feature__list_all = np.array(feature__list_all)
feature__list_all.reshape(-1,1)
print(feature__list_all)
pred_probs = loaded_model.predict_proba(feature__list_all[:,1:])
print (pred_probs)
pred_probs = np.array(pred_probs[0])
result = np.where(pred_probs == np.amax(pred_probs))
print(int(result[0]))
genre = get_genre(int(result[0]))
print("G=",genre)
return render_template('output.html', genre = '{}'.format(genre))
else:
return "Could not open the song"
#give output
@app.route('/cnn_pca', methods=['POST','GET'])
def cnn_pca():
audio_songs = load_song()
if (audio_songs != []):
feature__list_all = feature_extraction(audio_songs)
loaded_model = load_model("cnn_pca.pkl")
feature__list_all = np.array(feature__list_all)
feature__list_all = pca_extraction(feature__list_all)
feature__list_all.reshape(-1,1)
print(feature__list_all)
pred_probs = loaded_model.predict_proba(feature__list_all[:,1:])
print (pred_probs)
pred_probs = np.array(pred_probs[0])
result = np.where(pred_probs == np.amax(pred_probs))
print(int(result[0]))
genre = get_genre(int(result[0]))
print("G=",genre)
return render_template('output.html', genre = '{}'.format(genre))
else:
return "Could not open the song"
@app.route('/en', methods=['POST','GET'])
def en():
audio_songs = load_song()
if (audio_songs != []):
feature__list_all = feature_extraction(audio_songs)
loaded_model = load_model("cnn.pkl")
feature__list_all = np.array(feature__list_all)
feature__list_all.reshape(-1,1)
print(feature__list_all)
pred_probs = loaded_model.predict_proba(feature__list_all[:,1:])
print (pred_probs)
pred_probs = np.array(pred_probs[0])
result = np.where(pred_probs == np.amax(pred_probs))
print(int(result[0]))
genre = get_genre(int(result[0]))
print("G=",genre)
return render_template('output.html', genre = '{}'.format(genre))
else:
return "Could not open the song"
#give output
@app.route('/en_pca', methods=['POST','GET'])
def en_pca():
audio_songs = load_song()
if (audio_songs != []):
feature__list_all = feature_extraction(audio_songs)
loaded_model = load_model("en_pca.pkl")
feature__list_all = np.array(feature__list_all)
feature__list_all = pca_extraction(feature__list_all)
feature__list_all.reshape(-1,1)
print(feature__list_all)
pred_probs = loaded_model.predict_proba(feature__list_all[:,1:])
print (pred_probs)
pred_probs = np.array(pred_probs[0])
result = np.where(pred_probs == np.amax(pred_probs))
print(int(result[0]))
genre = get_genre(int(result[0]))
print("G=",genre)
return render_template('output.html', genre = '{}'.format(genre))
else:
return "Could not open the song"
@app.route('/upload', methods=['POST','GET'])
def upload():
return render_template('upload.html')
if __name__ == '__main__':
app.run(debug=True)