Skip to content

Instantly share code, notes, and snippets.

@robertomiranda
Created August 8, 2022 08:42
Show Gist options
  • Save robertomiranda/b3bd48f9ab383b1859051aa480f147cf to your computer and use it in GitHub Desktop.
Save robertomiranda/b3bd48f9ab383b1859051aa480f147cf to your computer and use it in GitHub Desktop.
def speech_sync_recognize(audio_file_path: nil, language_code: "en-US", profanity_filter: false)
# [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,
profanity_filter: profanity_filter,
language_code: language_code }
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
audio_file_path = "test-en-US.wav"
speech_sync_recognize(audio_file_path: audio_file_path, language_code: "en-US", profanity_filter: true)
# ffmpeg -i ~/Downloads/IMG_4467.MOV -f s16le -acodec pcm_s16le -vn -ac 1 -ar 16k -map_metadata -1 test.wav
audio_file_path = "test-es-co.wav"
speech_sync_recognize(audio_file_path: audio_file_path, language_code: "es-CO", profanity_filter: true)
speech_sync_recognize(audio_file_path: audio_file_path, language_code: "es-CO", profanity_filter: true)
# Transcription: probando 1 2 3 probando 1 2 3 probando 1 2 3 probando Hola mundo Hola mundo Hola mundo Hola mundo
# => [<Google::Cloud::Speech::V1::SpeechRecognitionAlternative: transcript: "probando 1 2 3 probando 1 2 3 probando 1 2 3 probando Hola mundo Hola mundo Hola mundo Hola mundo", confidence: 0.9666613340377808, words: []>]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment