Google 语音转文本 Python 示例代码不起作用
Google speech-to-text Python example code doesn't work
以下是我的代码(我对原来的示例代码做了一些小改动):
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。但是,当我 运行 代码时,什么也没有发生。我执行了所有步骤,我可以在我的控制台上看到请求流量发生了变化。但是我看不到任何成绩单。有人可以帮帮我吗?
您正在尝试解码编码为 MP3 的 TOEFL2.mp3 文件,同时您使用
指定 LINEAR 音频编码
encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16
您必须先将 mp3 转换为 wav,请参阅 information about AudioEncoding
以下是我的代码(我对原来的示例代码做了一些小改动):
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。但是,当我 运行 代码时,什么也没有发生。我执行了所有步骤,我可以在我的控制台上看到请求流量发生了变化。但是我看不到任何成绩单。有人可以帮帮我吗?
您正在尝试解码编码为 MP3 的 TOEFL2.mp3 文件,同时您使用
指定 LINEAR 音频编码encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16
您必须先将 mp3 转换为 wav,请参阅 information about AudioEncoding