flashlight / wav2letter

Facebook AI Research's Automatic Speech Recognition Toolkit
https://github.com/facebookresearch/wav2letter/wiki
Other
6.37k stars 1.01k forks source link

Run "interactive_streaming_asr_example" with python and save to file in v0.2 #931

Closed a-doering closed 3 years ago

a-doering commented 3 years ago

Question

I am trying to follow the documentation for interactive streaming asr with python to write output to a file. I can have the output printed to the terminal without problems, but when writing to a file, it doesn't kill the process (when I manually kill the process with ctrl + c it does save the output). It seems to be stuck in the line output = w2l_process.stdout.readline() according to the message when killing it. How can I kill the process and save the output to file?

Setup

I am running this in the inference docker container, python version = 3.6.12 . The model and audio files are also in the container.

Code

# same imports, some path setups

w2l_bin = '/root/wav2letter/build/inference/inference/examples/interactive_streaming_asr_example'
2l_process = Popen(['{} --input_files_base_path={}'.format(w2l_bin, model_path)],
                    stdin=PIPE, stdout=PIPE, stderr=PIPE,
                    shell=True)

w2l_process.stdin.write("output=/root/celery_workers/transcript.txt\n".encode())
w2l_process.stdin.write("endtoken=DONE\n".encode())
w2l_process.stdin.write("input={}\n".format(audio_wav_path).encode())
w2l_process.stdin.flush()
while True:
    # Read from process stdout
    output = w2l_process.stdout.readline()

    if output == b'#finish transcribing\n':
        break
    else:
        print(output)
# Finish the process
os.killpg(os.getpgid(w2l_process.pid), signal.SIGTERM)

Terminal Output

Terminal output with ctrl + c kill message.

b'Started features model file loading ... \n'
b'Completed features model file loading elapsed time=1901 microseconds\n'
b'\n'
b'Started acoustic model file loading ... \n'
b'Completed acoustic model file loading elapsed time=3569 milliseconds\n'
b'\n'
b'Started tokens file loading ... \n'
b'Completed tokens file loading elapsed time=783 microseconds\n'
b'\n'
b'Tokens loaded - 9998 tokens\n'
b'Started decoder options file loading ... \n'
b'Completed decoder options file loading elapsed time=45 microseconds\n'
b'\n'
b'Started create decoder ... \n'
b'Completed create decoder elapsed time=1411 milliseconds\n'
b'\n'
b"Entering interactive command line shell. enter '?' for help.\n"
b'------------------------------------------------------------\n'
b'$>Redirecting trascription output to:/root/celery_workers/transcript.txt\n'
b'$>End of trascription token=DONE\n'

^CTraceback (most recent call last):
  File "test_asr.py", line 36, in <module>
    output = w2l_process.stdout.readline()
KeyboardInterrupt

Thanks in advance for any hints and thanks for providing this repo.

tlikhomanenko commented 3 years ago

Do you provide audio_wav_path?

a-doering commented 3 years ago

Yes, I provide the path to a .wav file. The file is sampled correctly and can be accessed. The script above worked for printing to terminal, but with these added changes I was unable to write to file. I managed to make it work another way (see below) but am still very interested in what went wrong previously. If you do not have an idea, I think this issue can be closed. Thanks for the answer :)

import sys
# My path setup, imports

w2l_bin = '/root/wav2letter/build/inference/inference/examples/interactive_streaming_asr_example'
2l_process = Popen(['{} --input_files_base_path={}'.format(w2l_bin, model_path)],
                    stdin=PIPE, stdout=PIPE, stderr=PIPE,
                    shell=True)

with file('path/to/file.txt') as f:
    sys.stdout = f
    # Now all the print statements go to file 
    # and I can use the inference code for printing to terminal
    # that is now redirected to file.
    w2l_process.stdin.write("input={}\n".format(audio_wav_path).encode())
    w2l_process.stdin.flush()
    # Rest of the process as before
tlikhomanenko commented 3 years ago

So in the code above (original code) you cannot open file and instead of print(output) do f.write(output)?

PS. Your posted solution looks good to me. Probably you can directly set it to file with configuring the Popen stdout.

a-doering commented 3 years ago

In the original code from the documentation I cannot write to file (no errors, I just don't know where it writes to). The one with f.write(output) works for me.

tlikhomanenko commented 3 years ago

From this example https://github.com/facebookresearch/wav2letter/wiki/Inference-Run-Examples#how-we-can-i-use-this-from-python it should not write into file, it will write into standard output.