我已尝试复制您的问题,并且能够成功执行来自 documentation 的代码和其他示例。另外,既然您说您正在学习如何使用 Google 的 Speech to Text API,我将描述我所采取的步骤。
您似乎已从documentation 获取代码。但是,您没有代码的第一行和最后几行,它们分别定义了转录方法和执行它的调用。因此,当您执行代码时,不会调用任何方法,也不会调用 Speech-to-Text API。您的代码应如下所示:
def speech_sync_recognize audio_file_path: nil
# [START speech_transcribe_sync]
# audio_file_path = "Path to file on which to perform speech recognition"
require "google/cloud/speech"
speech = Google::Cloud::Speech.speech
# [START speech_ruby_migration_sync_response]
audio_file = File.binread audio_file_path
config = { encoding: :LINEAR16,
sample_rate_hertz: 16_000,
language_code: "en-US" }
audio = { content: audio_file }
response = speech.recognize config: config, audio: audio
results = response.results
alternatives = results.first.alternatives
alternatives.each do |alternative|
puts "Transcription: #{alternative.transcript}"
end
# [END speech_ruby_migration_sync_response]
# [END speech_transcribe_sync]
end
if $PROGRAM_NAME == __FILE__
command = ARGV.shift
#I have added this part in order to use a command after to define which method to call within the code.
case command
when "recognize"
speech_sync_recognize audio_file_path: ARGV.first
end
end
为了运行示例,
bundle exec ruby speech_samples.rb
注意参数recognize,它描述了从代码中执行的方法。在上述情况下,只有一个。但是,当代码中有其他方法可供调用时,在调用中使用参数非常有用。
此外,我将描述我为正确执行代码而采取的步骤。我已按照here 描述的步骤进行操作,
-
我已经从 Cloud Shell 运行了代码。但是,speech.googleapis.com 不支持来自 Cloud Shell 的最终用户身份验证。出于这个原因,我已将来自IAM Console 的Service Account Token Creator 提供给我的用户,这样我就可以模拟一个服务帐户并调用API。
-
将您的project_id 导出到环境变量,例如export GOOGLE_CLOUD_PROJECT="YOUR-PROJECT-ID"。
-
从文档中下载Gemfile文件并运行命令:bundle install
-
将代码speech_samples.rb 复制到您的环境中。
-
将文件路径复制到变量audio_file_path 并取消注释。请注意,将有许多audio_file_path 局部变量,每个变量用于特定方法。就我而言,我只将路径复制到第一个函数中的变量 audio_file_path = "home/alex/audio.wav"。
-
注意每个函数都有一个特定的配置参数(从第 437 行开始)。就我而言,我想使用 recognize 之一。
-
使用bundle exec ruby speech_samples.rb recognize 运行示例代码。
-
检查您的环境中是否有一个名为 results 的新目录。然后检查输出。
-
请不要忘记将您的文件路径复制到您将使用正确参数调用的所有函数。
更新:
正如我在评论部分提到的,我上面分享的代码是文件speech_samples.rb 的一部分,其中包含调用 Speech-To-Text API 的各种函数示例。就我而言,我只是使用了上面粘贴的部分示例。
关于您分享的链接,代码的 GitHub 源代码库有一个按钮,与我使用的示例相同。请注意,您在 GitHub 中的代码here,它被包装在一个函数中。另外,这个函数应该被调用才能被执行,你可以通过在函数定义后简单地写它的名字来调用它。因此代码应该如下所示,
def quickstart
# [START speech_quickstart]
# Imports the Google Cloud client library
# [START speech_ruby_migration_import]
require "google/cloud/speech"
# [END speech_ruby_migration_import]
# Instantiates a client
# [START speech_ruby_migration_client]
speech = Google::Cloud::Speech.speech
# [END speech_ruby_migration_client]
# The name of the audio file to transcribe
file_name = "./resources/brooklyn_bridge.raw"
# [START speech_ruby_migration_sync_request]
# [START speech_ruby_migration_config]
# The raw audio
audio_file = File.binread file_name
# The audio file's encoding and sample rate
config = { encoding: :LINEAR16,
sample_rate_hertz: 16_000,
language_code: "en-US" }
audio = { content: audio_file }
# Detects speech in the audio file
response = speech.recognize config: config, audio: audio
# [END speech_ruby_migration_config]
results = response.results
# [END speech_ruby_migration_sync_request]
# Get first result because we only processed a single audio file
# Each result represents a consecutive portion of the audio
results.first.alternatives.each do |alternatives|
puts "Transcription: #{alternatives.transcript}"
end
# [END speech_quickstart]
end
#call the function defined above
quickstart
注意:我必须指出,我也能够执行您问题中的代码,而无需包装在函数中。我按照documentation 中描述的步骤操作并成功检索到输出。