首先感謝Eoman博友,博文很大一部分參考他的去實現,這里樓主因為樹莓派不在身邊,也截取一些Eoman的圖片,再次感謝Eoman在工作的幫助。
1、樹莓派配置
樹莓派3代去實現語音控制,便於語音控制,這里要進行sudo raspi-config的配置。
根據自己實際的系統,選擇expand filesystem的一項,確定后sudo reboot
2、語音聊天和語音控制
這里選用Eoman的控制方法:使用的是wiringPi(C語言)
安裝步驟:
一、
1 sudo apt-get install git-core 2 git clone git://git.drogon.net/wiringPi 3 cd wiringPi 4 ./build
為了便於控制引腳高低,新建一個文件夾。
二、
1 cd ~ 2 mkdir scripts 3 cd scripts
新建的文件夾里保存控制電平高低的腳本。
三、
編輯light腳本,內容如下:
#!/bin/bash if [ $# > 1 ] then /usr/local/bin/gpio mode 4 out if [[ "$1" = "on" ]] then /usr/local/bin/gpio write 4 on fi if [[ "$1" = "off" ]] then /usr/local/bin/gpio write 4 off fi fi
light腳本里的mode 4 是以board編碼方式來實現的,參照自己Pi的引腳圖,對照出實際的引腳。
也可以根據自己的愛好,自行改變引腳號。
四、賦給腳本權限
1 chmod u+x light 2 chmod u+x 1
檢驗引腳是否可以通過命令行控制。
1 ./light on 2 ./light off
實際還是語言檢測部分不是很靈敏,需要調試好代碼部分的時間。試驗中選用百度語音API。
1 #usr/bin/python 2 # -*- coding: utf-8 -*- 3 4 import numpy as np 5 from datetime import datetime 6 import wave 7 import time 8 import urllib, urllib2, pycurl 9 import base64 10 import json 11 import os 12 import sys 13 14 reload(sys) 15 sys.setdefaultencoding( "utf-8" ) 16 17 save_count = 0 18 save_buffer = [] 19 t = 0 20 sum = 0 21 time_flag = 0 22 flag_num = 0 23 filename = 'asr.wav' 24 commun = '1' 25 answer = '1' 26 def getHtml(url): 27 page = urllib.urlopen(url) 28 html = page.read() 29 return html 30 31 def get_token(): 32 apiKey = "jpRPyTHnGgQ0u011uKZ0" 33 secretKey = "d07fa6c332810e9956cf5d1b" 34 auth_url = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=" + apiKey + "&client_secret=" + secretKey; 35 res = urllib2.urlopen(auth_url) 36 json_data = res.read() 37 return json.loads(json_data)['access_token'] 38 39 def dump_res(buf): 40 #global duihua 41 global res 42 print "字符串類型" 43 print (buf) 44 a = eval(buf) 45 print type(a) 46 if a['err_msg']=='success.': 47 res = a['result'][0] #可以在這里輸出返回的語句 48 else: 49 res="" 50 #print duihua 51 52 def use_cloud(token): 53 fp = wave.open(filename, 'rb') 54 nf = fp.getnframes() 55 f_len = nf * 2 56 audio_data = fp.readframes(nf) 57 cuid = "9691607" #產品id 58 srv_url = 'http://vop.baidu.com/server_api' + '?cuid=' + cuid + '&token=' + token 59 http_header = [ 60 'Content-Type: audio/pcm; rate=8000', 61 'Content-Length: %d' % f_len 62 ] 63 64 c = pycurl.Curl() 65 c.setopt(pycurl.URL, str(srv_url)) #curl doesn't support unicode 66 #c.setopt(c.RETURNTRANSFER, 1) 67 c.setopt(c.HTTPHEADER, http_header) #must be list, not dict 68 c.setopt(c.POST, 1) 69 c.setopt(c.CONNECTTIMEOUT, 30) 70 c.setopt(c.TIMEOUT, 30) 71 c.setopt(c.WRITEFUNCTION, dump_res) 72 c.setopt(c.POSTFIELDS, audio_data) 73 c.setopt(c.POSTFIELDSIZE, f_len) 74 c.perform() #pycurl.perform() has no return val 75 76 # 將data中的數據保存到名為filename的WAV文件中 77 def save_wave_file(filename, data): 78 wf = wave.open(filename, 'wb') 79 wf.setnchannels(1) 80 wf.setsampwidth(2) 81 wf.setframerate(SAMPLING_RATE) 82 wf.writeframes("".join(data)) 83 wf.close() 84 85 token = get_token() 86 #key = 'd07fa6c332810e9956cf5d1bc0f4ee5f' 87 while(True): 88 os.system('arecord -D "plughw:1,0" -f S16_LE -d3 -r 8000 /home/pi/asr.wav') 89 use_cloud(token) 90 print "-----> return result:"+commun[0] 91 print res 92 if "開" in res: #在返回的文本里尋找“開” 93 answer = '好的,正在為您開啟,請稍后' 94 url = "http://tsn.baidu.com/text2audio?tex="+answer+"&lan=zh&per=0&pit=1&spd=7&cuid=9691607&ctp=1&tok=24.de7d08e3164eed77f61cfcd1b2c0.2592000.1499256984.282335-9729638" 95 os.system('mplayer "%s"'%(url)) 96 os.system('cd /home/pi/scripts&&./light on') 97 if "關" in res: 98 answer = '好的,正在為您關閉,請稍后' 99 url = "http://tsn.baidu.com/text2audio?tex="+answer+"&lan=zh&per=0&pit=1&spd=7&cuid=9691607&ctp=1&tok=24.de7d08e3164eed77f61cfc8a7cd1b2c0.2592000.1499256984.282335-9729638" 100 os.system('mplayer "%s"'%(url)) 101 os.system('cd /home/pi/scripts&&./light off') 102 if "蘭州理工大學" in res: #在返回的文本里尋找“開” 103 answer = '蘭州理工大學位於甘肅省蘭州市' 104 url = "http://tsn.baidu.com/text2audio?tex="+answer+"&lan=zh&per=0&pit=1&spd=7&cuid=9691607&ctp=1&tok=24.de7d08e3164eed777cd1b2c0.2592000.1499256984.282335-9729638" 105 os.system('mplayer "%s"'%(url)) 106 os.system('cd /home/pi/scripts&&./light on') 107 108 #if _name_ == "_main_": 109 # token = get_token() 110 #獲取token 111 # use_cloud(token) 112 #進行處理,輸出在函數內部
實現視頻如下:
參考鏈接:
http://www.cnblogs.com/eoman/p/5621928.html
http://blog.csdn.net/benhuo931115/article/details/54342145
http://blog.csdn.net/qinxiandiqi/article/details/39136195
http://blog.csdn.net/qazwyc/article/details/57153734