-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdata_helpers.py
71 lines (57 loc) · 3.1 KB
/
data_helpers.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import pandas as pd
import librosa
import os
base = yaml.load(open('../CONFIG.yaml'))['base_dir']
labels_dir = base + '/labels/Golden/'
wav_dir = base + '/wavs/'
human_noises = set(['mix traffic', 'braking', 'voices', 'electrical',
'anthropogenic unknown', 'airplane', 'beep',
'metal', 'bus emitting', 'footsteps', 'mower', 'whistle',
'siren', 'coughing', 'music', 'horn', 'startthecar', 'bells',
'applause', 'dog bark', 'road traffic', 'braking vehicle (road or rail)',
'human voices', 'mechanical', 'vehicle horn (road or rail)',
'air traffic', 'vehicle alarm', 'human voice', 'machinery',
'church bell', 'breaking vehicle', 'deck lowering', 'car horn',
'rail traffic', 'alarm', 'vehicle horn',
'building ventilation system', 'car alarm', 'rock', 'church bells',
'train horn', 'mobile phone', 'train station announcement', 'hammering',
'door opening', 'dog barking', 'vehicle breaking', 'cat',
'glass into bins', 'barking dog', 'television', 'sweeping broom',
'ball bouncing', 'bat hitting ball', 'laughing', 'clapping', 'camera',
'train doors (beeping)', 'lawnmower'])
animal_noises = set(['bird', 'wing beats', 'bat', 'fox',
'grey squirrel', 'invertebrate', 'insect', 'animal',
'wings beating', 'russling leaves (animal)',
'amphibian', 'squirrel', 'russling vegetation (animal)'])
other = set(['rain', 'unknown sound', 'electrical disturbance', 'vegetation',
'wind', 'unknown', 'metalic sound', 'dripping water', 'shower',
'metalic', 'rubbish bag','water dripping', 'water splashing',
'rainfall on vegetation'])
def load_annotations(fname, labels_dir=labels_dir, wav_dir=wav_dir):
csv_fname = labels_dir + fname.replace('.wav', '-sceneRect.csv')
if os.path.exists(csv_fname):
pd_annots = pd.read_csv(csv_fname)
else:
pd_annots = pd.DataFrame()
print "Warning - no annotations found for %s" % fname
# where we'll temp store the blank snippets for this file
blank_snippets = []
# load file and convert to spectrogram
wav, sample_rate = librosa.load(wav_dir + fname, sr=22050)
# create label vector...
biotic = 0 * wav
anthropogenic = 0 * wav
# loop over each annotation...
for _, annot in pd_annots.iterrows():
# fill in the label vector
start_point = int(float(annot['LabelStartTime_Seconds']) * sample_rate)
end_point = int(float(annot['LabelEndTime_Seconds']) * sample_rate)
if annot['Label'].lower() in human_noises:
anthropogenic[start_point:end_point] = 1
elif annot['Label'].lower() in animal_noises:
biotic[start_point:end_point] = 1
elif annot['Label'].lower() in other:
pass
else:
raise Exception("Unknown label ", annot['Label'])
return {'anthrop': anthropogenic, 'biotic': biotic}, wav, sample_rate