Last active
November 1, 2023 09:12
-
-
Save parulnith/7f8c174e6ac099e86f0495d3d9a4c01e to your computer and use it in GitHub Desktop.
Untitled9.ipynb
What was the use of spectrogram images???
I wrote a easy to understand notebook based on FE ideas in this one:
https://github.com/jkotra/MusicGenreClassification/blob/master/MusicGenreClassification_FeatureEnsemble.ipynb
Take a look if this seems too complicated 😉
Hello @ndujar.
Could you please describe in more detail how to me predictions, namely the arrays that are displayed during prediction, turn into genres. I will be very grateful!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have just been trying to use the same code of above but I am getting error. I have just made tiny changes on file directory.
Extracting spec from audios
cmap = plt.get_cmap('inferno')
plt.figure(figsize=(10,10))
genres = 'blues classical country disco hiphop jazz metal pop reggae rock'.split()
for g in genres:
pathlib.Path(f'image_genres/{g}').mkdir(parents=True, exist_ok=True)
for filename in os.listdir(f'./genres/{g}'):
songname = f'./genres/{g}/{filename}'
y, sr = librosa.load(songname, mono=True, duration=5)
plt.specgram(y, NFFT=2048, Fs=2, Fc=0, noverlap=128, cmap=cmap, sides='default', mode='default', scale='dB');
plt.axis('off');
plt.savefig(f'image_genres/{g}/{filename[:-3].replace(".", "")}.png')
plt.clf()
extracting features from spect
header = 'filename chroma_stft rmse spectral_centroid spectral_bandwidth rolloff zero_crossing_rate'
for i in range(1, 21):
header += f' mfcc{i}'
header += ' label'
header = header.split()
writing to csv
file = open('data.csv', 'w', newline='')
with file:
writer = csv.writer(file)
writer.writerow(header)
genres = 'blues classical country disco hiphop jazz metal pop reggae rock'.split()
for g in genres:
for filename in os.listdir(f'./genres/{g}'):
songname = f'./genres/{g}/{filename}'
y, sr = librosa.load(songname, mono=True, duration=30)
chroma_stft = librosa.feature.chroma_stft(y=y, sr=sr)
spec_cent = librosa.feature.spectral_centroid(y=y, sr=sr)
spec_bw = librosa.feature.spectral_bandwidth(y=y, sr=sr)
rolloff = librosa.feature.spectral_rolloff(y=y, sr=sr)
zcr = librosa.feature.zero_crossing_rate(y)
mfcc = librosa.feature.mfcc(y=y, sr=sr)
to_append = f'{filename} {np.mean(chroma_stft)} {np.mean(spec_cent)} {np.mean(spec_bw)} {np.mean(rolloff)} {np.mean(zcr)}'
for e in mfcc:
to_append += f' {np.mean(e)}'
to_append += f' {g}'
file = open('data.csv', 'a', newline='')
with file:
writer = csv.writer(file)
writer.writerow(to_append.split())
reading csv
data = pd.read_csv('data.csv')
data.head()
standard scaler
scaler = StandardScaler()
X = scaler.fit_transform(np.array(data.iloc[:, :1], dtype = float))