【问题标题】:Google Speech to Text API Undefined MethodGoogle Speech to Text API 未定义方法
【发布时间】:2020-10-18 16:29:48
【问题描述】:

我正在尝试学习如何使用谷歌语音转文本 API,但在运行代码时遇到了未定义的方法错误。代码取自谷歌云客户端库使用。我不确定如何解决该错误。 错误: : nil:NilClass (NoMethodError) 的未定义方法替代

#use gem install google-cloud-speech

require "google/cloud/speech"


speech = Google::Cloud::Speech.speech


file_name = "file_path"


audio_file = File.binread file_name


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

results.first.alternatives.each do |alternatives|
puts "Transcription: #{alternatives.transcript}"
  end

【问题讨论】:

  • 没有第一个结果 - 这意味着没有结果。这可能不是错误;也许它只是无法从您的音频文件中获取任何内容。是否有您可以使用的演示音频文件,您知道 Google 可以从中获取结果?
  • 也许您已遵循此文档 (cloud.google.com/speech-to-text/docs/…)。您是否也使用了提供的示例文件?还是您使用了其他的?我尝试在 GCP 控制台中运行此代码,但没有遇到错误。我想知道您是否在不同的环境中运行此代码?
  • @TomHarvey 我使用了我使用谷歌文本到语音 API 创建的音频文件,并使用了它的文件路径。我也尝试了自己的录音。
  • @RallyH 您是否只是从该文档中复制粘贴提供的代码并运行它?我做了同样的事情,但我将它复制到 vs 代码中并使用终端运行它。我也有自己的录音文件,用于路径。

标签: ruby api speech-recognition speech-to-text google-cloud-speech


【解决方案1】:

我已尝试复制您的问题,并且能够成功执行来自 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 描述的步骤进行操作,

  1. 我已经从 Cloud Shell 运行了代码。但是,speech.googleapis.com 不支持来自 Cloud Shell 的最终用户身份验证。出于这个原因,我已将来自IAM ConsoleService Account Token Creator 提供给我的用户,这样我就可以模拟一个服务帐户并调用API。

  2. 将您的project_id 导出到环境变量,例如export GOOGLE_CLOUD_PROJECT="YOUR-PROJECT-ID"

  3. 从文档中下载Gemfile文件并运行命令:bundle install

  4. 将代码speech_samples.rb 复制到您的环境中。

  5. 将文件路径复制到变量audio_file_path 并取消注释。请注意,将有许多audio_file_path 局部变量,每个变量用于特定方法。就我而言,我只将路径复制到第一个函数中的变量 audio_file_path = "home/alex/audio.wav"

  6. 注意每个函数都有一个特定的配置参数(从第 437 行开始)。就我而言,我想使用 recognize 之一。

  7. 使用bundle exec ruby speech_samples.rb recognize 运行示例代码。

  8. 检查您的环境中是否有一个名为 results 的新目录。然后检查输出。

  9. 请不要忘记将您的文件路径复制到您将使​​用正确参数调用的所有函数。

更新:

正如我在评论部分提到的,我上面分享的代码是文件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 中描述的步骤操作并成功检索到输出。

【讨论】:

  • 我从here 获取了代码。我以为我可以使用给出的代码,因为它适用于文本到语音版本。为什么有必要使用您提供的示例中的 400 多行代码,而不是我从中获取的链接中的代码?在谷歌的快速入门指南中搜索时我没有看到这一点,所以我认为给出的示例代码很好。谢谢!
  • @Nam,您不需要使用 400+ 行代码。在此代码中,您可以使用它的任何部分,因为我只是使用了我在答案中复制的部分。此外,当单击在 GitHub 上查看时,您共享的链接中的代码会转到同一个 GitHub 存储库。然后,您可以检查它包装在函数中的代码,并且在函数定义之后,必须调用它才能执行。
  • 但是,我能够使用来自 GitHub 的代码以及您共享的链接(没有将其包装到函数中)。请确保您已遵循文档中描述的所有步骤。另外,您使用哪个命令来执行代码?我已经用 GitHub 上的代码更新了答案,你也可以试试,告诉我它是否有效?
  • @Nam,如果您发现这些信息有用,请考虑接受和投票。
猜你喜欢
  • 2019-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多