[AsteriskBrasil] Usando o speech-recog.agi

Gulliver Nobrega gullivernobrega em gmail.com
Quinta Fevereiro 22 16:10:46 BRT 2018


Alguém conseguiu fazer funcionar o speech-recog.agi do google?.

Atenciosamente.

2018-02-01 16:02 GMT-02:00 Nuno Cunha <nuno.cunha em engdb.com.br>:

> Olas,
>
>   a URL da versão que uso, v1, também dá problema... https://speech.
> googleapis.com/v1/speech
>
>
> ----
> Nuno Correia dos Santos Cunha
>
> Analista de sistemas
> Automação & Controle - Serviços Logann - T&T
>
> Office:
> Skype:
>
> (31) 3211-7396
> nuno.cunha.engdb
> <http://www.engdb.com.br/> <https://www.facebook.com/engdb/>
> <https://www.linkedin.com/company-beta/27862?pathWildcard=27862>
> <https://www.youtube.com/channel/UCEUvHtjrY3-kD1tFryzCJyA>          Avenida
> Getúlio Vargas, nº 1.300, 9º e 10º andares | Belo Horizonte – MG | CEP
> 30112-021
>
> 2018-02-01 15:54 GMT-02:00 Renato Santos <renato473 em gmail.com>:
>
>> José ,
>>
>> Você já testou , pois URL de api que tem nele não tem acesso
>> https://www.google.com/speech-api/v2/recognize
>>
>> 2018-02-01 13:47 GMT-03:00 Jose <jasanchez113 em gmail.com>:
>>
>>> Essa funciona so coloque sua chave:
>>>
>>>
>>> #!/usr/bin/env perl
>>>
>>>
>>> use warnings;
>>> use strict;
>>> use URI::Escape;
>>> use File::Copy qw(move);
>>> use File::Temp qw(tempfile);
>>> use LWP::UserAgent;
>>> use JSON;
>>> use Encode qw(encode);
>>>
>>> $| = 1;
>>>
>>> # ----------------------------- #
>>> #   User defined parameters:    #
>>> # ----------------------------- #
>>> # Speech API key                #
>>> my $key = sua chave do google";
>>>
>>> # Default language              #
>>> my $language = "pt-BR";
>>>
>>> # Default max silence timeout   #
>>> my $timeout = 2;
>>>
>>> # Absolute Recording timeout    #
>>> my $abs_timeout = -1;
>>>
>>> # Default interrupt key         #
>>> my $intkey = "#";
>>>
>>> # Input audio sample rate       #
>>> # Leave blank to auto-detect    #
>>> my $samplerate = "";
>>>
>>> # Profanity filter              #
>>> my $pro_filter = 0;
>>>
>>> # Use speex                     #
>>> my $use_speex = 0;
>>>
>>> # Use SSL                       #
>>> my $use_ssl = 1;
>>>
>>> # Verbose debugging messages    #
>>> my $debug = 0;
>>>
>>> # ----------------------------- #
>>>
>>> my %AGI;
>>> my $ua;
>>> my $fh;
>>> my $tmpname;
>>> my $format;
>>> my @result;
>>> my $name;
>>> my $audio;
>>> my $uaresponse;
>>> my %response;
>>> my $endian;
>>> my $url;
>>> my $silence;
>>> my $filetype;
>>> my $flac;
>>> my $speex;
>>> my $results    = 1;
>>> my $grammar    = "builtin:dictation"; #"builtin:search";
>>> my $beep       = "BEEP";
>>> my $comp_level = -8;
>>> my $ua_timeout = 10;
>>> my $tmpdir     = "/tmp";
>>> my $host       = "www.google.com/speech-api/v2/recognize";
>>>
>>> # Store AGI input #
>>> ($AGI{arg_1}, $AGI{arg_2}, $AGI{arg_3}, $AGI{arg_4}) = @ARGV;
>>> while (<STDIN>) {
>>> chomp;
>>> last if (!length);
>>> $AGI{$1} = $2 if (/^agi_(\w+)\:\s+(.*)$/);
>>> }
>>>
>>> $name = " -- $AGI{request}:";
>>>
>>> # Reset variables. #
>>> %response = (
>>> utterance  => -1,
>>> confidence => -1,
>>> );
>>> warn "$name Clearing channel variables.\n" if ($debug);
>>> foreach (keys %response) {
>>> print "SET VARIABLE \"$_\" \"$response{$_}\"\n";
>>> checkresponse();
>>> }
>>>
>>> # Abort if key is missing or required programs not found. #
>>> if (!$key) {
>>> print "VERBOSE \"API key is missing. Aborting.\" 3\n";
>>> checkresponse();
>>> die "$name API key is missing. Aborting.\n";
>>> }
>>> if ($use_speex) {
>>> $speex = `/usr/bin/which speexenc`;
>>> die "$name speexenc is missing. Aborting.\n" if (!$speex);
>>> chomp($speex);
>>> warn "$name Found speexenc in: $speex\n" if ($debug);
>>> } else {
>>> $flac = `/usr/bin/which flac`;
>>> die "$name flac is missing. Aborting.\n" if (!$flac);
>>> chomp($flac);
>>> warn "$name Found flac in: $flac\n" if ($debug);
>>> }
>>>
>>> # Setting language, timeout, interrupt keys and BEEP indication #
>>> if (length($AGI{arg_1})) {
>>> $language = $AGI{arg_1} if ($AGI{arg_1} =~ /^[a-z]{2}(-[a-zA-Z]{2,6})?$/)
>>> ;
>>> }
>>>
>>> if (length($AGI{arg_2})) {
>>> if ($AGI{arg_2} == -1) {
>>> $silence = "";
>>> } elsif ($AGI{arg_2} =~ /^\d+$/) {
>>> $silence = "s=$AGI{arg_2}";
>>> } else {
>>> $silence = "s=$timeout";
>>> }
>>> } else {
>>> $silence = "s=$timeout";
>>> }
>>>
>>> if (length($AGI{arg_3})) {
>>> $intkey = "0123456789#*" if ($AGI{arg_3} eq "any");
>>> $intkey = $AGI{arg_3} if ($AGI{arg_3} =~ /^[0-9*#]+$/);
>>> }
>>>
>>> if (length($AGI{arg_4})) {
>>> $beep = "" if ($AGI{arg_4} eq "NOBEEP");
>>> }
>>>
>>> # Answer channel if not already answered #
>>> warn "$name Checking channel status.\n" if ($debug);
>>> print "CHANNEL STATUS\n";
>>> @result = checkresponse();
>>> if ($result[0] == 4) {
>>> warn "$name Answering channel.\n" if ($debug);
>>> print "ANSWER\n";
>>> @result = checkresponse();
>>> if ($result[0] != 0) {
>>> die "$name Failed to answer channel.\n";
>>> }
>>> }
>>>
>>> # Setting recording file format according to sample rate. #
>>> if    (!$samplerate)         { ($format, $samplerate) = detect_format();
>>> }
>>> elsif ($samplerate == 12000) { $format = "sln12"; }
>>> elsif ($samplerate == 16000) { $format = "sln16"; }
>>> elsif ($samplerate == 32000) { $format = "sln32"; }
>>> elsif ($samplerate == 44100) { $format = "sln44"; }
>>> elsif ($samplerate == 48000) { $format = "sln48"; }
>>> else                         { ($format, $samplerate) =  ("sln", 8000); }
>>>
>>> # Initialise User angent #
>>> if ($use_ssl) {
>>> $url = "https://" . $host;
>>> $ua  = LWP::UserAgent->new(ssl_opts => {verify_hostname => 1});
>>> } else {
>>> $url = "http://" . $host;
>>> $ua  = LWP::UserAgent->new;
>>> }
>>> $language = uri_escape($language);
>>> $grammar  = uri_escape($grammar);
>>> $url .= "?key=$key&lang=$language&pfilter=$pro_filter&lm=$grammar&ma
>>> xresults=$results";
>>> $ua->agent("Asterisk AGI speeech recognition script");
>>> $ua->env_proxy;
>>> $ua->timeout($ua_timeout);
>>>
>>> # Hnadle interrupts #
>>> $SIG{'INT'} = \&int_handler;
>>> $SIG{'HUP'} = \&int_handler;
>>>
>>> # Record file #
>>> ($fh, $tmpname) = tempfile("stt_XXXXXX", DIR => $tmpdir, UNLINK => 1);
>>> print "RECORD FILE $tmpname $format \"$intkey\" \"$abs_timeout\" $beep
>>> \"$silence\"\n";
>>> @result = checkresponse();
>>> die "$name Failed to record file, aborting...\n" if ($result[0] == -1);
>>>
>>> if ($debug) {
>>> warn "$name Recording Format: $format, Rate: $samplerate Hz, ",
>>> "Encoding format: ", ($use_speex) ? "speex" : "flac", "\n",
>>> "$name Languge: $language, SSL: ", ($use_ssl) ? "yes, " : "no, ",
>>> "$silence, Interrupt keys: $intkey\n";
>>> }
>>>
>>> # Encode sound data #
>>> if ($use_speex) {
>>> $filetype = "x-speex-with-header-byte";
>>> $endian = (unpack("h*", pack("s", 1)) =~ /01/) ? "--be" : "--le";
>>> # Encode file to speex. #
>>> system($speex, "--vbr", "--rate", $samplerate, "--headerbyte",
>>> "--quiet", $endian,
>>> "$tmpname.$format", "$tmpname.spx") == 0 or die "$name $speex failed:
>>> $?\n";
>>> open($fh, "<", "$tmpname.spx") or die "Can't read file: $!";
>>> } else {
>>> $filetype = "x-flac";
>>> $endian = (unpack("h*", pack("s", 1)) =~ /01/) ? "big" : "little";
>>> # Encode file to flac. #
>>> system($flac, $comp_level, "--totally-silent", "--channels=1",
>>> "--endian=$endian",
>>> "--sign=signed", "--bps=16", "--force-raw-format",
>>> "--sample-rate=$samplerate",
>>> "$tmpname.$format") == 0 or die "$name $flac failed: $?\n";
>>> open($fh, "<", "$tmpname.flac") or die "Can't read file: $!";
>>> }
>>>
>>> $audio = do { local $/; <$fh> };
>>> close($fh);
>>>
>>> # Send adio data for analysis #
>>> $uaresponse = $ua->post(
>>> "$url",
>>> Content_Type => "audio/$filetype; rate=$samplerate",
>>> Content      => "$audio",
>>> );
>>> if (!$uaresponse->is_success) {
>>> print "VERBOSE \"Unable to get recognition data.\" 3\n";
>>> checkresponse();
>>> die "$name Unable to get recognition data.\n";
>>> }
>>>
>>> foreach (split(/\n/,$uaresponse->content)) {
>>> my $jdata = decode_json($_);
>>> for ( $jdata->{result}[0]->{alternative}[0] ) {
>>> $response{utterance}  = encode('utf8', $_->{transcript});
>>> $response{confidence} = $_->{confidence};
>>> }
>>> }
>>> warn "$name The response was:\n", $uaresponse->content if ($debug);
>>>
>>> foreach (keys %response) {
>>> warn "$name Setting variable: $_ = $response{$_}\n" if ($debug);
>>> print "SET VARIABLE \"$_\" \"$response{$_}\"\n";
>>> checkresponse();
>>> }
>>> exit;
>>>
>>> sub checkresponse {
>>> my $input = <STDIN>;
>>> my @values;
>>>
>>> chomp $input;
>>> if ($input =~ /^200 result=(-?\d+)\s?(.*)$/) {
>>> warn "$name Command returned: $input\n" if ($debug);
>>> @values = ("$1", "$2");
>>> } else {
>>> $input .= <STDIN> if ($input =~ /^520-Invalid/);
>>> warn "$name Unexpected result: $input\n";
>>> @values = (-1, -1);
>>> }
>>> return @values;
>>> }
>>>
>>> sub detect_format {
>>> # Detect the sound format used #
>>> my @format;
>>> print "GET FULL VARIABLE \${CHANNEL(audionativeformat)}\n";
>>> my @reply = checkresponse();
>>> for ($reply[1]) {
>>> if    (/(silk|sln)12/)                    { @format = ("sln12", 12000); }
>>> elsif (/(speex|slin|silk)16|g722|siren7/) { @format = ("sln16", 16000);
>>> }
>>> elsif (/(speex|slin|celt)32|siren14/)     { @format = ("sln32", 32000);
>>> }
>>> elsif (/(celt|slin)44/)                   { @format = ("sln44", 44100); }
>>> elsif (/(celt|slin)48/)                   { @format = ("sln48", 48000); }
>>> else                                      { @format = ("sln",    8000); }
>>> }
>>> return @format;
>>> }
>>>
>>> sub int_handler {
>>> die "$name Interrupt signal received, terminating...\n";
>>> }
>>>
>>> END {
>>> if ($tmpname) {
>>> warn "$name Cleaning temp files.\n" if ($debug);
>>> unlink glob "$tmpname.*";
>>> }
>>> }
>>>
>>> Atenciosamente
>>>
>>> Eng. Jose Antonio Sanchez
>>> Erimat Oeste Telecom
>>> www.pbxerix.com.br
>>> www.virtualpbxip.com.br
>>> www.erimatoeste.com.br
>>> jasanchez em terra.com.br
>>> 17-21393970 <(17)%202139-3970> 17-81437977
>>>
>>>
>>> Em 1 de fev de 2018, à(s) 14:44, Nuno Cunha <nuno.cunha em engdb.com.br>
>>> escreveu:
>>>
>>> Olas,
>>>
>>>   Também tentei:
>>>
>>>             p_channel.exec("Agi", "speech-recog.agi,pt-BR,2");
>>>
>>>   com o mesmo resultado.
>>>
>>>
>>>
>>> <https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail> Livre
>>> de vírus. www.avast.com
>>> <https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>.
>>>
>>>
>>>
>>> ----
>>> Nuno Correia dos Santos Cunha
>>>
>>> Analista de sistemas
>>> Automação & Controle - Serviços Logann - T&T
>>>
>>> Office:
>>> Skype:
>>>
>>> (31) 3211-7396
>>> nuno.cunha.engdb
>>> <http://www.engdb.com.br/> <https://www.facebook.com/engdb/>
>>> <https://www.linkedin.com/company-beta/27862?pathWildcard=27862>
>>> <https://www.youtube.com/channel/UCEUvHtjrY3-kD1tFryzCJyA>          Avenida
>>> Getúlio Vargas, nº 1.300, 9º e 10º andares | Belo Horizonte – MG | CEP
>>> 30112-021
>>>
>>> Em 1 de fevereiro de 2018 14:23, Renato Santos <renato473 em gmail.com>
>>> escreveu:
>>>
>>>> Acho que está api foi descontinuada pois ela está na V1 e o Google usa
>>>> a V3
>>>>
>>>> Em qui, 1 de fev de 2018 às 13:11, Nuno Cunha <nuno.cunha em engdb.com.br>
>>>> escreveu:
>>>>
>>>>> Ola,
>>>>>
>>>>>   Em meu AGI (fastAGI, em java) tenho estas duas linhas:
>>>>>
>>>>>             p_channel.exec("Playback", "Fale o endereço!");
>>>>>             p_channel.exec("Agi", "speech-recog.agi", "pt-BR");
>>>>>
>>>>>
>>>>>   O problema é que após ler a pergunta o tempo de fala parece estar
>>>>> sendo de zero segundos, mal começo a falar e já recebo a mensagem que o
>>>>> endereço não foi compreendido (que é a lógica logo a seguir a estas duas
>>>>> linhas).
>>>>>
>>>>>   No script   speech-recog.agi tenho as configurações:
>>>>>
>>>>> # ----------------------------- #
>>>>> #   User defined parameters:    #
>>>>> # ----------------------------- #
>>>>> # Speech API key                #
>>>>> my $key = "minha chave...";
>>>>>
>>>>> # Default language              #
>>>>> my $language = "pt-BR";
>>>>>
>>>>> # Default max silence timeout   #
>>>>> my $timeout = 2;
>>>>>
>>>>> # Absolute Recording timeout    #
>>>>> my $abs_timeout = -1;
>>>>>
>>>>> # Default interrupt key         #
>>>>> my $intkey = "#";
>>>>>
>>>>> # Input audio sample rate       #
>>>>> # Leave blank to auto-detect    #
>>>>> my $samplerate = "";
>>>>>
>>>>> # Profanity filter              #
>>>>> my $pro_filter = "false";
>>>>>
>>>>> # Verbose debugging messages    #
>>>>> my $debug = 0;
>>>>>
>>>>> # ----------------------------- #
>>>>>
>>>>>
>>>>>   Testei colocando explicitamente na chamada do agi do Google o tempo
>>>>> de timeout, como mostrado abaixo, mas não mudou o comportamento.
>>>>>
>>>>>             p_channel.exec("Playback", "Fale o endereço!");
>>>>>             p_channel.exec("Agi", "speech-recog.agi", " pt-BR ", "2");
>>>>>
>>>>>   Alguma ideia de como ter mais tempo para falar o endereço?
>>>>>
>>>>> Obrigado,
>>>>>
>>>>>
>>>>> ----
>>>>> Nuno Correia dos Santos Cunha
>>>>>
>>>>> Analista de sistemas
>>>>> Automação & Controle - Serviços Logann - T&T
>>>>>
>>>>> Office:
>>>>> Skype:
>>>>>
>>>>> (31) 3211-7396
>>>>> nuno.cunha.engdb
>>>>> <http://www.engdb.com.br/> <https://www.facebook.com/engdb/>
>>>>> <https://www.linkedin.com/company-beta/27862?pathWildcard=27862>
>>>>> <https://www.youtube.com/channel/UCEUvHtjrY3-kD1tFryzCJyA>          Avenida
>>>>> Getúlio Vargas, nº 1.300, 9º e 10º andares | Belo Horizonte – MG | CEP
>>>>> 30112-021
>>>>>
>>>>>
>>>>> <https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail> Livre
>>>>> de vírus. www.avast.com
>>>>> <https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>.
>>>>>
>>>>> _______________________________________________
>>>>> KHOMP: completa linha de placas externas FXO, FXS, GSM e E1
>>>>> Media Gateways de 1 a 64 E1s para SIP com R2, ISDN e SS7
>>>>> Intercomunicador e acesso remoto via rede IP e telefones IP
>>>>> Conheça todo o portfólio em www.Khomp.com <http://www.khomp.com/>
>>>>> _______________________________________________
>>>>> Para remover seu email desta lista, basta enviar um email em branco
>>>>> para asteriskbrasil-unsubscribe em listas.asteriskbrasil.org
>>>>
>>>> --
>>>> Enviado do Gmail para celular
>>>>
>>>> _______________________________________________
>>>> KHOMP: completa linha de placas externas FXO, FXS, GSM e E1
>>>> Media Gateways de 1 a 64 E1s para SIP com R2, ISDN e SS7
>>>> Intercomunicador e acesso remoto via rede IP e telefones IP
>>>> Conheça todo o portfólio em www.Khomp.com <http://www.khomp.com/>
>>>> _______________________________________________
>>>> Para remover seu email desta lista, basta enviar um email em branco
>>>> para asteriskbrasil-unsubscribe em listas.asteriskbrasil.org
>>>>
>>>
>>> _______________________________________________
>>> KHOMP: completa linha de placas externas FXO, FXS, GSM e E1
>>> Media Gateways de 1 a 64 E1s para SIP com R2, ISDN e SS7
>>> Intercomunicador e acesso remoto via rede IP e telefones IP
>>> Conheça todo o portfólio em www.Khomp.com
>>> _______________________________________________
>>> Para remover seu email desta lista, basta enviar um email em branco para
>>> asteriskbrasil-unsubscribe em listas.asteriskbrasil.org
>>>
>>>
>>>
>>> _______________________________________________
>>> KHOMP: completa linha de placas externas FXO, FXS, GSM e E1
>>> Media Gateways de 1 a 64 E1s para SIP com R2, ISDN e SS7
>>> Intercomunicador e acesso remoto via rede IP e telefones IP
>>> Conheça todo o portfólio em www.Khomp.com
>>> _______________________________________________
>>> Para remover seu email desta lista, basta enviar um email em branco para
>>> asteriskbrasil-unsubscribe em listas.asteriskbrasil.org
>>>
>>
>>
>>
>> --
>> Renato
>>
>> *you don't need a hero to succeed on the field....you need a team!*
>>
>>
>>
>>
>> _______________________________________________
>> KHOMP: completa linha de placas externas FXO, FXS, GSM e E1
>> Media Gateways de 1 a 64 E1s para SIP com R2, ISDN e SS7
>> Intercomunicador e acesso remoto via rede IP e telefones IP
>> Conheça todo o portfólio em www.Khomp.com
>> _______________________________________________
>> Para remover seu email desta lista, basta enviar um email em branco para
>> asteriskbrasil-unsubscribe em listas.asteriskbrasil.org
>>
>
>
> _______________________________________________
> KHOMP: completa linha de placas externas FXO, FXS, GSM e E1
> Media Gateways de 1 a 64 E1s para SIP com R2, ISDN e SS7
> Intercomunicador e acesso remoto via rede IP e telefones IP
> Conheça todo o portfólio em www.Khomp.com
> _______________________________________________
> Para remover seu email desta lista, basta enviar um email em branco para
> asteriskbrasil-unsubscribe em listas.asteriskbrasil.org
>
-------------- Próxima Parte ----------
Um anexo em HTML foi limpo...
URL: <http://asteriskbrasil.org/pipermail/asteriskbrasil/attachments/20180222/f92d16f0/attachment-0001.html>
-------------- Próxima Parte ----------
Um anexo não-texto foi limpo...
Nome: logo.erix.teste.fw.png
Tipo: image/png
Tamanho: 90365 bytes
Descrição: não disponível
URL: <http://asteriskbrasil.org/pipermail/asteriskbrasil/attachments/20180222/f92d16f0/attachment-0001.png>


Mais detalhes sobre a lista de discussão AsteriskBrasil