[英]Attribute error in python with Google Speech API
'str'對象沒有屬性'long_running_recognize'。 此錯誤發生在顯示的代碼的最后一行。 我跳進代碼,看不出它失敗的明顯原因。 跳出下面的wrap_method。
if "long_running_recognize" not in self._inner_api_calls:
self._inner_api_calls[
"long_running_recognize"
] = google.api_core.gapic_v1.method.wrap_method(
self.transport.long_running_recognize,
default_retry=self._method_configs["LongRunningRecognize"].retry,
default_timeout=self._method_configs["LongRunningRecognize"].timeout,
client_info=self._client_info,
)
我能想到的唯一問題是音頻文件太大(45mb ... 53分鍾)。 但代碼中斷發生在一個似乎與此無關的位置。
import io
import os
from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types
with open("path\\My First Project-edb18653fedf.json") as f:
GOOGLE_APPLICATION_CREDENTIALS = f.read()
client = speech.SpeechClient(GOOGLE_APPLICATION_CREDENTIALS)
file_path = "audio\\102.flac"
with io.open(file_path, 'rb') as audio_file:
content = audio_file.read()
audio = types.RecognitionAudio(content=content)
config = types.RecognitionConfig(
encoding=enums.RecognitionConfig.AudioEncoding.FLAC,
sample_rate_hertz=32000,
language_code='en-US')
operation = client.long_running_recognize(config, audio)
Connected to pydev debugger (build 191.7479.30)
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.2\helpers\pydev\pydevd.py", line 1758, in <module>
main()
File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.2\helpers\pydev\pydevd.py", line 1752, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.2\helpers\pydev\pydevd.py", line 1147, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.2\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/shear/Documents/PythonScripts/GStTEnv/SpeechToText/CloudSpeechText.py", line 105, in <module>
operation = client.long_running_recognize(config, audio)
File "C:\Users\shear\GStTEnv\lib\site-packages\google\cloud\speech_v1\gapic\speech_client.py", line 314, in long_running_recognize
self.transport.long_running_recognize,
AttributeError: 'str' object has no attribute 'long_running_recognize'
您是否試圖證明您關於音頻長度的理論? 如果您修剪音頻並嘗試它會發生什么?
如果您在命令行而不是代碼中嘗試卷曲請求,是否會發生?
我想看看我是否可以提供幫助。 要進一步診斷,您能否提供: - 重現的確切步驟 - 指向github repo(如果可用) - 盡可能多的代碼 - 精確錯誤消息 - 語言代碼(config) - 適用的所有配置選項 - 示例移除所有個人數據的音頻文件(在Google雲端硬盤或雲存儲上)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.