-
Notifications
You must be signed in to change notification settings - Fork 0
/
runmodel.py
48 lines (43 loc) · 1.73 KB
/
runmodel.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
import os
import signal
import re
from subprocess import Popen, PIPE
class runmodel():
def run(model_path,w2l_bin,path_to_audio_file):
with open(path_to_audio_file, 'rb', 0) as a:
w2l_process = Popen(['{} --input_files_base_path={}'.format(w2l_bin, model_path)],
stdin=a, stdout=PIPE, stderr=PIPE,
shell=True)
# write to the stdin of the process
# make sure to flush and add \n to the string
#print(("input=%b\n" % path_to_audio_file).encode())
#w2l_process.stdin.write("input=%b\n".encode() % path_to_audio_file)
#w2l_process.stdin.flush()
#output = bytearray()
output = str()
while True:
# read from process stdout
intermediate_output = w2l_process.stdout.readline()
match = re.search('[0-9]{1,6}',intermediate_output.decode("utf-8"))
if match:
split_array = re.split(',' , intermediate_output.decode("utf-8"))
else:
split_array = []
if b'Completed converting audio input from stdin to text' in intermediate_output :#output == b'#finish transcribing\n':
# finish transcribing an audio
break
else:
if len(split_array) > 1:
#output.extend(split_array[2])
output+= split_array[2]
print(split_array[2])
print(intermediate_output)
return output
# finish the process
#os.killpg(os.getpgid(w2l_process.pid), signal.SIGTERM)
if __name__ == '__main__':
model_path = "/home/model/"
w2l_bin = "/root/wav2letter/build/inference/inference/examples/simple_streaming_asr_example"
path_to_audio_file = '/home/wav2letterInference/numbersAudioGirl.wav'
output = runmodel.run(model_path,w2l_bin,path_to_audio_file)
print(output)