LoginSignup
5
5

More than 5 years have passed since last update.

Railsで通話内容を音声認識する

Last updated at Posted at 2017-08-18

概要

TwilioとGoogleCloudSpeechAPIを使用し、Railsで通話内容を音声認識する

手順

GoogleCloudSpeechAPIを有効にする

https://console.cloud.google.com/
API_ライブラリ_-_autok_and_iharakenji_s_notebook_—_Evernote.png
APIs___services_-_autok.png

Googleサービスアカウントキーを作成する

認証情報_-_autok_and_iharakenji_s_notebook_—_Evernote_and_Google_翻訳.png
サービス_アカウント_キーの作成_-_autok_and_iharakenji_s_notebook_—_Evernote.png
ファイルがダウンロードされるので保存する

Gemをインスールする

gem 'twilio-ruby'
gem 'google-cloud-speech'

Railsアプリを作成する

call_controller.rb
require 'google/cloud/speech'
class CallController < ApplicationController
  def transcription
    recording_url = params[:RecordingUrl]
    voice = 'man'
    language = 'ja-JP'
    response = Twilio::TwiML::VoiceResponse.new
    if recording_url.blank?
      response.say('お名前をフルネームで教えてください。', voice: voice, language: language)
      response.record(timeout: 5, max_length: 5)
    else
      project = 'プロジェクトID'
      keyfile = 'サービスアカウントキーのファイルのパス'
      speech = Google::Cloud::Speech.new project: project, keyfile: keyfile
      audio = speech.audio open(recording_url),
                   encoding: :linear16,
                   language: 'ja-JP',
                   sample_rate: 8000
      results = audio.recognize
      result = results.first

      if result.present?
        transcript = result.transcript
        response.say('あなたの名前は、', voice: voice, language: language)
        response.say(transcript, voice: voice, language: language)
        response.say('です。', voice: voice, language: language)
      else
        response.say('すみませんが、もう一度お願いします。', voice: voice, language: language)
        response.record(timeout: 5, max_length: 5)
      end
    end

    render :xml => response.to_s
  end
end

Railsアプリを起動

bundle exec rails s

ngrokを起動する

ngrok http 3000

google_speech_service_rb_-_recruiting-bot_-____workspace_regulus-technologies_recruiting-bot_.png

TwiML Appを作成する

https://www.twilio.com/
https://jp.twilio.com/docs/api/twiml
Twilio_Console_-_Phone_Numbers_Dev_Tools_TwiML_Apps_Add_and_iharakenji_s_notebook_—_Evernote_and_Google_翻訳.png

TwiML Appを確認する

Twilio_Console_-_Phone_Numbers_Dev_Tools_TwiML_Apps.png
Twilio_Console_-_Phone_Numbers_Dev_Tools_TwiML_Apps.png

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5