Was this page helpful?

Google Cloud Speech API

    speech-api-lead.pngGoogle 雲端語音辨識 API(中文)

    官網連結:https://cloud.google.com/speech/?hl=zh_TW

    注意事項:

    • 語音辨識 API 必須先申請 API Key 才能使用,請參閱:Creating an API Key for Google Speech Recognition
    • 此篇新增了查詢通訊錄檔的 AGI,如不需要,可以省略,不過也要同時修改相關的 AGI。
    • 要讓語音辨識與TTS 兩個 AGI 正常工作,還必須要安裝各自相依的其他套件,詳細請參考作者的網站說明。

    在 Linux 上做簡單測試

    test-google-speech.sh:

    #!/bin/bash
    # Copyright 2017 Google Inc.
    # Licensed under the Apache License, Version 2.0 (the "License");
    # you may not use this file except in compliance with the License.
    # You may obtain a copy of the License at
    #     http://www.apache.org/licenses/LICENSE-2.0
    # Unless required by applicable law or agreed to in writing, software
    # distributed under the License is distributed on an "AS IS" BASIS,
    # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    # See the License for the specific language governing permissions and
    # limitations under the License.

    # Create a request file with our JSON request in the current directory
    FILENAME="request-"`date +"%s".json`
    cat <<EOF > $FILENAME
    {
      "config": {
        "encoding":"FLAC",
        "sampleRateHertz":16000,
        "profanityFilter": true,
        "languageCode": "en-US",
        "speechContexts": {
          "phrases": ['Sox']
        },
        "maxAlternatives": 1
      },
      "audio": {
        "content":
    }
    }
    EOF

    # Update the languageCode parameter if one was supplied
    if [ $# -eq 1 ]
      then
        sed -i -e "s/en-US/$1/g" $FILENAME
    fi

    # Record an audio file, base64 encode it, and update our request object
    read -p "Press enter when you're ready to record" rec
    if [ -z $rec ]; then
      rec --channels=1 --bits=16 --rate=16000 audio.flac trim 0 5
      echo \"`base64 audio.flac -w 0`\" > audio.base64
      sed -i -e '/"content":/r audio.base64' $FILENAME
    fi
    echo Request "file" $FILENAME created:
    head -7 $FILENAME # Don't print the entire file because there's a giant base64 string
    echo $'\t"Your base64 string..."\n\x20\x20}\n}'


    # Call the speech API (requires an API key)
    read -p $'\nPress enter when you\'re ready to call the Speech API' var
    if [ -z $var ];
      then
        echo "Running the following curl command:"
        echo "curl -s -X POST -H 'Content-Type: application/json' --data-binary @${FILENAME} https://speech.googleapis.com/v1/speech:recognize?key=API_KEY"
        curl -s -X POST -H "Content-Type: application/json" --data-binary @${FILENAME} https://speech.googleapis.com/v1/speech:recognize?key=ThisYourAPIKey 
    fi

    開始做測試

    chmod +x test-google-speech.sh
    ./test-google-speech
    
    按下 Enter 後開始錄音 -> 你有 4 秒鐘時間 -> 按下 Enter 呼叫語音辨識 API -> 輸出辨識結果
    

    必要套件需求

    Debian 7

    apt-get install libwww-perl libjson-perl
    

    與 Asterisk/FreePBX 做整合

    下載 AGI Scripts:

     

    將 AGI 上傳到 Asterisk 主機的目錄 /var/lib/asterisk/agi-bin

    • Google Cloud Speech API
      google-speech-recog.agi
    • Google TTS
      googletts.agi

     

    新增 AGI: my-speech-callwho.php
    用途:用來以名稱查詢通訊錄的號碼,通訊錄的格式為 JSON。

    #!/usr/bin/php -q 
    <? 
    ob_implicit_flush(false); 
    error_reporting(0); 
    set_time_limit(300); 
    
    // The path to JSON file
    $CONTACTS=dirname(__FILE__)."/my-speech-contact.json";
    
    //-------- DON'T CHANGE ANYTHING ABOVE THIS LINE ----------------
    $debug = 1;
    $newlogeachdebug = 1;
    
    //---- Clean the log file ----
    $log = "/var/log/asterisk/my-speech-callwho.log" ;
    if ($debug and $newlogeachdebug) :
     if (file_exists($log)) :
      unlink($log) ;
     endif ;
    endif ;
    
    $stdlog = fopen($log, 'a'); 
    $stdin = fopen('php://stdin', 'r'); 
    $stdout = fopen( 'php://stdout', 'w' ); 
    
    if ($debug) :
      fputs($stdlog, "My CallWho for Google Cloud Speech, written by A-Lang.\n\n" . date("F j, Y - H:i:s") . "  *** New session ***\n\n" ); 
    endif ;
    
    //---- BEGIN: Function execute_agi() ----
    function execute_agi( $command ) 
    { 
    GLOBAL $stdin, $stdout, $stdlog, $debug; 
     
    fputs( $stdout, $command . "\n" ); 
    fflush( $stdout ); 
    if ($debug) 
    fputs( $stdlog, $command . "\n" ); 
     
    $resp = fgets( $stdin, 4096 ); 
     
    if ($debug) 
    fputs( $stdlog, $resp ); 
     
    if ( preg_match("/^([0-9]{1,3}) (.*)/", $resp, $matches) )  
    { 
    if (preg_match('/result=([-0-9a-zA-Z]*)(.*)/', $matches[2], $match))  
    { 
    $arr['code'] = $matches[1]; 
    $arr['result'] = $match[1]; 
    if (isset($match[3]) && $match[3]) 
    $arr['data'] = $match[3]; 
    return $arr; 
    }  
    else  
    { 
    if ($debug) 
    fputs( $stdlog, "Couldn't figure out returned string, Returning code=$matches[1] result=0\n" );  
    $arr['code'] = $matches[1]; 
    $arr['result'] = 0; 
    return $arr; 
    } 
    }  
    else  
    { 
    if ($debug) 
    fputs( $stdlog, "Could not process string, Returning -1\n" ); 
    $arr['code'] = -1; 
    $arr['result'] = -1; 
    return $arr; 
    } 
    }  
    //---- END: Function execute_agi() ----
    
    //---- BEGIN: Function validatePhone() ----
    function is_PhoneNumber($string) {
        $_string = str_replace("-", "", $string);
        if (is_numeric($_string)) {
            return true;
        }else{
            return false;
        }
    }
    
    //---- END: Function validatePhone() ----
    
    function quit() {
        GLOBAL $stdin, $stdout, $stdlog, $debug;
        fclose($stdin);
        fclose($stdout);
        fclose($stdlog);
        exit;
    }
    
    
    //---- BEGIN: Main Codes ----
    //execute_agi("SET VARIABLE NUM2CALL 99999999");
    if (!isset($argv[1])) {
        if ($debug) {
            fputs($stdlog,"Abort: Missing some info.\n");
        }
        quit();
    }
    
    if (!file_exists($CONTACTS)) {
        if ($debug) {
            fputs($stdlog,"Abort: File Not Found ($CONTACTS).\n");
        }
        quit();
    }
    
    $searchWord = trim($argv[1]);
    
    // Check if phone numbers
    if (is_PhoneNumber($searchWord)) {
        $num2call = str_replace("-", "", $searchWord);
        // return the result
        execute_agi("SET VARIABLE NUM2CALL $num2call");
        quit();
    }
    
    $contact_a = json_decode(file_get_contents($CONTACTS), true);
    if ($debug){
        fputs($stdlog,"search word: $searchWord\n");
    }
    
    $num2call = "0";
    foreach ($contact_a as $name => $phoneNum) {
        if ($name == $searchWord){
            $num2call = $phoneNum;
        }    
    }
    
    if ($debug){
        fputs($stdlog,"num2call: $num2call\n");
    }
    
    // return the result
    execute_agi("SET VARIABLE NUM2CALL $num2call");
    
    quit();
    ?> 

    通訊錄檔 my-speech-contact.json:

    {
     "老大":"213",
     "老二":"205",
     "老三":"206",
     "老么":"206",
     "=Don't Remove me=":"00000000"
    }
    

    編輯 /etc/asterisk/extensions_custom.conf

    [my-google-speech]
    exten => s,1,Answer()
     same => n,Wait(1)
     same => n,AGI(googletts.agi,"歡迎使用語音辨識系統。",zh-TW)
     same => n(pickagain),AGI(googletts.agi,"請在嗶聲後,說出您要撥的號碼,或姓名。",zh-TW)
     same => n,AGI(google-speech-recog.agi,zh-TW)
     same => n,Noop(= Script returned: ${status} , ${id} , ${confidence} , ${utterance} =)
     same => n,AGI(my-speech-callwho.php, ${utterance})
     same => n,NoOp(What I said: ${utterance})
     same => n,NoOp(Number to call: ${NUM2CALL})
     same => n,GotoIf($["foo${NUM2CALL}" = "foo0"]?pickagain)
     same => n,GotoIf($["foo${NUM2CALL}" = "foo"]?pickagain)
     same => n,AGI(googletts.agi,"您要撥的電話是,${utterance},正確,請按一。",zh-TW)
     same => n,Read(MYPICK,,1)
     same => n,GotoIf($["foo${MYPICK}" = "foo1"]?proceed)
     same => n,Goto(done)
     same => n(proceed),AGI(googletts.agi,"電話轉接中,請稍後。",zh-TW)
     same => n,Goto(from-internal,${NUM2CALL},1)
     ;same => n,Goto(ext-local,${NUM2CALL},1)
     same => n(done),AGI(googletts.agi,"謝謝您的來電",zh-TW)
     same => n,Hangup
    Was this page helpful?
    標籤 (Edit tags)
    • No tags

    文件 1

    文件大小日期附件上傳者 
     speech-api-lead.png
    無描述
    4.41 KB01:15, 18 Jul 2017alang動作
    您必須 登入 才能發佈評論。
    Powered by MindTouch Core