首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google Speech to Text API未定义方法

Google Speech to Text API未定义方法
EN

Stack Overflow用户
提问于 2020-10-19 00:29:48
回答 1查看 190关注 0票数 1

我正在尝试学习如何使用google speech to text API,但在运行代码时遇到未定义的方法错误。代码取自google云客户端库以供使用。我不确定如何修复这个错误。错误::未定义nil:NilClass (NoMethodError)的替代方法

代码语言:javascript
复制
#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
EN

回答 1

Stack Overflow用户

发布于 2020-10-20 19:52:02

我已经尝试复制您的问题,并且能够成功地执行documentation中的代码和其他示例。此外,由于您表示您正在学习如何使用Google的Speech to Text API,因此我将介绍我采取的步骤。

看起来您已经从documentation中获取了代码。但是,您没有代码的第一行和最后几行,它们分别定义转录方法和执行该方法的调用。因此,在执行代码时,不会调用任何方法,也不会调用Speech- to -Text API。您的代码应如下所示:

代码语言:javascript
复制
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

为了运行样本,

代码语言:javascript
复制
bundle exec ruby speech_samples.rb

请注意参数recognize,它描述了从代码中执行哪个方法。在上面的例子中,只有一个。但是,当代码中有其他方法可供调用时,在调用中使用参数是非常有用的。

此外,我将描述我为正确执行代码而采取的步骤。我遵循了here描述的步骤,

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

  1. project_id导出到环境变量,如export GOOGLE_CLOUD_PROJECT="YOUR-PROJECT-ID".

从文档下载Gemfile文件并运行命令:

  1. 将代码speech_samples.rb复制到您的environment.

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

中的变量

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

使用bundle exec ruby speech_samples.rb recognize.运行

  1. 示例代码

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

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

更新:

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

关于您共享的链接,有一个指向代码的GitHub源代码库的按钮,该按钮与我使用的示例中的按钮相同。注意,您在GitHub中的代码here,它被包装在一个函数中。此外,此函数应该被调用才能执行,您可以通过在函数定义后简单地写入它的名称来调用它。因此,代码应该如下所示:

代码语言:javascript
复制
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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64415670

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档