-
Notifications
You must be signed in to change notification settings - Fork 13
/
music-genre-recognition-app.py
318 lines (232 loc) · 9.47 KB
/
music-genre-recognition-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
import streamlit as st
from keras import layers
from keras.layers import (Input, Add, Dense, Activation, ZeroPadding2D, BatchNormalization,
Flatten, Conv2D, AveragePooling2D, MaxPooling2D, GlobalMaxPooling2D,
Dropout)
from keras.models import Model, load_model
import matplotlib.pyplot as plt
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from keras.initializers import glorot_uniform
from keras.preprocessing.image import ImageDataGenerator
from keras.preprocessing.image import load_img,img_to_array
from bing_image_downloader import downloader
from streamlit import caching
from unsplash_search import UnsplashSearch
unsplash = UnsplashSearch("C5OCp7HRCjNi9nr72kUXBpsY46mAPJizBcOrBEpA9EI")
st.write(""" # Music Genre Recognition
App """)
st.write("Made By Kunal Vaidya")
st.write("**This is a Web App to predict Genre of Music.**")
st.write("On the backend of this Web App a Convolutional Neural Network Model is used.The Model was trained on Mel Spectrogram of Music Files in the GTZAN Dataset.")
st.markdown(
f'''
<style>
.sidebar .sidebar-content {{
width: 390px;
}}
</style>
''',
unsafe_allow_html=True
)
col1,col2 = st.sidebar.beta_columns(2)
@st.cache
def get_url(query):
img = unsplash.search_photo(query)
img_url = img['img']
return img_url,img
def default_background():
page_bg_img = '''
<style>
body {
background-image: url("https://images.unsplash.com/photo-1421217336522-861978fdf33a?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1050&q=80");
background-size: cover;
}
</style>
'''
st.markdown(page_bg_img, unsafe_allow_html=True)
img_url,img = get_url("best landscape photos")
st.sidebar.write("Check out Github Repo for this App and my LinkedIn Profile")
background = st.sidebar.radio("Do You Want Use Default Background or Change?",("Default Background","Change Background"))
def change_background():
change = st.sidebar.button("Change Background of App")
if(change):
caching.clear_cache()
img_url,img = get_url("best landscape photos")
st.write("Photo By " + img['credits'] + " on Unsplash")
page_bg_img = '''
<style>
body {
background-image: url(''' +img_url+''');
background-size: cover;
}
</style>
'''
st.markdown(page_bg_img, unsafe_allow_html=True)
if(background=="Default Background"):
default_background()
if(background=="Change Background"):
page_bg_img = '''
<style>
body {
background-image: url(''' +img_url+''');
background-size: cover;
}
</style>
'''
st.markdown(page_bg_img, unsafe_allow_html=True)
change_background()
linked_in = '''[![LinkedIn](https://upload.wikimedia.org/wikipedia/commons/thumb/0/01/LinkedIn_Logo.svg/120px-LinkedIn_Logo.svg.png)](https://www.linkedin.com/in/kunal-vaidya-0bab51198/)'''
col1.markdown(linked_in, unsafe_allow_html=True)
github = '''[![LinkedIn](https://upload.wikimedia.org/wikipedia/commons/thumb/2/29/GitHub_logo_2013.svg/120px-GitHub_logo_2013.svg.png)](https://github.com/KunalVaidya99/Music-Genre-Classification)'''
col2.markdown(github, unsafe_allow_html=True)
file = st.sidebar.file_uploader("Please Upload Mp3 Audio File Here or Use Demo Of App Below using Preloaded Music",
type=["mp3"])
from PIL import Image
import librosa
import numpy as np
import librosa.display
from pydub import AudioSegment
import matplotlib.cm as cm
from matplotlib.colors import Normalize
class_labels = ['blues',
'classical',
'country',
'disco',
'hiphop',
'metal',
'pop',
'reggae',
'rock']
def GenreModel(input_shape = (288,432,4),classes=9):
X_input = Input(input_shape)
X = Conv2D(8,kernel_size=(3,3),strides=(1,1))(X_input)
X = BatchNormalization(axis=3)(X)
X = Activation('relu')(X)
X = MaxPooling2D((2,2))(X)
X = Conv2D(16,kernel_size=(3,3),strides = (1,1))(X)
X = BatchNormalization(axis=3)(X)
X = Activation('relu')(X)
X = MaxPooling2D((2,2))(X)
X = Conv2D(32,kernel_size=(3,3),strides = (1,1))(X)
X = BatchNormalization(axis=3)(X)
X = Activation('relu')(X)
X = MaxPooling2D((2,2))(X)
X = Conv2D(64,kernel_size=(3,3),strides=(1,1))(X)
X = BatchNormalization(axis=-1)(X)
X = Activation('relu')(X)
X = MaxPooling2D((2,2))(X)
X = Conv2D(128,kernel_size=(3,3),strides=(1,1))(X)
X = BatchNormalization(axis=-1)(X)
X = Activation('relu')(X)
X = MaxPooling2D((2,2))(X)
X = Conv2D(256,kernel_size=(3,3),strides=(1,1))(X)
X = BatchNormalization(axis=-1)(X)
X = Activation('relu')(X)
X = MaxPooling2D((2,2))(X)
X = Flatten()(X)
#X = Dropout(rate=0.3)(X)
#X = Dense(256,activation='relu')(X)
#X = Dropout(rate=0.4)(X)
X = Dense(classes, activation='softmax', name='fc' + str(classes), kernel_initializer = glorot_uniform(seed=9))(X)
model = Model(inputs=X_input,outputs=X,name='GenreModel')
return model
model = GenreModel(input_shape=(288,432,4),classes=9)
model.load_weights("genre.h5")
def convert_mp3_to_wav(music_file):
sound = AudioSegment.from_mp3(music_file)
sound.export("music_file.wav",format="wav")
def extract_relevant(wav_file,t1,t2):
wav = AudioSegment.from_wav(wav_file)
wav = wav[1000*t1:1000*t2]
wav.export("extracted.wav",format='wav')
def create_melspectrogram(wav_file):
y,sr = librosa.load(wav_file,duration=3)
mels = librosa.feature.melspectrogram(y=y,sr=sr)
fig = plt.Figure()
canvas = FigureCanvas(fig)
p = plt.imshow(librosa.power_to_db(mels,ref=np.max))
plt.savefig('melspectrogram.png')
def download_image():
filename = file.name
filename = str.split(filename,"(")[0]
downloader.download(filename + "Spotify", limit=1, output_dir='/', adult_filter_off=True, force_replace=False, timeout=60)
return filename
def download_image_demo(filename):
downloader.download(filename + "Spotify", limit=1, output_dir='/', adult_filter_off=True, force_replace=False, timeout=60)
def predict(image_data,model):
#image = image_data.resize((288,432))
image = img_to_array(image_data)
image = np.reshape(image,(1,288,432,4))
prediction = model.predict(image/255)
prediction = prediction.reshape((9,))
class_label = np.argmax(prediction)
return class_label,prediction
def show_output(songname):
convert_mp3_to_wav(songname + ".mp3")
extract_relevant("music_file.wav",40,50)
create_melspectrogram("extracted.wav")
image_data = load_img('melspectrogram.png',color_mode='rgba',target_size=(288,432))
download_image_demo(songname)
st.sidebar.write("The Song You have Choosen Is " +songname )
st.sidebar.image(songname +"Spotify" + "/Image_1.jpg",use_column_width=True)
st.sidebar.write("**Play the Song Below if you want!**")
st.sidebar.audio(songname + ".mp3" ,"audio/mp3")
class_label,prediction = predict(image_data,model)
st.write("## The Genre of Song is "+class_labels[class_label])
spec_or_prob = st.sidebar.radio("Select one of Below",("Probability Distribution","Mel Spectrogram"))
prediction = prediction.reshape((9,))
color_data = [1,2,3,4,5,6,7,8,9]
my_cmap = cm.get_cmap('jet')
my_norm = Normalize(vmin=0, vmax=9)
if(spec_or_prob =="Probability Distribution"):
fig,ax= plt.subplots(figsize=(6,4.5))
ax.bar(x=class_labels,height=prediction,
color=my_cmap(my_norm(color_data)))
plt.xticks(rotation=45)
ax.set_title("Probability Distribution Of The Given Song Over Different Genres")
plt.show()
st.pyplot(fig)
if(spec_or_prob=="Mel Spectrogram"):
st.image("melspectrogram.png",use_column_width=True)
demo = st.sidebar.checkbox("Do You Want to check the App with Preloaded Music")
if(demo):
song = st.sidebar.radio("Which Song you Want to check?",("Green Day-American Idiot","Taylor Swift-Love Story","Nirvana-Smells Like Teen Spirit","Muse-Plug In Baby"))
if(song=="Green Day-American Idiot"):
show_output("Green Day-American Idiot")
if(song=="Muse-Plug In Baby"):
show_output("Muse-Plug In Baby")
if(song=="Taylor Swift-Love Story"):
show_output("Taylor Swift-Love Story")
if(song=="Nirvana-Smells Like Teen Spirit"):
show_output("Nirvana-Smells Like Teen Spirit")
if file is None:
st.text("Please upload an mp3 file")
else:
convert_mp3_to_wav(file)
extract_relevant("music_file.wav",40,50)
create_melspectrogram("extracted.wav")
image_data = load_img('melspectrogram.png',color_mode='rgba',target_size=(288,432))
filename = download_image()
st.sidebar.write("The Song You have Choosen Is " +filename )
st.sidebar.image(filename +"Spotify" + "/Image_1.jpg",use_column_width=True)
st.sidebar.write("**Play the Song Below if you want!**")
st.sidebar.audio(file,"audio/mp3")
class_label,prediction = predict(image_data,model)
st.write("## The Genre of Song is "+class_labels[class_label])
spec_or_prob = st.sidebar.radio("Select one of Below",("Probability Distribution","Mel Spectrogram"))
prediction = prediction.reshape((9,))
color_data = [1,2,3,4,5,6,7,8,9]
my_cmap = cm.get_cmap('jet')
my_norm = Normalize(vmin=0, vmax=9)
if(spec_or_prob =="Probability Distribution"):
fig,ax= plt.subplots(figsize=(6,4.5))
ax.bar(x=class_labels,height=prediction,
color=my_cmap(my_norm(color_data)))
plt.xticks(rotation=45)
ax.set_title("Probability Distribution Of The Given Song Over Different Genres")
plt.show()
st.pyplot(fig)
if(spec_or_prob=="Mel Spectrogram"):
st.image("melspectrogram.png",use_column_width=True)
#st.text("Probability (0: Blues, 1: Classical, 2: Country,3: Disco,4: Hiphop,5: Metal,6: Pop,7: Reggae,8: Rock")
#st.write(prediction)