博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
树莓派+语音控制
阅读量:7044 次
发布时间:2019-06-28

本文共 4894 字,大约阅读时间需要 16 分钟。

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

 

1、树莓派配置

      

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

 

 

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

2、语音聊天和语音控制

 

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

      安装步骤:

       一、

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

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

       二、

1  cd ~2  mkdir scripts3  cd scripts

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

 

     三、

编辑light脚本,内容如下:

#!/bin/bashif [ $# > 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    fifi

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): 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    #获取token111   # 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

 

转载于:https://www.cnblogs.com/babyoliver/p/7114019.html

你可能感兴趣的文章
诊断 Java 代码: 提高 Java 代码的性能 尾递归转换能加快应用程序的速度,但不是所有的 JVM 都会做这种转换...
查看>>
一次数据库hang住的分析过程
查看>>
ArcGIS使用字体文件制作符号库!
查看>>
Cocoa框架类之间的继承关系
查看>>
Windows安全认证是如何进行的?
查看>>
dll文件
查看>>
C# 多线程详解 Part.04(Lock、Monitor、生产与消费)
查看>>
HTTP协议之chunk介绍
查看>>
误区1:数据是可靠的
查看>>
根据IP找到计算机名字(小技巧)
查看>>
遍历jquery的对象
查看>>
system app 下面的apk 修改后不需要重新签名
查看>>
openoj的一个小比赛(F题解题报告)poj3978(dp+素数筛选)
查看>>
【转载】动态代理DynamicProxy 介绍
查看>>
读写cookie的方法
查看>>
淘宝技术发展(Oracle/支付宝/旺旺)
查看>>
分布式版本控制工具 Mercurial 使用教程
查看>>
使用Keil MDK运行第一个STM32程序
查看>>
同时寻找最大数和最小数的最优算法
查看>>
【Visual C++】游戏开发笔记十四 游戏画面绘图(四) 华丽的CImage类
查看>>