forked from espnet/espnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecog_wav.sh
executable file
·242 lines (205 loc) · 7 KB
/
recog_wav.sh
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/bin/bash
# Copyright 2019 Nagoya University (Takenori Yoshimura)
# 2019 RevComm Inc. (Takekatsu Hiramura)
# Apache 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
if [ ! -f path.sh ] || [ ! -f cmd.sh ]; then
echo "Please change current directory to recipe directory e.g., egs/tedlium2/asr1"
exit 1
fi
. ./path.sh
# general configuration
backend=pytorch
stage=0 # start from 0 if you need to start from data preparation
stop_stage=100
ngpu=0 # number of gpus ("0" uses cpu, otherwise use gpu)
debugmode=1
verbose=1 # verbose option
# feature configuration
do_delta=false
cmvn=
# rnnlm related
use_lang_model=true
lang_model=
# decoding parameter
recog_model=
decode_config=
decode_dir=decode
api=v2
# download related
models=tedlium2.transformer.v1
help_message=$(cat <<EOF
Usage:
$0 [options] <wav_file>
Options:
--backend <chainer|pytorch> # chainer or pytorch (Default: pytorch)
--ngpu <ngpu> # Number of GPUs (Default: 0)
--decode_dir <directory_name> # Name of directory to store decoding temporary data
--models <model_name> # Model name (e.g. tedlium2.tacotron2.v1)
--cmvn <path> # Location of cmvn.ark
--lang_model <path> # Location of language model
--recog_model <path> # Location of E2E model
--decode_config <path> # Location of configuration file
--api <api_version> # API version (v1 or v2, available in only pytorch backend)
Example:
# Record audio from microphone input as example.wav
rec -c 1 -r 16000 example.wav trim 0 5
# Decode using model name
$0 --models tedlium2.rnn.v1 example.wav
# Decode using model file
$0 --cmvn cmvn.ark --lang_model rnnlm.model.best --recog_model model.acc.best --decode_config conf/decode.yaml example.wav
Available models:
- tedlium2.rnn.v1
- tedlium2.transformer.v1
- tedlium3.transformer.v1
- librispeech.transformer.v1
- commonvoice.transformer.v1
EOF
)
. utils/parse_options.sh || exit 1;
# make shellcheck happy
train_cmd=
decode_cmd=
. ./cmd.sh
wav=$1
download_dir=${decode_dir}/download
if [ $# -lt 1 ]; then
echo "${help_message}"
exit 1;
fi
set -e
set -u
set -o pipefail
# check api version
if [ "${api}" = "v2" ] && [ "${backend}" = "chainer" ]; then
echo "chainer backend does not support api v2." >&2
exit 1;
fi
# Check model name or model file is set
if [ -z $models ]; then
if [ $use_lang_model = "true" ]; then
if [[ -z $cmvn || -z $lang_model || -z $recog_model || -z $decode_config ]]; then
echo 'Error: models or set of cmvn, lang_model, recog_model and decode_config are required.' >&2
exit 1
fi
else
if [[ -z $cmvn || -z $recog_model || -z $decode_config ]]; then
echo 'Error: models or set of cmvn, recog_model and decode_config are required.' >&2
exit 1
fi
fi
fi
dir=${download_dir}/${models}
mkdir -p ${dir}
function download_models () {
if [ -z $models ]; then
return
fi
case "${models}" in
"tedlium2.rnn.v1") share_url="https://drive.google.com/open?id=1UqIY6WJMZ4sxNxSugUqp3mrGb3j6h7xe" ;;
"tedlium2.transformer.v1") share_url="https://drive.google.com/open?id=1mgbiWabOSkh_oHJIDA-h7hekQ3W95Z_U" ;;
"tedlium3.transformer.v1") share_url="https://drive.google.com/open?id=1wYYTwgvbB7uy6agHywhQfnuVWWW_obmO" ;;
"librispeech.transformer.v1") share_url="https://drive.google.com/open?id=1BtQvAnsFvVi-dp_qsaFP7n4A_5cwnlR6" ;;
"commonvoice.transformer.v1") share_url="https://drive.google.com/open?id=1tWccl6aYU67kbtkm8jv5H6xayqg1rzjh" ;;
*) echo "No such models: ${models}"; exit 1 ;;
esac
if [ ! -e ${dir}/.complete ]; then
download_from_google_drive.sh ${share_url} ${dir} ".tar.gz"
touch ${dir}/.complete
fi
}
# Download trained models
if [ -z "${cmvn}" ]; then
download_models
cmvn=$(find ${download_dir}/${models} -name "cmvn.ark" | head -n 1)
fi
if [ -z "${lang_model}" ] && ${use_lang_model}; then
download_models
lang_model=$(find ${download_dir}/${models} -name "rnnlm*.best*" | head -n 1)
fi
if [ -z "${recog_model}" ]; then
download_models
recog_model=$(find ${download_dir}/${models} -name "model*.best*" | head -n 1)
fi
if [ -z "${decode_config}" ]; then
download_models
decode_config=$(find ${download_dir}/${models} -name "decode*.yaml" | head -n 1)
fi
if [ -z "${wav}" ]; then
download_models
wav=$(find ${download_dir}/${models} -name "*.wav" | head -n 1)
fi
# Check file existence
if [ ! -f "${cmvn}" ]; then
echo "No such CMVN file: ${cmvn}"
exit 1
fi
if [ ! -f "${lang_model}" ] && ${use_lang_model}; then
echo "No such language model: ${lang_model}"
exit 1
fi
if [ ! -f "${recog_model}" ]; then
echo "No such E2E model: ${recog_model}"
exit 1
fi
if [ ! -f "${decode_config}" ]; then
echo "No such config file: ${decode_config}"
exit 1
fi
if [ ! -f "${wav}" ]; then
echo "No such WAV file: ${wav}"
exit 1
fi
base=$(basename $wav .wav)
decode_dir=${decode_dir}/${base}
if [ ${stage} -le 0 ] && [ ${stop_stage} -ge 0 ]; then
echo "stage 0: Data preparation"
mkdir -p ${decode_dir}/data
echo "$base $wav" > ${decode_dir}/data/wav.scp
echo "X $base" > ${decode_dir}/data/spk2utt
echo "$base X" > ${decode_dir}/data/utt2spk
echo "$base X" > ${decode_dir}/data/text
fi
if [ ${stage} -le 1 ] && [ ${stop_stage} -ge 1 ]; then
echo "stage 1: Feature Generation"
steps/make_fbank_pitch.sh --cmd "$train_cmd" --nj 1 --write_utt2num_frames true \
${decode_dir}/data ${decode_dir}/log ${decode_dir}/fbank
feat_recog_dir=${decode_dir}/dump; mkdir -p ${feat_recog_dir}
dump.sh --cmd "$train_cmd" --nj 1 --do_delta ${do_delta} \
${decode_dir}/data/feats.scp ${cmvn} ${decode_dir}/log \
${feat_recog_dir}
fi
if [ ${stage} -le 2 ] && [ ${stop_stage} -ge 2 ]; then
echo "stage 2: Json Data Preparation"
dict=${decode_dir}/dict
echo "<unk> 1" > ${dict}
feat_recog_dir=${decode_dir}/dump
data2json.sh --feat ${feat_recog_dir}/feats.scp \
${decode_dir}/data ${dict} > ${feat_recog_dir}/data.json
rm -f ${dict}
fi
if [ ${stage} -le 3 ] && [ ${stop_stage} -ge 3 ]; then
echo "stage 3: Decoding"
if ${use_lang_model}; then
recog_opts="--rnnlm ${lang_model}"
else
recog_opts=""
fi
feat_recog_dir=${decode_dir}/dump
${decode_cmd} ${decode_dir}/log/decode.log \
asr_recog.py \
--config ${decode_config} \
--ngpu ${ngpu} \
--backend ${backend} \
--debugmode ${debugmode} \
--verbose ${verbose} \
--recog-json ${feat_recog_dir}/data.json \
--result-label ${decode_dir}/result.json \
--model ${recog_model} \
--api ${api} \
${recog_opts}
echo ""
recog_text=$(grep rec_text ${decode_dir}/result.json | sed -e 's/.*: "\(.*\)".*/\1/' | sed -e 's/<eos>//')
echo "Recognized text: ${recog_text}"
echo ""
echo "Finished"
fi