基于百度语音和图灵的交互机器人

1.准备工作:

(1)硬件:树莓派3b   usb麦克风   音响

(2)  软件:百度语音api   图灵机器人api  python2或3(树莓派自带)mplayer

 

申请一个百度语音账号,创建一个新应用,并获取相应的Key,截图如下:

注意:每天请求次数有限制,如果不够可以自己向百度申请,免费的

 

申请一个图灵机器人账号,创建新应用,并获取相应的api,截图如下:

 

用命令行 sudo apt-get install mplayer安装mplayer

在安装python的一些第三方库时可能会出现各种各样的问题,我百度了好多解决方法,感觉最好用的就是 sudo apt-get install python-库名,这里默认是python2,如果是3的话,把python换成python3即可。

软硬件到这里基本上就OK了

2.初步测试

(1)图灵机器人测试,从一个能够进行文本交互的机器人开始,代码如下:

 1 import urllib,urllib2
 2 import json
 3 
 4 def gethtml(url):
 5     page = urllib2.urlopen(url)
 6     html = page.read()
 7     return html
 8 
 9 if __name__=='__main__':
10     key = '**********'
11     api = '**********'
12     info = raw_input(‘Me:’)
13     request = api + info
14     response = gethtml(request)
15     dic_json = json.loads(response)
16     print'Robot:'.decode('utf-8')+dic_json['text']

  测试结果机器人还不是太傻,具有初步的撩人功能,附上我们的聊天记录:

(2) 百度语音识别测试,可以自行搜索百度文档,里面有详细的代码样例,代码如下;

import wave
import urllib,urllib2,pycurl
import base64
import json

def get_token():
    apiKey='**********'
    secreKey='**********'
    auth_url="https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credential&client_id="+apiKey+"&client_secret="+secretKey
    res = urllib2.urlopen(auth_url)
    json_data = res.read()
    rerurn json.loads(json_data)['access_token']

def dump_res(buf):
    print (buf)

def use_cloud(token):
    fp = wave.open('niu.wav','rb')
    nf = fp.getnframes()
    f_len = nf*2
    audio_data = fp.readframs(nf)
    cuid = "******"
    srv_url = 'http://vop.baidu.com/server_api'+'?cuid='+cuid+'&token='+token
    http_header=['Content-Type:audio/pcm;rate=8000','Content-Length: %d'%f_len]
    c=pycurl.Curl()
    c.setopt(pycurl.URL,str(srv_url))
    c.setopt(c.HTTPHEADER,http_header)
    c.setopt(c.POST,1)
    c.setopt(c.CONNECTTIMEOUT,30)
    c.setopt(c.TIMEOUT,30)
    c.setopt(c.WRUTEFUNCTION,dump_res)
    c.setopt(c.POSTFIELDS,audio_data)
    c.setopt(c.POSTFIELDSIZE,f_len)
    c.perform()

if __name__=='__main__':
    token=get_token()
    use_cloud(token)

  测试结果发现基本上都能识别准确,但偶尔会因未对噪音进行处理而出现偏差,上图:

 (3)  麦克风和音响测试

  用命令行 sudo arecord -D "plughw;1,0" 5 niu.wav录制一段5秒的音频

  用命令行 omxplayer -o local niu.wav即可播放刚刚录制的音频

  注意:树莓派支持3.5mm音频输出和HDMI音频输出,可以通过config界面进行控制,具体方法可自已解决


如果以上测试都成功的话,下面就很简单了,只是把上述内容整合一下即可

3.源代码

import json
import wave,urllib,urllib2,pycurl,base64
import os,time from aip import AipSpeech def gethtml(url): page = urllib2.urlopen(url) html = page.read() return html def get_token(): apiKey='**********' secreKey='**********' auth_url="https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credential&client_id="+apiKey+"&client_secret="+secretKey res = urllib2.urlopen(auth_url) json_data = res.read() rerurn json.loads(json_data)['access_token'] def dump_res(buf): global duihua print(buf) a=eval(buf) if a['err_msg']=='success.': duihua =a['result'][0] print duihua def use_cloud(token): fp = wave.open('niu.wav','rb') nf = fp.getnframes() f_len = nf*2 audio_data = fp.readframs(nf) cuid = "******" srv_url = 'http://vop.baidu.com/server_api'+'?cuid='+cuid+'&token='+token http_header=['Content-Type:audio/pcm;rate=8000','Content-Length: %d'%f_len] c=pycurl.Curl() c.setopt(pycurl.URL,str(srv_url)) c.setopt(c.HTTPHEADER,http_header) c.setopt(c.POST,1) c.setopt(c.CONNECTTIMEOUT,30) c.setopt(c.TIMEOUT,30) c.setopt(c.WRUTEFUNCTION,dump_res) c.setopt(c.POSTFIELDS,audio_data) c.setopt(c.POSTFIELDSIZE,f_len) c.perform() def answer(): key = '********' api ='http://www.tuling123.com/openapi/api?key='+key+'&info=' info =duihua request = api+info response = gethtml(request) return response
def getmp3():
   APP_ID='*********'
APP_KEY='********'
SECRET_KEY='*********'
  client=AipSpeech(APP_ID,APP_KEY,SECRET_KEY)  
  result = client.sysnthesis(a,'zh',1,{'vol':5,})
  if not isinstance(result,dict):
    with open('niu.mp3','wb') as f:
    f.write(result)
while True: 
  
print'Speaker:'
  os.ayatem(
'sudo arecord -D "plughw:1,0" -f S16_LE -d 4 -r 8000 niu.wav')
token
=get_token()
use_cloud(token)
dic_json
= json.loads(answer())
a
=dic_json['text'].encode('utf-8')
  getmp3() os.system('mplayer niu.mp3') time.sleep(0.5)

4.总结 

  总的来说,中间没有遇到太多复杂问题,而且最后的实测效果还是不错,但是仍然有反应慢,识别不准等问题需要进一步解决,另外还需要加上语音唤醒,以及语音结束等相关功能,代码有错的地方希望大家能够多多指正。(测试视频就不发了,因为我没有公众平台,尴尬,不过是有的喔,嘿嘿,想要的我可以私发

 

转载于:https://www.cnblogs.com/niuyisan/p/8430985.html

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值