forked from harvard-edge/multilingual_kws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
label_directory_dataperf.py
214 lines (178 loc) · 6.62 KB
/
label_directory_dataperf.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import fire
import subprocess
import argparse
import sys
import tty
import termios
from pathlib import Path
import csv
import os
import code
# import pydub
# import pydub.playback
# import pydub.effects
class _GetchUnix:
"""https://stackoverflow.com/a/510364"""
def __call__(self):
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
def label_hf(datadir: os.PathLike, keyword: str, csv_outdir: os.PathLike):
datadir = Path(datadir)
print("datadir:", datadir)
print(f"keyword: {keyword}")
val_kws = (datadir / "validation").glob(f"{keyword}_*.wav")
test_kws = (datadir / "test").glob(f"{keyword}_*.wav")
wav_list = list(val_kws) + list(test_kws)
outfile = Path(csv_outdir) / f"{keyword}_validated.csv"
assert not outfile.exists(), f"outfile already exists: {outfile}"
assert len(wav_list) > 0, f"no files found for keyword: {keyword}"
getch = _GetchUnix()
results = []
for ix, clip in enumerate(wav_list):
print(
f"\n:::::: CLIP # {ix:03d} / {len(wav_list)} ::: {ix / len(wav_list):0.2f}",
clip.name,
)
fpath = str(datadir / clip)
while True:
# for linux:
# wav = pydub.AudioSegment.from_wav(fpath)
# wav = pydub.effects.normalize(wav)
# pydub.playback.play(wav)
# for mac (note: skips normalization)
# assumes ffmpeg (loudnorm or speechnorm) was used already to normalize
# https://superuser.com/questions/323119/how-can-i-normalize-audio-using-ffmpeg
subprocess.call(["afplay", fpath])
print("rating? (g)ood, (b)ad, listen (a)gain, (q)uit")
choice = getch()
if choice == "g":
usable = True
break
elif choice == "b":
usable = False
break
elif choice == "q":
print("not writing output")
sys.exit()
result = "good" if usable else "bad"
# convert keyword_common_voice_id_1234.wav to keyword/common_voice_id_1234.wav
dataperf_clip_id = keyword + "/" + clip.name[len(keyword) + 1 :]
row = [dataperf_clip_id, result]
print(f"{ix:03d} {row}")
results.append(row)
with open(outfile, "a") as fh: # append to exisiting file
writer = csv.writer(fh)
writer.writerows(results)
# summary
print("\n\n\n\n:::::::::: SUMMARY ")
print("num good:", len([g for g in results if g[1] == "good"]))
print("num bad:", len([g for g in results if g[1] == "bad"]))
print(f">>>>>> results written to {outfile}")
def label(args):
already_listened = []
if os.path.isfile(args.out_csv):
previous_file = Path(args.out_csv)
print("datadir:", previous_file)
with open(previous_file, "r") as fh:
reader = csv.reader(fh) # file, good?
for row in reader:
already_listened.append(row[0])
already_listened = set(already_listened)
datadir = Path(args.datadir)
print("datadir:", datadir)
splitdir = Path(args.splitdir)
print("splitdir:", splitdir)
wav_list = [] # list of wav file names in the dev and test splits
dev_splits = splitdir / "en_dev.csv"
with open(dev_splits, "r") as fh:
reader = csv.reader(fh) # SET,LINK,WORD,VALID,SPEAKER,GENDER
for row in reader:
if row[1] == args.word:
opus = row[0] # aachen/common_voice_en_18833718.opus
wav = opus.replace("opus", "wav")
if wav in already_listened:
continue
wav_list.append(wav)
test_splits = splitdir / "en_test.csv"
with open(test_splits, "r") as fh:
reader = csv.reader(fh)
for row in reader:
if row[1] == args.word:
opus = row[0]
wav = opus.replace("opus", "wav")
if wav in already_listened:
continue
wav_list.append(wav)
print("len(wav_list):", len(wav_list))
getch = _GetchUnix()
results = []
for ix, clip in enumerate(wav_list):
print(f"\n:::::: CLIP # {ix} :::", clip)
fpath = str(datadir / clip)
stop_and_save = False
while True:
print("-")
# for linux:
# wav = pydub.AudioSegment.from_wav(fpath)
# wav = pydub.effects.normalize(wav)
# pydub.playback.play(wav)
# for mac (note: skips normalization)
# assumes ffmpeg (loudnorm or speechnorm) was used already to normalize
# https://superuser.com/questions/323119/how-can-i-normalize-audio-using-ffmpeg
subprocess.call(["afplay", fpath])
choice = getch()
if choice == "g":
usable = True
break
elif choice == "b":
usable = False
break
elif choice == "s":
stop_and_save = True
break
elif choice == "q":
sys.exit()
result = "good" if usable else "bad"
row = [clip, result]
print(row)
results.append(row)
if stop_and_save:
break # allows stopping and saving part of the way through a word
if not args.dryrun:
out_csv = args.out_csv
with open(out_csv, "a") as fh: # append to exisiting file
writer = csv.writer(fh)
writer.writerows(results)
# summary
print("\n\n\n\n:::::::::: SUMMARY ")
print("num good:", len([g for g in results if g[1] == "good"]))
print("num bad:", len([g for g in results if g[1] == "bad"]))
if not args.dryrun:
print(f">>>>>> results written to {out_csv}")
def main():
parser = argparse.ArgumentParser(description="labeler for listening data")
parser.add_argument("datadir", help="directory of wavs")
parser.add_argument("splitdir", help="directory of split csvs")
parser.add_argument("word", help="word to analyze")
parser.add_argument(
"out_csv", help="output filepath. location of checkpoint if previously saved"
)
parser.add_argument(
"--dryrun", action="store_true", help="do not write to output csv file"
)
label(parser.parse_args())
def test():
getch = _GetchUnix()
while True:
choice = getch()
print(choice)
if choice == "q":
break
if __name__ == "__main__":
fire.Fire(label_hf)