Skip to content
This repository was archived by the owner on Aug 3, 2021. It is now read-only.

Commit 56ef170

Browse files
committed
Added command-line demo for streaming ASR
Signed-off-by: Vitaly Lavrukhin <[email protected]>
1 parent 1f84eec commit 56ef170

File tree

3 files changed

+60
-8
lines changed

3 files changed

+60
-8
lines changed

demo_streaming_asr.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from frame_asr import FrameASR
2+
import numpy as np
3+
import pyaudio as pa
4+
import time
5+
6+
CHANNELS = 1
7+
RATE = 16000
8+
DURATION = 2.0
9+
CHUNK_SIZE = int(DURATION*RATE)
10+
11+
p = pa.PyAudio()
12+
13+
print('Available audio input devices:')
14+
for i in range(p.get_device_count()):
15+
dev = p.get_device_info_by_index(i)
16+
if dev.get('maxInputChannels'):
17+
print(i, dev.get('name'))
18+
print('Please type input device ID:')
19+
dev_idx = int(input())
20+
21+
22+
asr = FrameASR()
23+
print('Initialization was successful')
24+
25+
26+
def callback(in_data, frame_count, time_info, status):
27+
signal = np.frombuffer(in_data, dtype=np.int16)
28+
pred = asr.transcribe(signal)
29+
if len(pred.strip()):
30+
print('"{}"'.format(pred))
31+
return (in_data, pa.paContinue)
32+
33+
34+
stream = p.open(format=pa.paInt16,
35+
channels=CHANNELS,
36+
rate=RATE,
37+
input=True,
38+
input_device_index=dev_idx,
39+
stream_callback=callback,
40+
frames_per_buffer=CHUNK_SIZE)
41+
42+
stream.start_stream()
43+
44+
while stream.is_active():
45+
time.sleep(0.1)
46+
47+
stream.stop_stream()
48+
stream.close()
49+
p.terminate()
50+

frame_asr.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
create_model, get_interactive_infer_results
1212

1313
# Define the command line arguments that one would pass to run.py here
14-
MODEL_PARAMS = ["--config_file=data/jasper10x5-dr-librosa-novograd-speed/config_infer.py",
14+
MODEL_PARAMS = ["--config_file=models/Jasper-Mini-for-Jetson/config_infer_stream.py",
1515
"--mode=interactive_infer",
16-
"--logdir=data/jasper10x5-dr-librosa-novograd-speed/checkpoint/",
16+
"--logdir=models/Jasper-Mini-for-Jetson/",
1717
"--batch_size_per_gpu=1",
1818
"--num_gpus=1",
1919
"--use_horovod=False",
@@ -50,7 +50,7 @@ def __init__(self, model_params=MODEL_PARAMS, scope_name='S2T',
5050
saver_S2T.restore(self.sess, checkpoint_S2T)
5151

5252
self.vocab = self._load_vocab(
53-
self.model_S2T.params['decoder_params']['alphabet_config_path']
53+
self.model_S2T.params['data_layer_params']['vocab_file']
5454
)
5555
self.sr = sr
5656
self.frame_len = frame_len
@@ -59,7 +59,8 @@ def __init__(self, model_params=MODEL_PARAMS, scope_name='S2T',
5959
self.n_frame_overlap = int(frame_overlap * sr)
6060
self.n_timesteps_overlap = int(frame_overlap / timestep_duration) - 2
6161
self.buffer = np.zeros(shape=2*self.n_frame_overlap + self.n_frame_len, dtype=np.float32)
62-
self._calibrate_offset()
62+
# self._calibrate_offset()
63+
self.offser = 5
6364
self.reset()
6465

6566

@@ -86,11 +87,11 @@ def transcribe(self, frame=None, merge=True):
8687
return self.greedy_merge(unmerged)
8788

8889

89-
def _calibrate_offset(self, max_offset=10, n_calib_inter=10):
90+
def _calibrate_offset(self, wav_file, max_offset=10, n_calib_inter=10):
9091
'''
9192
Calibrate offset for frame-by-frame decoding
9293
'''
93-
sr, signal = wave.read('data/gtc2019_keynote_16kHz.wav')
94+
sr, signal = wave.read(wav_file)
9495

9596
# warmup
9697
n_warmup = 1 + int(np.ceil(2.0 * self.frame_overlap / self.frame_len))
@@ -117,7 +118,8 @@ def reset(self):
117118
'''
118119
self.buffer=np.zeros(shape=self.buffer.shape, dtype=np.float32)
119120
self.prev_char = ''
120-
121+
122+
121123
@staticmethod
122124
def _get_model(args, scope):
123125
'''

scripts/run_all_tests.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env bash
22
set -e
33
# This will take quite some time
4-
pip install -r requirements.txt
4+
# pip install -r requirements.txt
55
echo '**********>>>> CREATE TOY DATA <<<< ************'
66
scripts/create_toy_data.sh
77
echo '**********>>>> RUNNING UNIT TESTS <<<< ************'

0 commit comments

Comments
 (0)