-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocess_audio.py
More file actions
59 lines (38 loc) · 1.48 KB
/
preprocess_audio.py
File metadata and controls
59 lines (38 loc) · 1.48 KB
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
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import os
import warnings
from tqdm import tqdm
from sklearn.preprocessing import MinMaxScaler
import librosa
import librosa.display
audio_path = r"C:\Users\David D'Amario\Data\Birdcalls\train_audio"
df_train = pd.read_csv(r"C:\Users\David D'Amario\Data\Birdcalls\train.csv")
df_test = pd.read_csv(r"C:\Users\David D'Amario\Data\Birdcalls\test.csv")
audio_samples = []
def get_samples(file_path):
with warnings.catch_warnings():
warnings.simplefilter("ignore")
wave_data, sr = librosa.load(file_path)
n_clips = int(len(wave_data)/(sr*5))
clip_index = np.random.choice(n_clips)
wave_data = wave_data[clip_index:clip_index + sr*5]
mel_spec = librosa.feature.melspectrogram(wave_data, fmin=300)
mel_spec = librosa.power_to_db(mel_spec)
mel_spec = (mel_spec - np.min(mel_spec))/np.ptp(mel_spec)
return mel_spec
for i in tqdm(range(len(df_train))):
audio_filename = df_train['filename'][i]
bird = df_train['ebird_code'][i]
file_path = os.path.join(audio_path, bird, audio_filename)
try:
mel_spec = get_samples(file_path)
except:
continue
new_item = {'mel spec': mel_spec, 'bird': bird, 'id': audio_filename}
audio_samples.append(new_item)
df_train_audio = pd.DataFrame(audio_samples)
df_train_audio.to_pickle(r"C:\Users\David D'Amario\Data\Birdcalls\train_audio_samples.pkl")
print(len(df_train_audio))
print(df_train_audio.head())