動作概要
- コマンド入力スクリプトはリモコンから起動
- ピッという発信音の後5秒間録音してGoogle音声認識APIにかける
- Google音声認識APIの結果からコマンドかどうか判断
ソース
----
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding:utf-8 | |
require 'pp' | |
require 'uri' | |
require 'json' | |
require 'net/http' | |
require 'fileutils' | |
WAVFILE="/tmp/voice.wav" | |
FLACFILE="/tmp/voice.flac" | |
def start_alarm | |
`/home/pi/aquestalkpi/AquesTalkPi -b -g 100 "ピッ" | aplay` | |
end | |
Net::HTTP.version_1_2 | |
def voicerecog | |
start_alarm | |
`arecord -D plughw:1,0 -d 5 -f cd #{WAVFILE}` | |
FileUtils.rm_f(FLACFILE) | |
`ffmpeg -i /tmp/voice.wav -vn -ac 1 -ar 16000 -acodec flac #{FLACFILE}` | |
uri = URI.parse("https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=ja&maxresults=6&pfilter=2") | |
req = Net::HTTP::Post.new(uri.request_uri) | |
req.body = File.read(FLACFILE) | |
req.content_type = "audio/x-flac; rate=16000" | |
req.content_length = req.body.size | |
req['User-Agent'] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11" | |
#req['Accept-Encoding'] = "gzip,deflate,sdch" | |
req['Accept-Language'] = "ja,en-US;q=0.8,en;q=0.6" | |
req['Accept-Charset'] = "utf-8;q=0.7,*;q=0.3" | |
http = Net::HTTP.new(uri.host,uri.port) | |
http.use_ssl = true | |
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
http.start do |h| | |
res = h.request(req) | |
result = JSON.parse(res.body) | |
command = result["hypotheses"][0]["utterance"] | |
return command | |
end | |
end | |
# main | |
command = voicerecog | |
case command | |
when "天気予報","てんきよほう" | |
`/home/pi/aquestalkpi/AquesTalkPi -b -g 100 "天気予報" | aplay` | |
when "タイムライン","timeline" | |
`/home/pi/aquestalkpi/AquesTalkPi -b -g 100 "タイムライン" | aplay` | |
when "ツイート" | |
`/home/pi/aquestalkpi/AquesTalkPi -b -g 100 "ツイート" | aplay` | |
end | |
----
プロトタイプなので「天気予報」などのコマンドの中身は実装していない。
状態遷移で複雑な入力もできるし、対話型にしてもいいかも知んない。
色々できそうではあるんだがいかんせんヤル気が・・・
色々できそうではあるんだがいかんせんヤル気が・・・
0 件のコメント:
コメントを投稿