forked from dzubke/speech
-
Notifications
You must be signed in to change notification settings - Fork 0
/
non_streaming.py
71 lines (57 loc) · 2.4 KB
/
non_streaming.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
import argparse
import speech
import torch
import shlex
import subprocess
import numpy as np
import soundfile
from speech.loader import log_specgram
import pickle
# conducts inference on audio with just recording audio from a microphone without streaming.
def predict_from_stream(model_path: str):
"""This function takes in a path to a pytorch model and prints predictions of the model from live streaming
audio from a computer microphone.
"""
# the rec command: -q=quiet mode, -V0=volume factor of 0, -e signed=a signed integer encoding
## -L=endian little, -c 1=one channel, -b 16=16 bit sample size, -r 16k=16kHZ sampele rate
## -t raw=raw file type , - gain -2=
args = 'rec -q -V0 -e signed -L -c 1 -b 16 -r 16k -t raw - gain -2'
subproc = subprocess.Popen(shlex.split(args),
stdout=subprocess.PIPE,
bufsize=0)
model, preproc = speech.load(model_path, tag='')
num_buffers = 250
all_preds=[]
try:
while True:
print('You can start speaking now. Press Control-C to stop recording.')
data_list = []
for _ in range(num_buffers):
data = subproc.stdout.read(512)
data_list.append(data)
np_array = np.array([], dtype=np.int16)
for data in data_list:
np_data = np.frombuffer(data, dtype=np.int16)
np_array = np.append(np_array, np_data)
log_spec = log_specgram(np_array, sample_rate=16000)
norm_log_spec = (log_spec - preproc.mean) / preproc.std
fake_label = [27]
dummy_batch = ((norm_log_spec,), (fake_label,)) # model.infer expects 2-element tuple
preds = model.infer(dummy_batch)
preds = [preproc.decode(pred) for pred in preds]
print(preds)
all_preds.extend(preds)
except KeyboardInterrupt:
#soundfile.write('new_file.wav', np_array, 16000)
print('All predictions:', all_preds)
subproc.terminate()
subproc.wait()
if __name__ == "__main__":
### format of script command
# python streaming.py <path_to_model>
parser = argparse.ArgumentParser(
description="Will provide streaming predictions from model.")
parser.add_argument("model",
help="Path to the pytorch model.")
args = parser.parse_args()
predict_from_stream(args.model)