树莓派+语音控制

首先感谢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

 

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

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值