LoginSignup
1
1

More than 5 years have passed since last update.

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

Last updated at Posted at 2018-04-11

概要

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

参考にしたサイト

前提条件

  • モデル : 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

docomo API利用申請

docomo Developer supportのアカウントが必要なので作成します(とりあえず無料です)。
API利用申請・管理から「雑談対話」と「音声認識【Powered by アドバンスト・メディア】」を申請します。APIkeyが発行されるのでメモります。

事前準備

とりあえずのお約束作業です。

$ sudo apt-get update
$ sudo apt-get upgrade

USBマイクの認識

lsusbコマンドでUSBマイクが認識しているか確認します。USBマイクを抜いて実行、挿して実行で増えてればOKでしょう。

$ lsusb

次にUSBオーディオモジュールの優先順位の確認し、最優先に設定します。

$ cat /proc/asound/modules
 0 snd_bcm2835
 1 snd_usb_audio # これがUSBマイク 0であれば最優先だが1なので要変更
$ sudo vi /etc/modprobe.d/alsa-base.conf
options snd slots=snd_usb_audio,snd_bcm2835
options snd_usb_audio index=0
options snd_bcm2835 index=1
$ sudo reboot

~再起動~

$ cat /proc/asound/modules
 0 snd_usb_audio
 1 snd_bcm2835

マイクとスピーカーのデバイスの確認。以下の例だと、マイクは card 0 device 0 、スピーカーは card 1 device 0 らしい。

$ arecord -l # 録音
**** ハードウェアデバイス CAPTURE のリスト ****
カード 0: Device [USB PnP Sound Device], デバイス 0: USB Audio [USB Audio]
  サブデバイス: 1/1
  サブデバイス #0: subdevice #0

$ aplay -l # 再生
**** ハードウェアデバイス CAPTURE のリスト ****
**** ハードウェアデバイス PLAYBACK のリスト ****
カード 1: ALSA [bcm2835 ALSA], デバイス 0: bcm2835 ALSA [bcm2835 ALSA]
  サブデバイス: 7/7
  サブデバイス #0: subdevice #0
  サブデバイス #1: subdevice #1
  サブデバイス #2: subdevice #2
  サブデバイス #3: subdevice #3
  サブデバイス #4: subdevice #4
  サブデバイス #5: subdevice #5
  サブデバイス #6: subdevice #6
カード 1: ALSA [bcm2835 ALSA], デバイス 1: bcm2835 ALSA [bcm2835 IEC958/HDMI]
  サブデバイス: 1/1
  サブデバイス #0: subdevice #0

では、動作確認。

$ arecord -D plughw:0,0 -d 5 -r 16000 -f S16_LE test.wav # 5秒録音 マイクに向かってしゃべってください
$ aplay -D hw:1,0 test.wav # 再生

docomoAPIを使用した音声認識

・・・の前にPython環境整備。

$ sudo apt-get install python-pip
$ sudo pip install requests

メモったAPIKEYとさっきマイクで録音したファイルで音声認識できるかな?

$ python
>>> import requests
>>> path = '/home/pi/test.wav'
>>> 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)
>>> print r.json()['text']
こんにちは。
>>> exit()

録音した声がテキストになって出力されれば成功です!!

まとめ

第1回でテキストを音声に、今回の第2回では音声をテキストにできたので、次回は会話に挑戦したいです。

記事

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

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