ruby Google语音识别API结果为空

时间:2017-02-27 19:15:01

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

我正在测试Google语音识别API,但我没有结果。

脚本

# Imports the Google Cloud client library
require "google/cloud/speech"

# Instantiates a client
speech = Google::Cloud::Speech.new 

# The name of the audio file to transcribe

file_name = "audio.raw"
puts "audio_file_path:\n ------- #{file_name}   --------------------\n" 

# The audio file's encoding and sample rate
audio = speech.audio file_name, encoding: :raw, sample_rate: 16000
puts "audio: #{audio}\n"

# Detects speech in the audio file
results = audio.recognize
result  = results.first
puts "results: #{results}\n"
puts "Transcription: #{result}\n"
puts "\n ******************************* 2nd test **************************\n \n"
# [START initialize_speech_client] 
require "google/apis/speech_v1beta1" 
speech_service = Google::Apis::SpeechV1beta1::SpeechService.new 
speech_service.authorization = Google::Auth.get_application_default( 
%[ https://www.googleapis.com/auth/cloud-platform ] ) 
 # [END initialize_speech_client]
# [START transcript_from_audio_file]
audio_file_path = "audio.flac" 
puts "audio_file_path:\n ------- #{audio_file_path}   --------------------\n" 
request        = Google::Apis::SpeechV1beta1::SyncRecognizeRequest.new 
request.audio  = { content: File.read(audio_file_path) } 
request.config = { encoding: "LINEAR16", sample_rate: 16000 } 
puts "request :\n ------- #{request} ----------\n" 
response = speech_service.sync_recognize_speech request 
puts "response: ------- #{response} --------------------\n" 

  response.results.each do |recognize_result| 
    recognize_result.alternatives.each do |alternative_hypothesis| 
      puts "Text: #{alternative_hypothesis.transcript}" 
    end 
  end   

输出

ruby gspeechTest.rb

 ******************************* 1st test *******************************
 audio_file_path:
 ------- audio.raw   --------------------
 audio: #<Google::Cloud::Speech::Audio:0x00000001be60d0>
 results: []
 Transcription: 

 ******************************* 2nd test *******************************
 audio_file_path:
 ------- audio.flac   --------------------
 request : ------- #<Google::Apis::SpeechV1beta1::SyncRecognizeRequest:0x00000002710848> ----------
response: ------- #<Google::Apis::SpeechV1beta1::SyncRecognizeResponse:0x00000002696700> --------------------
 gspeechTest.rb:55:in `<main>': undefined method `each' for nil:NilClass (NoMethodError)

音频文件来自:audio links。为什么结果是空的?我错过了什么吗? 我通过了身份验证,但我没有结果。音频文件是示例文件,那么问题出在哪里?

ubuntu 16.04
ruby​​ 2.3.1

0 个答案:

没有答案