我正在尝试从我的音频创建srt文件。事实上,我正在遵循这个教程。但当我运行命令python3 speech2srt.py --storage_uri gs://subtitlingsc/en.wav
时,它显示以下错误:
Transcribing gs://subtitlingsc/en.wav ...
Traceback (most recent call last):
File "speech2srt.py", line 152, in <module>
main()
File "speech2srt.py", line 146, in main
subs = long_running_recognize(args)
File "speech2srt.py", line 44, in long_running_recognize
operation = client.long_running_recognize(config, audio)
TypeError: long_running_recognize() takes from 1 to 2 positional arguments but 3 were given
如果有人能帮忙解决这个问题!
speech2srt.py文件的链接。
之所以会发生这种情况,是因为您提供的参数config和audio都应该是关键字参数。为了解决这个问题,可以将client.long_running_recognize(config, audio)
替换为client.long_running_recognize(config = config, audio = audio)
正如您可以在这里确认的那样,函数long_running_accept具有以下定义:
async long_running_recognize(
request: google.cloud.speech_v1.types.cloud_speech.LongRunningRecognizeRequest = None,
*,
config: google.cloud.speech_v1.types.cloud_speech.RecognitionConfig = None,
audio: google.cloud.speech_v1.types.cloud_speech.RecognitionAudio = None,
retry: google.api_core.retry.Retry = <object object>,
timeout: float = None,
metadata: Sequence[Tuple[str, str]] = ()
)
在Python中,带有参数(arg1、arg2、*、arg3、agr4…(的函数定义意味着只有*之前的参数才能作为位置参数提供。在这种情况下,*之后的所有参数都应作为关键字参数提供。
举个例子,让我们用我提到的参数创建一个函数:
def function(arg1, arg2, * , arg3, agr4):
pass
如果我尝试将此函数调用为function(1,2,3,4)
,它将失败,因为只需要两个位置参数。为了正确调用它,我应该提供两个位置参数和两个关键字参数,因此正确的调用应该是function(1, 2, arg3 = 3, arg4 = 4)