LoginSignup
7
7

More than 5 years have passed since last update.

【Raspberry Pi 3 Model B】男ひとり酒を飲みながらラズパイと会話したい(第4回)

Last updated at Posted at 2018-04-11

概要

docomoのAPIや、Open JTalk等を組み合わせれば、ラズパイと会話ができそうなので作ってみました。
第4回は今までのを組み合わせた音声での会話です。

参考にしたサイト

前提条件

  • モデル : Raspberry Pi 3 Model B
  • OS : Raspbian Stretch
  • マイク(USB)
  • スピーカー(アナログ)
$ uname -a
Linux raspberrypi 4.14.30-v7+ #1102 SMP Mon Mar 26 16:45:49 BST 2018 armv7l GNU/Linux

音声会話

第1回から第3回の組み合わせで、新しいことはしてないつもりなので、いきなりスクリプトです。

$ vi pitalk.py
pitalk.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import requests
import json
import subprocess

APIKEY = '入手したAPIKEY'
url1 = "https://api.apigw.smt.docomo.ne.jp/amiVoice/v1/recognize?APIKEY={}".format(APIKEY)
url2 = "https://api.apigw.smt.docomo.ne.jp/dialogue/v1/dialogue?APIKEY={}".format(APIKEY)

#1回目の会話(context取得のため空実行)
payload = {'utt' : '', 'context': ''}
headers = {'Content-type': 'application/json'}
r = requests.post(url2, data=json.dumps(payload), headers=headers)
context = r.json()['context']

#2回目以降の会話(Ctrl+Zで終了)
while True:
    # 録音
    cmd = [
        'arecord',
        '-D','plughw:0,0',
        '-d','3',
        '-r','16000',
        '-f','S16_LE',
        '/tmp/tmp1.wav'
        ]
    subprocess.call(cmd)
    # 音声認識
    files = {"a": open('/tmp/tmp1.wav', 'rb'), "v":"on"}
    r = requests.post(url1, files=files)
    print r.json()['text']
    # 雑談対話
    payload['utt'] = r.json()['text']
    payload['context'] = context
    payload['t'] = '20'
    r = requests.post(url2, data=json.dumps(payload), headers=headers)
    response = r.json()['utt']
    print "response: %s" %(response)
    # 音声合成
    f = open('/tmp/tmp2.txt', 'w')
    f.write(response.encode('utf-8'))
    f.flush()
    f.close()     
    cmd = [
        'open_jtalk',
        '-m','/usr/share/hts-voice/mei/mei_normal.htsvoice',
        '-x','/var/lib/mecab/dic/open-jtalk/naist-jdic',
        '-ow','/tmp/tmp2.wav','/tmp/tmp2.txt'
        ]
    subprocess.call(cmd)
    cmd = [
        'aplay','/tmp/tmp2.wav'
        ]
    subprocess.call(cmd)
実行
$ python pitalk.py

音声で会話ができれば成功です!!
エンドレスで続きます。。。

まとめ

現状だと、こちらが発言するタイミングが分かりにくいので、BEEP音的なものがあるといいですね。
あとは、Bluetoothリモートシャッターを押すと起動して(参考)、特定の言葉で終了できるようにすれば、実用的かと思います。
気が向いたら番外編でアップデートします。。。

記事

【Raspberry Pi 3 Model B】男ひとり酒を飲みながらラズパイと会話したい(第1回)
【Raspberry Pi 3 Model B】男ひとり酒を飲みながらラズパイと会話したい(第2回)
【Raspberry Pi 3 Model B】男ひとり酒を飲みながらラズパイと会話したい(第3回)
【Raspberry Pi 3 Model B】男ひとり酒を飲みながらラズパイと会話したい(第4回)

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