-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_extraction.py
40 lines (31 loc) · 1.27 KB
/
data_extraction.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
import librosa
import numpy as np
import pandas as pd
import deeplake
dataset_path = 'hub://activeloop/gtzan-genre'
ds = deeplake.load(dataset_path)
features = []
filenames = []
genres = []
for i, audio_blob in enumerate(ds['audio']):
audio_data = audio_blob.numpy().flatten()
filename = f'file_{i}'
genre = ds['genre'][i].numpy().item()
sr = 22050
feature_vector = {
'chroma_stft': np.mean(librosa.feature.chroma_stft(y=audio_data, sr=sr)),
'rmse': np.mean(librosa.feature.rms(y=audio_data)),
'spectral_centroid': np.mean(librosa.feature.spectral_centroid(y=audio_data, sr=sr)),
'spectral_bandwidth': np.mean(librosa.feature.spectral_bandwidth(y=audio_data, sr=sr)),
'rolloff': np.mean(librosa.feature.spectral_rolloff(y=audio_data, sr=sr)),
'zero_crossing_rate': np.mean(librosa.feature.zero_crossing_rate(y=audio_data)),
'mfcc': np.mean(librosa.feature.mfcc(y=audio_data, sr=sr), axis=1).tolist()
}
features.append(feature_vector)
filenames.append(filename)
genres.append(genre)
features_df = pd.DataFrame(features)
features_df['filename'] = filenames
features_df['genre'] = genres
features_df.to_csv('audio_features.csv', index=False)
print("Features extracted for all audio files.")