I have a speech dataset and wanted to extract spectrogram/chromogram images as jpeg in google drive. The code snippet I am saving here saves only the last image. I have seen that librosa library gives only bgr images. Can someone help me in resolving this issue?
import os, glob
from PIL import Image, ImageOps
import matplotlib.pyplot as plt
def load_data():
X,y=[],[]
count = 0
for file in glob.glob("/content/drive/My Drive/rand/data/Actor_*//*.wav"):
file_name=os.path.basename(file)
#src_fname, ext = file_name.splitext(x)
emotion=emotions[file_name.split("-")[2]]
chromagram = feature_chromagram(waveform, sample_rate)
librosa.display.specshow(chromagram, y_axis='chroma', x_axis='time')
fig = plt.Figure()
save_fname = os.path.join(PATH, os.path.basename(emotion) '.tif')
#im.save(save_fname)
fig.savefig(save_fname)
features = get_features(file)
X.append(features)
#y.append(emotion)
count = 1
# '\r' end='' results in printing over same line
print('\r' f' Processed {count}/{1440} audio samples',end=' ')
# Return arrays to plug into sklearn's cross-validation algorithms
return np.array(X), np.array(y) ```
CodePudding user response:
I got the code corrected..
for folder in os.listdir(data_dir):
i=0
for fil in os.listdir(os.path.join(data_dir,folder)):
dir=os.path.join(data_dir,folder,fil)
with soundfile.SoundFile(dir) as audio:
waveform=audio.read(dtype="float32")
sample_rate=audio.samplerate
librosa.display.waveplot(waveform,sr=sample_rate)
stft_spectrum_matrix = librosa.stft(waveform)
librosa.display.specshow(librosa.amplitude_to_db(np.abs(stft_spectrum_matrix), ref=np.max),y_axis='log', x_axis='time')
#print(i)
plt.savefig(os.path.join(out_dir,folder,'spec{:04}'.format(i)))
mfc_coefficients = librosa.feature.mfcc(waveform, sr=sample_rate, n_mfcc=40)
librosa.display.specshow(mfc_coefficients, x_axis='time',norm=Normalize(vmin=-30,vmax=30))
plt.savefig(os.path.join(out_dir,folder,'mfcc{:04}'.format(i)))
chromagram = librosa.feature.chroma_stft(waveform, sr=sample_rate)
librosa.display.specshow(chromagram, y_axis='chroma', x_axis='time')
plt.savefig(os.path.join(out_dir,folder,'chroma{:04}'.format(i)))
i =1