-
Notifications
You must be signed in to change notification settings - Fork 1
/
transcriber.py
290 lines (243 loc) · 10.7 KB
/
transcriber.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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# https://github.com/NVIDIA/NeMo/blob/main/tutorials/asr/02_Online_ASR_Microphone_Demo.ipynb
import socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
HOST = '127.0.0.1' # Standard loopback interface address (localhost)
PORT = 65432 # Port to listen on (non-privileged ports are > 1023)
s.connect((HOST, PORT))
import numpy as np
import pyaudio as pa
import os, time
import nemo
import nemo.collections.asr as nemo_asr
# sample rate, Hz
SAMPLE_RATE = 16000 # Quatrz model is for 16 KHz
asr_model = nemo_asr.models.EncDecCTCModel.from_pretrained('QuartzNet15x5Base-En')
from omegaconf import OmegaConf
import copy
# Preserve a copy of the full config
cfg = copy.deepcopy(asr_model._cfg)
#print(OmegaConf.to_yaml(cfg))
# Make config overwrite-able
OmegaConf.set_struct(cfg.preprocessor, False)
# some changes for streaming scenario
cfg.preprocessor.params.dither = 0.0
cfg.preprocessor.params.pad_to = 0
# spectrogram normalization constants
normalization = {}
normalization['fixed_mean'] = [
-14.95827016, -12.71798736, -11.76067913, -10.83311182,
-10.6746914, -10.15163465, -10.05378331, -9.53918999,
-9.41858904, -9.23382904, -9.46470918, -9.56037,
-9.57434245, -9.47498732, -9.7635205, -10.08113074,
-10.05454561, -9.81112681, -9.68673603, -9.83652977,
-9.90046248, -9.85404766, -9.92560366, -9.95440354,
-10.17162966, -9.90102482, -9.47471025, -9.54416855,
-10.07109475, -9.98249912, -9.74359465, -9.55632283,
-9.23399915, -9.36487649, -9.81791084, -9.56799225,
-9.70630899, -9.85148006, -9.8594418, -10.01378735,
-9.98505315, -9.62016094, -10.342285, -10.41070709,
-10.10687659, -10.14536695, -10.30828702, -10.23542833,
-10.88546868, -11.31723646, -11.46087382, -11.54877829,
-11.62400934, -11.92190509, -12.14063815, -11.65130117,
-11.58308531, -12.22214663, -12.42927197, -12.58039805,
-13.10098969, -13.14345864, -13.31835645, -14.47345634]
normalization['fixed_std'] = [
3.81402054, 4.12647781, 4.05007065, 3.87790987,
3.74721178, 3.68377423, 3.69344, 3.54001005,
3.59530412, 3.63752368, 3.62826417, 3.56488469,
3.53740577, 3.68313898, 3.67138151, 3.55707266,
3.54919572, 3.55721289, 3.56723346, 3.46029304,
3.44119672, 3.49030548, 3.39328435, 3.28244406,
3.28001423, 3.26744937, 3.46692348, 3.35378948,
2.96330901, 2.97663111, 3.04575148, 2.89717604,
2.95659301, 2.90181116, 2.7111687, 2.93041291,
2.86647897, 2.73473181, 2.71495654, 2.75543763,
2.79174615, 2.96076456, 2.57376336, 2.68789782,
2.90930817, 2.90412004, 2.76187531, 2.89905006,
2.65896173, 2.81032176, 2.87769857, 2.84665271,
2.80863137, 2.80707634, 2.83752184, 3.01914511,
2.92046439, 2.78461139, 2.90034605, 2.94599508,
2.99099718, 3.0167554, 3.04649716, 2.94116777]
cfg.preprocessor.params.normalize = normalization
# Disable config overwriting
OmegaConf.set_struct(cfg.preprocessor, True)
asr_model.preprocessor = asr_model.from_config_dict(cfg.preprocessor)
# Set model to inference mode
asr_model.eval();
asr_model = asr_model.to(asr_model.device)
from nemo.core.classes import IterableDataset
from nemo.core.neural_types import NeuralType, AudioSignal, LengthsType
import torch
from torch.utils.data import DataLoader
# simple data layer to pass audio signal
class AudioDataLayer(IterableDataset):
@property
def output_types(self):
return {
'audio_signal': NeuralType(('B', 'T'), AudioSignal(freq=self._sample_rate)),
'a_sig_length': NeuralType(tuple('B'), LengthsType()),
}
def __init__(self, sample_rate):
super().__init__()
self._sample_rate = sample_rate
self.output = True
def __iter__(self):
return self
def __next__(self):
if not self.output:
raise StopIteration
self.output = False
return torch.as_tensor(self.signal, dtype=torch.float32), \
torch.as_tensor(self.signal_shape, dtype=torch.int64)
def set_signal(self, signal):
self.signal = signal.astype(np.float32)/32768.
self.signal_shape = self.signal.size
self.output = True
def __len__(self):
return 1
data_layer = AudioDataLayer(sample_rate=cfg.preprocessor.params.sample_rate)
data_loader = DataLoader(data_layer, batch_size=1, collate_fn=data_layer.collate_fn)
# inference method for audio signal (single instance)
def infer_signal(model, signal):
data_layer.set_signal(signal)
batch = next(iter(data_loader))
audio_signal, audio_signal_len = batch
audio_signal, audio_signal_len = audio_signal.to(asr_model.device), audio_signal_len.to(asr_model.device)
log_probs, encoded_len, predictions = model.forward(
input_signal=audio_signal, input_signal_length=audio_signal_len
)
return log_probs
# class for streaming frame-based ASR
# 1) use reset() method to reset FrameASR's state
# 2) call transcribe(frame) to do ASR on
# contiguous signal's frames
class FrameASR:
def __init__(self, model_definition,
frame_len=2, frame_overlap=2.5,
offset=10):
'''
Args:
frame_len: frame's duration, seconds
frame_overlap: duration of overlaps before and after current frame, seconds
offset: number of symbols to drop for smooth streaming
'''
self.vocab = list(model_definition['labels'])
self.vocab.append('_')
self.sr = model_definition['sample_rate']
self.frame_len = frame_len
self.n_frame_len = int(frame_len * self.sr)
self.frame_overlap = frame_overlap
self.n_frame_overlap = int(frame_overlap * self.sr)
timestep_duration = model_definition['AudioToMelSpectrogramPreprocessor']['window_stride']
for block in model_definition['JasperEncoder']['jasper']:
timestep_duration *= block['stride'][0] ** block['repeat']
self.n_timesteps_overlap = int(frame_overlap / timestep_duration) - 2
self.buffer = np.zeros(shape=2*self.n_frame_overlap + self.n_frame_len,
dtype=np.float32)
self.offset = offset
self.reset()
def _decode(self, frame, offset=0):
assert len(frame)==self.n_frame_len
self.buffer[:-self.n_frame_len] = self.buffer[self.n_frame_len:]
self.buffer[-self.n_frame_len:] = frame
logits = infer_signal(asr_model, self.buffer).cpu().numpy()[0]
# print(logits.shape)
decoded = self._greedy_decoder(
logits[self.n_timesteps_overlap:-self.n_timesteps_overlap],
self.vocab
)
return decoded[:len(decoded)-offset]
@torch.no_grad()
def transcribe(self, frame=None, merge=True):
if frame is None:
frame = np.zeros(shape=self.n_frame_len, dtype=np.float32)
if len(frame) < self.n_frame_len:
frame = np.pad(frame, [0, self.n_frame_len - len(frame)], 'constant')
unmerged = self._decode(frame, self.offset)
if not merge:
return unmerged
return self.greedy_merge(unmerged)
def reset(self):
'''
Reset frame_history and decoder's state
'''
self.buffer=np.zeros(shape=self.buffer.shape, dtype=np.float32)
self.prev_char = ''
@staticmethod
def _greedy_decoder(logits, vocab):
s = ''
for i in range(logits.shape[0]):
s += vocab[np.argmax(logits[i])]
return s
def greedy_merge(self, s):
s_merged = ''
for i in range(len(s)):
if s[i] != self.prev_char:
self.prev_char = s[i]
if self.prev_char != '_':
s_merged += self.prev_char
return s_merged
# duration of signal frame, seconds
FRAME_LEN = 1.0
# number of audio channels (expect mono signal)
CHANNELS = 1
CHUNK_SIZE = int(FRAME_LEN*SAMPLE_RATE)
asr = FrameASR(model_definition = {
'sample_rate': SAMPLE_RATE,
'AudioToMelSpectrogramPreprocessor': cfg.preprocessor.params,
'JasperEncoder': cfg.encoder.params,
'labels': cfg.decoder.params.vocabulary
},
frame_len=FRAME_LEN, frame_overlap=2,
offset=4)
asr.reset()
p = pa.PyAudio()
#print('Available audio input devices:')
input_devices = []
for i in range(p.get_device_count()):
dev = p.get_device_info_by_index(i)
if dev.get('maxInputChannels'):
input_devices.append(i)
#print(i, dev.get('name'))
if len(input_devices):
dev_idx = -2
while dev_idx not in input_devices:
# print('Please type input device ID:')
dev_idx = 3 # 3 for virtal cable
empty_counter = 0
def callback(in_data, frame_count, time_info, status):
global empty_counter
signal = np.frombuffer(in_data, dtype=np.int16)
text = asr.transcribe(signal)
if len(text):
print(text,end='')
s.send(str.encode(text))
empty_counter = asr.offset
elif empty_counter > 0:
empty_counter -= 1
if empty_counter == 0:
print(' ',end='')
s.send(str.encode(' '))
return (in_data, pa.paContinue)
stream = p.open(format=pa.paInt16,
channels=CHANNELS,
rate=SAMPLE_RATE,
input=True,
input_device_index=dev_idx,
stream_callback=callback,
frames_per_buffer=CHUNK_SIZE)
print()
print('Listening...')
stream.start_stream()
# Interrupt kernel and then speak for a few more words to exit the pyaudio loop !
try:
while stream.is_active():
time.sleep(0.1)
finally:
stream.stop_stream()
stream.close()
p.terminate()
#print()
print("PyAudio stopped")
else:
print('ERROR: No audio input device found.')