Commit dc5ab5e5 authored by Jan Rudolf's avatar Jan Rudolf

ADD features loading from cache

parent bc203805
......@@ -9,13 +9,15 @@ import librosa
DATASET_FOLDER = 'data'
DATASET_FILE = os.path.join(DATASET_FOLDER, 'input.mf')
OUTPUT_FOLDER = 'input'
OUTPUT_FILE = os.path.join(OUTPUT_FOLDER, 'data.csv')
DUMB_PREFIX = '/Users/sness/mirex2008/genres/'
def preprocess():
def preprocess(n_mfcc):
output_folder = f'preprocessed_nmfcc_{n_mfcc}'
output_file = os.path.join(output_folder, 'data.csv')
columns = ['file', 'label']
df = pd.read_csv(DATASET_FILE, sep='\t', header=None, names=columns)
......@@ -24,10 +26,14 @@ def preprocess():
output_index = list()
if not os.path.isdir(OUTPUT_FOLDER):
os.mkdir(OUTPUT_FOLDER)
if not os.path.isdir(output_folder):
os.mkdir(output_folder)
else:
print('Cached features found')
return
for genre in df['label'].unique().tolist():
genre_folder = os.path.join(OUTPUT_FOLDER, genre)
genre_folder = os.path.join(output_folder, genre)
if not os.path.isdir(genre_folder):
os.mkdir(genre_folder)
......@@ -35,13 +41,13 @@ def preprocess():
dumb_absolute_path = row['file']
smart_suffix = dumb_absolute_path[len(DUMB_PREFIX):]
smart_relative_path = os.path.join(DATASET_FOLDER, smart_suffix)
smart_relative_output_path = os.path.join(OUTPUT_FOLDER, f'{smart_suffix[:-4]}.npy')
smart_relative_output_path = os.path.join(output_folder, f'{smart_suffix[:-4]}.npy')
output_index.append((smart_relative_output_path, row['label']))
audio_file, sr = librosa.load(smart_relative_path)
y, _ = librosa.effects.trim(audio_file)
mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=40)
mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=n_mfcc)
np.save(smart_relative_output_path, mfccs)
......@@ -50,8 +56,8 @@ def preprocess():
print('\nDone')
output_df = pd.DataFrame(output_index, columns=columns, index=None)
output_df.to_csv(OUTPUT_FILE, header=False, index=False)
output_df.to_csv(output_file, header=False, index=False)
if __name__ == '__main__':
preprocess()
preprocess(20)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment