2

I run the DeepSpeech command for wav to text and want to save the result into a text file like so:

deepspeech --model path/to/model --audio path/to/audio >> path/to/result.txt

It works in the terminal but if I try to run it as a subprocess in Python like that:

subprocess.run(["deepspeech", "--model", "path/to/model", "--audio", "path/to/audio", ">>", "path/to/result.txt"])

I get this:

usage: deepspeech [-h] --model MODEL [--lm [LM]] [--trie [TRIE]] --audio AUDIO
              [--beam_width BEAM_WIDTH] [--lm_alpha LM_ALPHA]
              [--lm_beta LM_BETA] [--version] [--extended] [--json]
deepspeech: error: unrecognized arguments: >> path/to/result.txt

Is there a fix for that?

Lady_Hangaku
  • 97
  • 1
  • 9

2 Answers2

1
>> path/to/result.txt

This is not an argument to deepspeech. This is a shell feature called "output redirection". deepspeech obviously does not understand that.

To get the same behaviour in subprocess you can use the stdout option:

subprocess.run(["deepspeech", "--model", "path/to/model", "--audio", "path/to/audio"] , stdout=open("path/to/result.txt", 'a')])

For more info see the Popen class

stdin, stdout and stderr specify the executed program’s standard input, standard output and standard error file handles, respectively. Valid values are PIPE, DEVNULL, an existing file descriptor (a positive integer), an existing file object, and None. PIPE indicates that a new pipe to the child should be created. DEVNULL indicates that the special file os.devnull will be used. With the default settings of None, no redirection will occur; the child’s file handles will be inherited from the parent. Additionally, stderr can be STDOUT, which indicates that the stderr data from the applications should be captured into the same file handle as for stdout.

rdas
  • 20,604
  • 6
  • 33
  • 46
1

I think you can do:

with open("path/to/result.txt", mode="wb" as fd:
    subprocess.run(["deepspeech", "--model", "path/to/model", "--audio", "path/to/audio"], stdout=fd)

Or capture the output.

Read the documentation of subprocess.run.

Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103
  • Did it exactly like that, worked perfectly. Thank you! – Lady_Hangaku Apr 12 '20 at 12:22
  • @Laurent LAPORTE : I was trying to run this command `with open("C:/Users/hp/Documents/result.txt", mode="wb") as fd: subprocess.run(["deepspeech", "--model", "--model C:/deepspeechwk/deepspeech-0.6.0-models/output_graph.pb --lm C:/deepspeechwk/deepspeech-0.6.0-models/lm.binary --trie C:/deepspeechwk/deepspeech-0.6.0-models/trie", "--audio", "C:/deepspeechwk/audio/8455-210777-0068.wav"], stdout=fd)` but the file which is generated is blank but the same is working when i run from terminal Can you please let me know what's the issue – user3653474 Nov 01 '21 at 14:28