谷歌语音转文本挂在响应= client.recognize(配置音频)

时间:2020-02-25 09:00:55

标签: python google-api speech-to-text google-speech-api google-speech-to-text-api

我有问题,我不知道该怎么办。我想用Google语音转文字来转录音频文件。当我执行python脚本时,一切都很好,但是代码挂在'Response = Client.recognize(config,audio)'上。没有错误消息。当我中断脚本时,我看到der Code挂在time.sleep上。奇怪的是,Python用注释模块'Google.cloud.speech_v1.types'用红色标记types.RecognitionAudio(Content=concent)types.Recognitionconfig没有'RecognitionAudio'/'RecognitionConfig成员。但是控制台不会引发关于红色标记为“类型”的错误。

有人知道我在做什么错吗?我很高兴获得支持。

代码:

import io
import os

# Imports the Google Cloud client library
from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types
from google.oauth2 import service_account
#from google.cloud import speech_v1

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = "./path/Key.json"
creds = service_account.Credentials.from_service_account_file('./path/Key.json')
print('Credentials from environ: {}'.format(os.environ.get('GOOGLE_APPLICATION_CREDENTIALS'))) 


# Instantiates a client
client = speech.SpeechClient()

# The name of the audio file to transcribe
file_name = os.path.join(
    os.path.dirname(__file__),
    'resources',
    'test.wav')

# Loads the audio into memory
with io.open(file_name, 'rb') as audio_file:
    content = audio_file.read()
    audio = types.RecognitionAudio(content=content)

config = types.RecognitionConfig(
    encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,
    sample_rate_hertz=16000,
    language_code='de-DE')

# Detects speech in the audio file
response = client.recognize(config, audio)
#response = client.recognize(config, audio)

for result in response.results:
    print('Transcript: {}'.format(result.alternatives[0].transcript))

控制台:

PS C:\Users\Twilio-Test> & C:/Users/Twilio-Test/AppData/Local/Programs/Python/Python38/python.exe c:/Users/Twilio-Test/Desktop/temp.py
Credentials from environ: ./path/Key.json
Traceback (most recent call last):
  File "c:/Users/Twilio-Test/Desktop/temp.py", line 36, in <module>
    response = client.recognize(config, audio)
  File "C:\Users\Twilio-Test\AppData\Local\Programs\Python\Python38\lib\site-packages\google\cloud\speech_v1\gapic\speech_client.py", line 255, in recognize
    return self._inner_api_calls["recognize"](
  File "C:\Users\Twilio-Test\AppData\Local\Programs\Python\Python38\lib\site-packages\google\api_core\gapic_v1\method.py", line 143, in __call__
    return wrapped_func(*args, **kwargs)
  File "C:\Users\Twilio-Test\AppData\Local\Programs\Python\Python38\lib\site-packages\google\api_core\retry.py", line 281, in retry_wrapped_func
    return retry_target(
  File "C:\Users\Twilio-Test\AppData\Local\Programs\Python\Python38\lib\site-packages\google\api_core\retry.py", line 215, in retry_target
    time.sleep(sleep)
KeyboardInterrupt

0 个答案:

没有答案