freeswitchforum.com https://freeswitchforum.com/ |
|
создание меню с использованием google voice api https://freeswitchforum.com/viewtopic.php?f=6&t=91 |
Страница 1 из 1 |
Автор: | pastuhov_dyu [ 20 июл 2012 13:30 ] |
Заголовок сообщения: | создание меню с использованием google voice api |
Всем известный поисковик давно предоставляет бесплатно возможность распознование голоса и синтезатор речи. Давно есть идея прикрутить Распознование речи к freeswitch. работает это так записывается файл в формате flac и отсылаеться в google там он распознаеться и ответ приходит ввиде текста пример на финде http://code.google.com/p/mspeech/downlo ... 10-r14.rar скрипты для Linux Код: #!/bin/sh echo "1 SoX Sound Exchange - Convert WAV to FLAC with 16000" sox $1 message.flac pad .1 0 rate 16k echo "2 Submit to Google Voice Recognition" wget -q -U "Mozilla/5.0" --post-file message.flac --header="Content-Type: audio/x-flac; rate=16000" -O - "http://www.google.com/speech-api/v1/recognize?lang=en-us&client=chromium" > message.ret echo "3 SED Extract recognized text" cat message.ret | sed 's/.*utterance":"//' | sed 's/","confidence.*//' > message.txt echo "4 Remove Temporary Files" rm message.flac # rm message.ret echo "5 Show Text " cat message.txt Код: #!/usr/bin/env perl # # AGI script that renders speech to text using Google's speech recognition engine. # # Copyright (C) 2011 - 2012, Lefteris Zafiris <zaf.000@gmail.com> # # This program is free software, distributed under the terms of # the GNU General Public License Version 2. See the COPYING file # at the top of the source tree. # # ----- # Usage # ----- # agi(speech-recog.agi,[lang]) # Records from the current channel untill the pound key (#) is pressed or the # timeout (15 seconds) is reached. The recording is send over to googles speech # recognition service and the returned text string is assigned as the value # of the channel variable 'utterance'. # The scripts sets the following channel variables: # status : Return status. 0 means success, non zero values indicating different errors. # id : Some id string that googles engine returns, not very useful(?). # utterance : The generated text string. # confidence : A value between 0 and 1 indicating how 'confident' the recognition engine # feels about the result. Values biger than 0.95 usually mean that the # resulted text is correct. # use warnings; use strict; use File::Temp qw(tempfile); use LWP::UserAgent; $| = 1; # ----------------------------- # # User defined parameters: # # ----------------------------- # # Default language # my $language = "en-US"; # Input audio sample rate # my $samplerate = 8000; # 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 $timeout = 15000; my $tmpdir = "/tmp"; my $filetype = "x-flac"; my $url = "http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium"; my $flac = `/usr/bin/which flac`; my $big_endian = unpack("h*", pack("s", 1)) =~ /01/; if ($big_endian) { $endian = "big"; } else { $endian = "little"; } # Store AGI input # while (<STDIN>) { chomp; last if (!length); $AGI{$1} = $2 if (/^agi_(\w+)\:\s+(.*)$/); } ($AGI{arg_1}) = @ARGV; $name = " -- $AGI{request}:"; # Abort if required programs not found. # die "$name flac is missing. Aborting.\n" if (!$flac); chomp($flac); print STDERR "$name Found flac in: $flac\n" if ($debug); # Setting recording file format according to sample rate. # if ($samplerate == 16000) { $format = "sln16"; } else { $format = "sln"; $samplerate = 8000; } # Setting language # if (length($AGI{arg_1})) { $language = $AGI{arg_1} if ($AGI{arg_1} =~ /^[a-z]{2}(-[a-zA-Z]{2,6})?$/); } # Answer channel if not already answered # print STDERR "$name Checking channel status.\n" if ($debug); print "CHANNEL STATUS\n"; @result = &checkresponse(); if ($result[0] == 4) { print STDERR "$name Answering channel.\n" if ($debug); print "ANSWER\n"; &checkresponse(); } # Hnadle interrupts # $SIG{'INT'} = \&int_handler; $SIG{'HUP'} = \&int_handler; # Record file # ($fh, $tmpname) = tempfile("stt_XXXXXX", DIR => $tmpdir, UNLINK => 1); print STDERR "$name Recording file.\n" if ($debug); print "RECORD FILE $tmpname $format \"#\" $timeout\n"; @result = &checkresponse(); die "$name Failed to record file, aborting...\n" if ($result[0] == -1); if ($debug) { print STDERR "$name Languge: $language, Rate: $samplerate, Format: $format\n"; print STDERR "$name Playing back recorded file.\n"; print "STREAM FILE $tmpname \"\"\n"; @result = &checkresponse(); print STDERR "$name Failed to play file\n" if ($result[0] == -1); } # Convert sound file to flac # system($flac, "--totally-silent", "--channels=1", "--endian=$endian", "--sign=signed", "--bps=16", "--sample-rate=$samplerate", "--force-raw-format", "$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); $ua = LWP::UserAgent->new; $ua->agent("Mozilla/5.0 (X11; Linux) AppleWebKit/535.2 (KHTML, like Gecko)"); $ua->timeout(25); $uaresponse = $ua->post( "$url&lang=$language", Content_Type => "audio/$filetype; rate=$samplerate", Content => "$audio", ); die "$name Unable to get speech data.\n" if (!$uaresponse->is_success); if ($uaresponse->content =~ /^\{"status":(\d*),"id":"(.*)","hypotheses":\[(.*)\]\}$/) { $response{status} = "$1"; $response{id} = "$2"; print STDERR "Error reading audio file\n" if ($response{status} == 5); if ($3 =~ /^\{"utterance":"(.*)","confidence":(.*)\}/) { $response{utterance} = "$1"; $response{confidence} = "$2"; } } print STDERR "$name The response was:\n", $uaresponse->content if ($debug); foreach (keys %response) { print STDERR "$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/) { $input =~ /result=(-?\d+)\s?(.*)$/; if (!length($1)) { print STDERR "$name Command failed: $input\n"; @values = ("-1"); } else { print STDERR "$name Command returned: $input\n" if ($debug); @values = ("$1", "$2"); } } else { print STDERR "$name Unexpected result: $input\n"; @values = ("-1"); } return @values; } sub int_handler { die "$name Interrupt signal received, terminating...\n"; } END { if ($tmpname) { print STDERR "$name Cleaning temp files.\n" if ($debug); unlink glob "$tmpname*"; } } php Код: #!/usr/bin/php -q <? $agivars = array(); while (!feof(STDIN)) { $agivar = trim(fgets(STDIN)); if ($agivar === '') break; $agivar = explode(':', $agivar); $agivars[$agivar[0]] = trim($agivar[1]); } extract($agivars); $filename = $_SERVER["argv"][1]; exec('flac -f -s '.$filename.'.wav -o '.$filename.'.flac'); $file_to_upload = array('myfile'=>'@'.$filename.'.flac'); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,"https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=ru-RU"); curl_setopt($ch, CURLOPT_POST,1); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: audio/x-flac; rate=8000")); curl_setopt($ch, CURLOPT_POSTFIELDS, $file_to_upload); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); $result=curl_exec ($ch); curl_close ($ch); $json_array = json_decode($result, true); $voice_cmd = $json_array["hypotheses"][0]["utterance"]; unlink($filename.'.flac'); unlink($filename.'.wav'); echo 'SET VARIABLE VOICE "'.$voice_cmd.'"'."\n"; fgets(STDIN); echo 'VERBOSE ("'.$voice_cmd.'")'."\n"; fgets(STDIN); exit(0); ?> синтезатор речи на php Код: #!/usr/bin/php -q <?php $agivars = array(); while (!feof(STDIN)) { $agivar = trim(fgets(STDIN)); if ($agivar === '') break; $agivar = explode(':', $agivar); $agivars[$agivar[0]] = trim($agivar[1]); } extract($agivars); $text = $_SERVER["argv"][1]; if (isset($_SERVER["argv"][2])) $lang = $_SERVER["argv"][2]; else $lang = 'ru'; $md5 = md5($text); $prefix = '/var/lib/asterisk/festivalcache/'; $filename = $prefix.$md5; if (!file_exists($filename.'.alaw')) { $wget = 'wget -U "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5" '; $wget.= '"http://translate.google.com/translate_tts?q='.$text.'&tl='.$lang.'" -O '.$filename.'.mp3'; $ffmpeg = 'ffmpeg -i '.$filename.'.mp3 -ar 8000 -ac 1 -ab 64 '.$filename.'.wav -ar 8000 -ac 1 -ab 64 -f alaw '.$filename.'.alaw -map 0:0 -map 0:0'; $exec = $wget.' && '.$ffmpeg.' && rm '.$filename.'.mp3 '.$filename.'.wav'; exec($exec); } echo 'STREAM FILE "'.$filename.'" ""'."\n"; fgets(STDIN); exit(0); ?> все скрипты адаптированы для asterisk может поможете переписать для freeswitch |
Автор: | joohny [ 24 авг 2012 09:58 ] |
Заголовок сообщения: | Re: создание меню с использованием google voice api |
Пример использования распознавания голоса от Google в FreeSWITCH можно посмотреть в этой статье на habrahabr |
Страница 1 из 1 | Часовой пояс: UTC + 4 часа |
Powered by phpBB® Forum Software © phpBB Group http://www.phpbb.com/ |