LoginSignup
12
16

More than 5 years have passed since last update.

PepperでJKりんなと会話する

Posted at

※注意事項※
※りんなAPIは、Microsoftが提供しているAPIです。
※AITalkは、株式会社AI(エーアイ)が提供しているAPIです。
※各種APIは、ハッカソンで提供されたものです。

『Pepperを介して、MSのAI女子高生りんなと会話』を作ってみました。
りんなの応答はjsonで戻るので、AIのAITalkでJK風の音声にしてPepperで再生します。

Choregraphe

実装の内容はこんな感じです。
1.Pepperで収集した音声データをGoogle Speech APIでテキスト化
2.テキストをりんなAPIに送信し、りんなの応答テキスト(JSONデータ)をレスポンス取得する
3.応答テキストをAITalk APIに送信し、音声合成された音声データをレスポンス取得する
4.音声データファイルをPepperで再生する
Pepperが聞くモードの時に解りやすいよう目を青にしています。

choregraphe.PNG

Google Speech API

Google Speech APIの書き方は http://qiita.com/lethe2211/items/7c9b1b82c7eda40dafa9 を参照
stt.pyを少し修正したので、ここだけUPします。他はそのままで動きました。

`import sys
 import os
 import json
 import requests
 import urllib

#apikey = os.environ.get("GOOGLE_API_KEY")
apikey = "自分のキーを記入"
TIMEOUT 30

def stt_google_wav(filename):
    q = {"output": "json", "lang": "ja-JP", "key": apikey}

    url = "http://www.google.com/speech-api/v2/recognize?%s" % (urllib.parse.urlencode(q))

    headers = {"Content-Type": "audio/l16; rate=16000"}
    data = open(filename, "rb").read()

    response = requests.post(
        url,
        headers=headers,
        data=data,
        timeout=TIMEOUT
    )

    #jsonunits = response.text.split(os.linesep)
    jsonunits = response.text.split("\n")
    res = ""

    for unit in jsonunits:
        if not unit:
            continue
        #print(unit)
        #print([ord(x) for x in unit])
        #print('---------\n\n')
        obj = json.loads(unit)
        alternatives = obj["result"]

        if len(alternatives) > 0:
            breakflag = False
            for obj in alternatives:
                results = obj["alternative"]
                for result in results:
                    res = result["transcript"]
                    breakflag = True
                    break

                if breakflag:
                    break

    return res
if __name__ == '__main__':
    print(stt_google_wav(sys.argv[1]))`

りんなAPI

ハッカソンでMSの担当者の方がCloud Robotics APIをHTTPSベースで呼び出すサンプルを提供してくれました。
それを参考に応答からセリフを抜き出します。

   `headers = None
    body = None
    resCode = None
    #while True:
    for x in range(5):
        time.sleep(0.5)
        try:
            request = urllib2.Request(p_url2, None, p_headers2)
            response = urllib2.urlopen(request)
            headers = response.info()
            body = response.read()
            resCode = response.code
            if resCode == 200:
                messageId = headers['ETag'].replace('\"','')
                rinMsg0 = json.loads(body)#['RbBody']['talkListByRinna'][0]['SayText'].encode("utf-8")
                rinMsg = rinMsg0['RbBody']['talkListByRinna'][0]['SayText'].encode("utf-8")
                break
        except urllib2.HTTPError as he:
            break
        except Exception as e:
            break
        else:
            if resCode == 204: # No Content
                continue`

AITalk API

AITalkはphpのAPIサンプルを提供してくれました。
そちらをpythonに書き換えます。

  `import sys, os
    self.folderName = os.path.join(self.framemanager.getBehaviorPath(self.behaviorId), "../lib")

    if self.folderName not in sys.path:
        sys.path.append(self.folderName)

    import requests
    filepath='test.wav'

    username='********'
    password='********'
    speakername='********' #JKっぽいセレクトにします

    url = 'http://***********' #AITalk提供APIのURL
    params = {
        'username':username,
        'password':password,
        'text':p,
        'speaker_name':speakername,
        'input_type':'text',
        'output_type':'sound',
        'ext':'wav',
        'pitch':'1.50',
        'range':'1.50',
        'style':{'j':'1.0'}
    }
    r = requests.get(url, params=params)


    if( self.getParameter("Temporary storage") ):
        import tempfile
        path = tempfile.mkdtemp() + "/"
    else:
        path = os.path.expanduser('~') + "/recordings/microphones/"

    filepath = path+"test.wav"
    #self.logger.info(filepath)
    with open(filepath, 'wb') as f:
        f.write(r.content)

    self.onStopped(filepath)`

おわり

どのAPIも使いやすく、カスタマイズしやすかったです。
次回ハッカソンでも色々なAPIに挑戦したいです。

個人的にりんなの塩対応がPepperから流れる様子は面白かったです!

12
16
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
12
16