Speech Recognition with Google Cloud API on Asterisk 15 and CentOS

asterisk-logo2
Image 1. Asterisk & Google ASR

After long time trying setting up Google ASR on Asterisk, but this time using CentOS, finally I got it. In this post I will try to explain how to do it. The first step we must to do, is install the next packet:

yum -y groupinstall 'Development Tools'

The next step is install the dependencies for Asterisk 15.

yum install wget ssh ncurses ncurses-devel uuid uuid-devel libuuid-devel jansson-devel libxml2-devel sqlite-devel

Now, we can move to Home and download the the version number 15 of Asterisk:

cd ~
wget http://downloads.asterisk.org/pub/telephony/asterisk/asterisk-15-current.tar.gz

Now we shall move to /usr/src, decompress the tarball and move to the new directory:

cd /usr/src
tar zxvf ~/asterisk-15-current.tar.gz
cd asterisk-15*

Now execute the script configure and after make to generate the menu selection.

./configure
make menuselect

Then, we shall execute the followings commands. If we do not have a mandatory  Asterisk’s dependency the process would let us know, so pay attention to de STDERR.

make
make install
make config
make install-logrotate

If we want to generate files samples we should execute the following command:

make samples

For the proper operation of Asterisk we shall proceed to open the port 5060 for TCP and UDP.

firewall-cmd --set-default-zone=work
firewall-cmd --zone=work --permanent --change-interface=$YOUR_DEVICEID
firewall-cmd --zone=work --permanent --add-port=5060/udp
firewall-cmd --zone=work --permanent --add-port=5060/tcp

If you have any problem with Asterisk when you try to start it, you can try to deactivate SELinux and try again. Then we shall restart the service:

service asterisk restart

To open de CLI, execute:

asterisk -cvvvvvvv

We shall user the Zaf’s script for the Google Cloud Speech API, we can download it from here

The next step is install the script’s dependencies, so we have to download the followings packets:

yum install flac flac-devel perl perl-JSON perl-libwww-perl.noarch perl-LWP-Protocol-https perl-Crypt-SSLeay

We should move the file speech-recog.agi to the path for the AGI bin files, by default /var/lib/asterisk/agi-bin, if this directory does not exists, it must be declared in /etc/asterisk/asterisk.conf file.

As an example we will create the context google-asr, inside of /etc/asterisk/extensions.conf 

[google-asr]

exten => 666,1,Answer(); 
same => n,agi(speech-recog.agi,en,2,#);
same => n,Verbose(1,You said: ${utterance}); 
same => n,Verbose(1,The probability to be correct: ${confidence});
same => n,Verbose(1,The numbers of characters is: ${LEN(${utterance})});
same => n,Hangup();

Keep in mind this context must be added to the main context for our sip users in the sip.conf file.

[100]
---------
context=google-asr;

If we have another context as main, we can include the google-asr context in the main one as i show below:

[extensionesinternas]
---------
include => google-asr;

Now, we must do the two most important steps:

  1. Generate our key for the Google Cloud API
  2. Activate the billing in Google Cloud
  3. Insert our key in the speech-recog.agi file

For the first step you have to do the following:

  1. Go to Google Cloud Console: https://console.cloud.google.com/?hl=en
  2. If we do not have any  project, we must create a new one, after that we shall search “Google Cloud Speech API“, select the API and enable it.
    Sin título2
    Image 2. Enable Google Cloud Speech API
  3. At that moment a popup window will come out, letting know us we have to enable the billing in order to use the API.

6.JPG
Image 3. Enable billing for the project
  • When you enable the billing, on the left side you will see a key (Credentials). Click it and after ‘Create credentials’ –> ‘API Key’ –> Copy the API Key, and close.

  • 8.jpg
    Imagen 4. Create credentials -> API key
    9
    Image 5. API key

    In order to check we do not have any problem with the billing setting in our project we can execute the following command:

    curl https://speech.googleapis.com/v1beta1/speech:syncrecognize?key=$KEY -X POST -d '{}'
    

    In the STDOUT you should not see any problem related with the billing settings.

    Now we should edit the file speech-recog.agi and add our  API Key to the variable $key:

    my $key = “”; //it should be like my $key = “AIzaSyB**the rest**“
    

    When everything is ready from our softphone if we call to the 666 extension we should listen a BEEP sound, if it is like that, then we can say something. In the PoC i said in Spanish: «Hola esto es una prueba», (Hello this is a test) and as you can see in the Asterisk Console, Google ASR is working properly:

    logs
    Image 6. Result.

    3 comentarios sobre “Speech Recognition with Google Cloud API on Asterisk 15 and CentOS

    Deja una respuesta

    Introduce tus datos o haz clic en un icono para iniciar sesión:

    Logo de WordPress.com

    Estás comentando usando tu cuenta de WordPress.com. Salir /  Cambiar )

    Google photo

    Estás comentando usando tu cuenta de Google. Salir /  Cambiar )

    Imagen de Twitter

    Estás comentando usando tu cuenta de Twitter. Salir /  Cambiar )

    Foto de Facebook

    Estás comentando usando tu cuenta de Facebook. Salir /  Cambiar )

    Conectando a %s