Google语音文本Python示例代码不起作用

时间:2018-10-16 04:20:12

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

以下是我的代码(我对原始示例代码做了一些小的更改):

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

# Instantiates a client
client = speech.SpeechClient()

# The name of the audio file to transcribe
file_name = os.path.join(
    os.path.dirname(__file__),
    'C:\\Users\\louie\\Desktop',
    'TOEFL2.mp3')

# 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='en-US')

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

for result in response.results:
    print('Transcript: {}'.format(result.alternatives[0].transcript))
    text_file = open("C:\\Users\\louie\\Desktop\\Output.txt", "w")
    text_file.write('Transcript: {}'.format(result.alternatives[0].transcript))
    text_file.close()

我只能在Windows提示符命令中直接运行此代码,因为否则,系统将无法识别GOOGLE_APPLICATION_CREDENTIALS。但是,当我运行代码时,什么也没发生。我按照所有步骤操作,可以在控制台上看到请求流量的变化。但是我看不到任何成绩单。有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

您尝试使用指定线性音频编码的同时解码编码为MP3的TOEFL2.mp3文件

encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16

您必须先将mp3转换为wav,请参见information about AudioEncoding