docomo APIによるraspepbrry piの音声会話
# -*- coding: utf-8 -*-
#マイク0番からの入力を受ける。一定時間(RECROD_SECONDS)だけ録音し、ファイル名: mono.wavで保存する。
import pyaudio
import sys
import time
import wave
import requests
import os
import json
def recognize():
url = "https://api.apigw.smt.docomo.ne.jp/amiVoice/v1/recognize?APIKEY={}".format(APIKEY)
files = {"a": open(PATH, 'rb'), "v":"on"}
r = requests.post(url, files=files)
message = r.json()['text']
print message
return message
def dialogue(message="こんにちは"):
url = "https://api.apigw.smt.docomo.ne.jp/dialogue/v1/dialogue?APIKEY={}".format(APIKEY)
payload = {
"utt": message,
"context": "",
"nickname": "光",
"nickname_y": "ヒカリ",
"sex": "女",
"bloodtype": "B",
"birthdateY": "1997",
"birthdateM": "5",
"birthdateD": "30",
"age": "16",
"constellations": "双子座",
"place": "東京",
"mode": "dialog",
"t":20
}
r = requests.post(url, data=json.dumps(payload))
print r.json()['utt']
return r.json()['utt']
def talk(message="こんにちは", card=1, device=0):
os.system('/home/pi/aquestalkpi/AquesTalkPi " ' + message.encode('utf-8') + ' " | aplay -Dhw:{},{}').format(card, device)
if __name__ == '__main__':
chunk = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
PATH = '/var/tmp/tmp.wav'
APIKEY='xxxxxxxxxxxxx' #DocomoAPI Key
CARD = 1 #OUTPUTの指定
DEVICE = 0 #OUTPUTの指定
#サンプリングレート、マイク性能に依存
RATE = 16000
#録音時間
RECORD_SECONDS = input('Please input recoding time>>>')
#pyaudio
p = pyaudio.PyAudio()
#マイク0番を設定
input_device_index = 0
#マイクからデータ取得
stream = p.open(format = FORMAT,
channels = CHANNELS,
rate = RATE,
input = True,
frames_per_buffer = chunk)
all = []
for i in range(0, RATE / chunk * RECORD_SECONDS):
data = stream.read(chunk)
all.append(data)
stream.close()
data = ''.join(all)
out = wave.open(PATH,'w')
out.setnchannels(1) #mono
out.setsampwidth(2) #16bits
out.setframerate(RATE)
out.writeframes(data)
out.close()
p.terminate()
message = recognize()
talk_message = dialogue(message)
talk(talk_message, CARD, DEVICE)
以上のpythonを実行しようとすると
Please input recoding time>>>3
ALSA lib pcm_dmix.c:1022:(snd_pcm_dmix_open) unable to open slave
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pulse.c:243:(pulse_connect) PulseAudio: Unable to connect: Connection refused
ALSA lib pulse.c:243:(pulse_connect) PulseAudio: Unable to connect: Connection refused
ALSA lib pcm_dmix.c:1022:(snd_pcm_dmix_open) unable to open slave
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
こんにちは!話しかけてくれるのを待ってた
ALSA lib conf.c:1701:(snd_config_load1) :1:1:Unexpected }
ALSA lib conf.c:4722:(snd_config_expand) Parse arguments error: 無効な引数です
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM hw:{},{}
aplay: main:722: audio open error: 無効な引数です
Traceback (most recent call last):
File "dialogue_test.py", line 87, in <module>
talk(talk_message, CARD, DEVICE)
File "dialogue_test.py", line 43, in talk
os.system('/home/pi/aquestalkpi/AquesTalkPi " ' + message.encode('utf-8') + ' " | aplay -Dhw:{},{}').format(card,device)
AttributeError: 'int' object has no attribute 'format'
というふうに最後のところでエラーになってしまいます
どうすればいいでしょうか
P.S.
DcomoAPIKEYとRATEは正しく入力しています
参考サイトはQiita:簡単にできる!音声認識と音声合成を使ってRaspberrypiと会話です
最初の大量の文字列(”こんにちは!話しかけてくれるのを待ってた”より前は警告
ですがエラーにはなっていないようなので無視していいと思います
使用していいるものは音声認識と対話にDocomo API 録音に pyaudio 発声にAquestalkです
raspberry piを 使っています