google的speech2srt python代码显示错误

2024-05-23 18:21:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从我的音频创建srt文件。事实上,为了这个,我正在跟踪这个tutorial。但是当我运行命令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.pyfile的链接


Tags: inpygsmainline音频runninglong
1条回答
网友
1楼 · 发布于 2024-05-23 18:21:52

出现这种情况的原因是,您提供了参数configaudio,这两个参数都应该是关键字参数。要解决此问题,可以将client.long_running_recognize(config, audio)替换为client.long_running_recognize(config = config, audio = audio)

您可以确认here,函数long\u running\u recognize的定义如下:

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)

相关问题 更多 >