Ruby Google语音到文本API未定义方法

Ruby Google语音到文本API未定义方法,ruby,api,speech-recognition,speech-to-text,google-cloud-speech,Ruby,Api,Speech Recognition,Speech To Text,Google Cloud Speech,我试图学习如何使用GoogleSpeech-to-text API,但在运行代码时遇到了一个未定义的方法错误。该代码取自google云客户端库以供使用。我不知道如何修正这个错误。 错误::nil:NilClass(NoMethodError)的未定义方法备选方案 我已经尝试复制您的问题,并且我能够成功地执行代码和来自的其他示例。此外,由于您表示正在学习如何使用谷歌的语音到文本API,我将描述我所采取的步骤 看来你已经把代码从。但是,您没有代码的第一行和最后一行,它们分别定义转录方法和执行它的调用

我试图学习如何使用GoogleSpeech-to-text API,但在运行代码时遇到了一个未定义的方法错误。该代码取自google云客户端库以供使用。我不知道如何修正这个错误。 错误::nil:NilClass(NoMethodError)的未定义方法备选方案


我已经尝试复制您的问题,并且我能够成功地执行代码和来自的其他示例。此外,由于您表示正在学习如何使用谷歌的语音到文本API,我将描述我所采取的步骤

看来你已经把代码从。但是,您没有代码的第一行和最后一行,它们分别定义转录方法和执行它的调用。因此,当您执行代码时,不会调用任何方法,也不会调用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
,它描述了从代码执行哪个方法。在上面的例子中,只有一个。但是,当代码中有其他方法可供调用时,在调用中使用参数非常有用

此外,我将描述为正确执行代码而采取的步骤。我已经按照描述的步骤进行了操作

  • 我已经从Cloud Shell运行了代码。但是,
    speech.googleapis.com
    不支持来自Cloud Shell的最终用户身份验证。因此,我将
    服务帐户令牌Creator
    从提供给了我的用户,因此我可以模拟服务帐户并调用API

  • 将您的
    project\u id
    导出到环境变量中,例如
    Export GOOGLE\u CLOUD\u project=“your-project-id”

  • 从文档中下载文件并运行命令:
    bundle install

  • 将代码复制到您的环境中

  • 将文件路径复制到变量
    audio\u file\u path
    并取消注释。请注意,将有许多
    audio\u file\u path
    局部变量,每个变量对应一个特定的方法。在我的例子中,我只将路径复制到第一个函数中的变量,
    audio\u file\u path=“home/alex/audio.wav”

  • 请注意,每个函数都有一个特定的配置参数(从第437行开始)。在我的例子中,我想使用
    recognize
    one

  • 使用
    bundle exec ruby speech\u samples.rb recognize
    运行示例代码

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

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

  • 更新:

    正如我在评论部分提到的,我上面共享的代码是文件
    speech\u samples.rb
    的一部分,该文件包含调用speech-To-Text API的各种函数示例。在我的例子中,我只是使用了我粘贴在上面的部分样本

    关于您共享的链接,代码的GitHub源repo有一个按钮,与我使用的示例中的按钮相同。请注意,GitHub中的代码被包装在函数中。此外,为了执行该函数,应该调用该函数,您只需将其名称写在函数定义之后即可调用它。因此,代码应如下所示:

    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
    

    注意:我必须指出,我也能够执行您问题中的代码,而无需包装在函数中。我按照中描述的步骤操作,它成功地检索了输出。

    没有第一个结果-这意味着没有结果。这可能不是一个错误;也许它只是无法从你的音频文件中获取任何信息。是否有一个演示音频文件,你可以使用它,你知道谷歌可以从中获得结果?也许你已经遵循了这个文档()。您是否也使用了提供的示例文件?还是你用了另一个?我试图在GCP控制台中运行此代码,但没有遇到错误。我可以知道您是否在其他环境中运行了此代码吗?@TomHarvey我使用了一个音频文件,该文件是我使用google文本到语音API创建的,并使用了其文件路径。我也尝试了我自己的录音。@RallyH您刚才是不是从文档中复制粘贴了提供的代码并运行了它?我做了同样的事情,但我把它复制到vs代码中,并使用终端运行它。我也有我自己的语音记录文件,我用它来记录路径。我想我可以只使用给出的代码,因为它与文本到语音版本一起工作。为什么需要使用您提供的示例中的400多行代码,而不是我从中获取的链接中的代码?我在谷歌的快速入门指南中搜索时没有看到这一点,所以我认为给出的示例代码很好。谢谢@Nam,您不需要使用400多行代码。在这段代码中,你可以使用它的任何部分,因为我只是使用了我在答案中复制的部分。此外,当在GitHub上单击视图时,您共享的链接中的代码将转到同一GitHub repo。然后,您可以检查它包装在函数中的代码,在函数定义之后,必须调用它才能执行。但是,我可以使用来自GitHub和您共享的链接的代码(无需将其包装到函数中)。请确保您已遵循文档中描述的所有步骤。另外,您使用哪个命令来执行代码?我已经用GITHUB的代码更新了答案,你也可以尝试一下,告诉我它是否起作用了?@ Nam,请考虑接受和投票的答案,以防你发现这些信息有用。
    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