600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > 树莓派+语音控制

树莓派+语音控制

时间:2021-05-11 17:25:57

相关推荐

树莓派+语音控制

首先感谢Eoman博友,博文很大一部分参考他的去实现,这里楼主因为树莓派不在身边,也截取一些Eoman的图片,再次感谢Eoman在工作的帮助。

1、树莓派配置

树莓派3代去实现语音控制,便于语音控制,这里要进行sudo raspi-config的配置。

根据自己实际的系统,选择expand filesystem的一项,确定后sudo reboot

2、语音聊天和语音控制

这里选用Eoman的控制方法:使用的是wiringPi(C语言)

安装步骤:

一、

1 sudo apt-get install git-core2 git clone git:///wiringPi3 cd wiringPi4 ./build

为了便于控制引脚高低,新建一个文件夹。

二、

1 cd ~2 mkdir scripts3 cd scripts

新建的文件夹里保存控制电平高低的脚本。

三、

编辑light脚本,内容如下:

#!/bin/bashif [ $# > 1 ]then/usr/local/bin/gpio mode 4 outif [[ "$1" = "on" ]]then/usr/local/bin/gpio write 4 onfiif [[ "$1" = "off" ]]then/usr/local/bin/gpio write 4 offfifi

light脚本里的mode 4 是以board编码方式来实现的,参照自己Pi的引脚图,对照出实际的引脚。

也可以根据自己的爱好,自行改变引脚号。

四、赋给脚本权限

1 chmod u+x light2 chmod u+x 1

检验引脚是否可以通过命令行控制。

1 ./light on2 ./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): 27page = urllib.urlopen(url) 28html = page.read() 29return html 30 31 def get_token(): 32apiKey = "jpRPyTHnGgQ0u011uKZ0" 33secretKey = "d07fa6c332810e9956cf5d1b" 34auth_url = "/oauth/2.0/token?grant_type=client_credentials&client_id=" + apiKey + "&client_secret=" + secretKey; 35res = urllib2.urlopen(auth_url) 36json_data = res.read() 37return json.loads(json_data)['access_token'] 38 39 def dump_res(buf): 40#global duihua 41global res 42print "字符串类型" 43print (buf) 44a = eval(buf) 45print type(a) 46if a['err_msg']=='success.': 47 res = a['result'][0] #可以在这里输出返回的语句 48else: 49 res="" 50 #print duihua 51 52 def use_cloud(token): 53fp = wave.open(filename, 'rb') 54nf = fp.getnframes() 55f_len = nf * 2 56audio_data = fp.readframes(nf) 57cuid = "9691607" #产品id 58srv_url = '/server_api' + '?cuid=' + cuid + '&token=' + token 59http_header = [ 60 'Content-Type: audio/pcm; rate=8000', 61 'Content-Length: %d' % f_len 62] 63 64c = pycurl.Curl() 65c.setopt(pycurl.URL, str(srv_url)) #curl doesn't support unicode 66#c.setopt(c.RETURNTRANSFER, 1) 67c.setopt(c.HTTPHEADER, http_header) #must be list, not dict 68c.setopt(c.POST, 1) 69c.setopt(c.CONNECTTIMEOUT, 30) 70c.setopt(c.TIMEOUT, 30) 71c.setopt(c.WRITEFUNCTION, dump_res) 72c.setopt(c.POSTFIELDS, audio_data) 73c.setopt(c.POSTFIELDSIZE, f_len) 74c.perform() #pycurl.perform() has no return val 75 76 # 将data中的数据保存到名为filename的WAV文件中 77 def save_wave_file(filename, data): 78wf = wave.open(filename, 'wb') 79wf.setnchannels(1) 80wf.setsampwidth(2) 81wf.setframerate(SAMPLING_RATE) 82wf.writeframes("".join(data)) 83wf.close() 84 85 token = get_token() 86 #key = 'd07fa6c332810e9956cf5d1bc0f4ee5f' 87 while(True): 88os.system('arecord -D "plughw:1,0" -f S16_LE -d3 -r 8000 /home/pi/asr.wav') 89use_cloud(token) 90print "-----> return result:"+commun[0] 91print res 92if "开" in res: #在返回的文本里寻找“开” 93 answer = '好的,正在为您开启,请稍后' 94 url = "/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') 97if "关" in res: 98 answer = '好的,正在为您关闭,请稍后' 99 url = "/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')102if "兰州理工大学" in res: #在返回的文本里寻找“开”103 answer = '兰州理工大学位于甘肃省兰州市'104 url = "/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 #获取token111 # use_cloud(token)112 #进行处理,输出在函数内部

实现视频如下:

参考链接:

/eoman/p/5621928.html

/benhuo931115/article/details/54342145

/qinxiandiqi/article/details/39136195

/qazwyc/article/details/57153734

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。