-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
91 lines (82 loc) · 3.02 KB
/
utils.py
File metadata and controls
91 lines (82 loc) · 3.02 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import librosa
import numpy as np
import pdb
import string
from Levenshtein import distance
def wav2feat(wavfile):
'''
Input: audio wav file name
Output: Magnitude spectrogram
'''
x, Fs = librosa.load(wavfile, sr=44100, mono=True)
hop = int(0.01 * Fs) # 10ms
win = int(0.02 * Fs) # 20ms
X = librosa.stft(x, n_fft=1024, hop_length=hop, win_length=win, window='hann', center=True, pad_mode='reflect')
return np.abs(X)
def wavs2feat(wavfiles):
'''
Concatenate the audio files listed in wavfiles
Input: list of audio wav file names
Output: Magnitude spectrogram of concatenated wav
'''
x = []
for wf in wavfiles:
x1, Fs = librosa.load(wf, sr=44100, mono=True)
x.append(x1)
x = np.hstack(x)
hop = int(0.01 * Fs) # 10ms
win = int(0.02 * Fs) # 20ms
X = librosa.stft(x, n_fft=1024, hop_length=hop, win_length=win, window='hann', center=True, pad_mode='reflect')
return np.abs(X)
def read_csv(filename):
id_label = {}
with open(filename,'r') as fid:
for line in fid: # '176787-5-0-27.wav,engine_idling\n'
tokens = line.strip().split(',') # ['176787-5-0-27.wav', 'engine_idling']
id_label[tokens[0]] = tokens[1]
return id_label
def editDistance(gt, est):
'''both are lists of labels
E.g. gt is "dog_bark-street_music-engine_idling"
E.g. est is "street_music-engine_idling"
'''
gttokens = gt.split('-')
esttokens = est.split('-')
# Map token to char
tokenset = list(set(gttokens+esttokens)) # ['dog_bark', 'siren', 'street_music', 'engine_idling']
token_char = {}
for i in range(len(tokenset)):
token_char[tokenset[i]] = string.ascii_uppercase[i] # {'dog_bark': 'A', 'siren': 'B', 'street_music': 'C', 'engine_idling': 'D'}
# convert gt and est to strings
gtstr = [token_char[t] for t in gttokens]
gtstr = ''.join(gtstr) # 'BCA'
eststr = [token_char[t] for t in esttokens]
eststr = ''.join(eststr) #
# Compare
editdist = distance(gtstr, eststr) # 1
score = 1 - editdist/len(gtstr)
return editdist, score
def evals(gtcsv, estcsv, taskid):
gt_id_label = read_csv(gtcsv)
est_id_label = read_csv(estcsv)
score = 0
for id in est_id_label:
if taskid==1:
if est_id_label[id] == gt_id_label[id]:
score += 1
elif taskid==2:
_, ss = editDistance(gt_id_label[id], est_id_label[id])
score += ss
else:
pdb.set_trace()
assert False, ["taskid not correct; it is", taskid]
avgScore = score/len(est_id_label)
return avgScore
if __name__=="__main__":
wavs = ['../shared_train/audio_train/180937-7-3-27.wav']
wavs2feat(wavs)
# # wavfiles = ['../shared_train/audio_train/180937-7-3-27.wav','../shared_train/audio_train/180937-7-3-27.wav']
# # X = wavs2feat(wavfiles)
# # eval('test_task1/labels.csv', 'test_task1/est.csv', 1)
# editDistance("dog_bark-street_music-engine_idling",
# "siren-street_music-engine_idling")