基于树莓派的语音对话机器人


Posted in Python onJune 17, 2019

本文实例为大家分享了基于树莓派的语音对话机器人,供大家参考,具体内容如下

第一部分代码

arecord -D "plughw:1" -f S16_LE -r 16000 -d 3 /home/pi/Desktop/voice.wav

第二部分代码

# coding: utf-8
import sys 
import json 
import urllib2 
import base64 
import requests
reload(sys) 
sys.setdefaultencoding(“utf-8”)
def get_access_token(): 
url = “https://openapi.baidu.com/oauth/2.0/token” 
body = { 
“grant_type”:”client_credentials”, 
“client_id” :”此处填写自己的client_id”, 
“client_secret”:”此处填写自己的client_secret”, 
}
r = requests.post(url,data=body,verify=True)
respond = json.loads(r.text)
return respond["access_token"]
def yuyinshibie_api(audio_data,token): 
speech_data = base64.b64encode(audio_data).decode(“utf-8”) 
speech_length = len(audio_data) 
post_data = { 
“format” : “wav”, 
“rate” : 16000, 
“channel” : 1, 
“cuid” : “B8-27-EB-BA-24-14”, 
“token” : token, 
“speech” : speech_data, 
“len” : speech_length 
}
url = "http://vop.baidu.com/server_api"
json_data = json.dumps(post_data).encode("utf-8")
json_length = len(json_data)
#print(json_data)
 
req = urllib2.Request(url, data=json_data)
req.add_header("Content-Type", "application/json")
req.add_header("Content-Length", json_length)
 
#print("asr start request\n")
resp = urllib2.urlopen(req)
#print("asr finish request\n")
resp = resp.read()
resp_data = json.loads(resp.decode("utf-8"))
if resp_data["err_no"] == 0:
  return resp_data["result"]
else:
  print(resp_data)
  return None
def asr_main(filename,tok): 
try: 
f = open(filename, “rb”) 
audio_data = f.read() 
f.close() 
resp = yuyinshibie_api(audio_data,tok) 
return resp[0] 
except Exception,e: 
print “e:”,e 
return “识别失败”.encode(“utf-8”)

第三部分代码

# coding: utf-8
 
import requests
import json
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
 
 
def Tuling(words):
  Tuling_API_KEY = "此处填写自己的Turling KEY"
 
  body = {"key":Tuling_API_KEY,"info":words.encode("utf-8")}
 
  url = "http://www.tuling123.com/openapi/api"
  r = requests.post(url,data=body,verify=True)
 
  if r:
    date = json.loads(r.text)
    print date["text"]
    return date["text"]
  else:
    return None

第四部分代码

# coding: utf-8
import sys 
import urllib2 
import json 
import os 
import yuyinshibie
reload(sys) 
sys.setdefaultencoding("utf-8")
def yuyinhecheng_api(tok,tex): 
  cuid = "B8-27-EB-BA-24-14" 
  spd = "4" 
  url = "http://tsn.baidu.com/text2audio?tex="+tex+"&lan=zh&cuid="+cuid+"&ctp=1&tok="+tok+"&per=3" 
  #print url 
  #response = requests.get(url) 
  #date = response.read() 
  return url
  def tts_main(filename,words,tok): 
    voice_date = yuyinhecheng_api(tok,words)
    f = open(filename,"wb")
    f.write(voice_date)
    f.close()

第五部分代码

# coding: utf-8
 
import os
import time
import yuyinhecheng
import Turling
import yuyinshibie
 
 
tok = yuyinshibie.get_access_token()
 
switch = True
while switch:
  os.system('sudo arecord -D "plughw:1" -f S16_LE -r 16000 -d 3 /home/pi/Desktop/voice.wav')
  time.sleep(0.5)
  info = yuyinshibie.asr_main("/home/pi/Desktop/voice.wav",tok)
  if '关闭'.encode("utf-8") in info:
    while True:
      os.system('sudo arecord -D "plughw:1" -f S16_LE -r 16000 -d 10 /home/pi/Desktop/voice.wav')
      time.sleep(10)
 
      info = yuyinshibie.asr_main("/home/pi/Desktop/voice.wav",tok)
      if '开启'.encode("utf-8") in info:
        break
 
    url = "http://tsn.baidu.com/text2audio?tex=开启成功&lan=zh&cuid=B8-27-EB-BA-24-14&ctp=1&tok="+tok+"&per=3"
    os.system('mpg123 "%s"'%url)
 
 
  elif '暂停'.encode("utf-8") in info:
    url = "http://tsn.baidu.com/text2audio?tex=开始暂停&lan=zh&cuid=B8-27-EB-BA-24-14&ctp=1&tok="+tok+"&per=3"
    os.system('mpg123 "%s"'%url)
    time.sleep(10)
 
    url = "http://tsn.baidu.com/text2audio?tex=暂停结束&lan=zh&cuid=B8-27-EB-BA-24-14&ctp=1&tok="+tok+"&per=3"
    os.system('mpg123 "%s"'%url)
    continue
 
 
  else:
    tex = Turling.Tuling(info)
    url = yuyinhecheng.yuyinhecheng_api(tok,tex)
    os.system('mpg123 "%s"'%url)
    time.sleep(0.5)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
py2exe 编译ico图标的代码
Mar 08 Python
django文档学习之applications使用详解
Jan 29 Python
Flask模拟实现CSRF攻击的方法
Jul 24 Python
Python使用sklearn库实现的各种分类算法简单应用小结
Jul 04 Python
Python+selenium点击网页上指定坐标的实例
Jul 05 Python
python实现在函数图像上添加文字和标注的方法
Jul 08 Python
python批量解压zip文件的方法
Aug 20 Python
Django 在iframe里跳转顶层url的例子
Aug 21 Python
Python3直接爬取图片URL并保存示例
Dec 18 Python
Pycharm中安装Pygal并使用Pygal模拟掷骰子(推荐)
Apr 08 Python
python实现测试工具(一)——命令行发送get请求
Oct 19 Python
Python+OpenCV检测灯光亮点的实现方法
Nov 02 Python
PyQt5 QListWidget选择多项并返回的实例
Jun 17 #Python
Pyqt清空某一个QTreeewidgetItem下的所有分支方法
Jun 17 #Python
使用python进行波形及频谱绘制的方法
Jun 17 #Python
PyQt5图形界面播放音乐的实例
Jun 17 #Python
PyQt5 在label显示的图片中绘制矩形的方法
Jun 17 #Python
PyQt5显示GIF图片的方法
Jun 17 #Python
详解pytorch 0.4.0迁移指南
Jun 16 #Python
You might like
php二维数组排序详解
2013/11/06 PHP
php curl post 时出现的问题解决
2014/01/30 PHP
php实现ip白名单黑名单功能
2015/03/12 PHP
php中session_id()函数详细介绍,会话id生成过程及session id长度
2015/09/23 PHP
php使用pdo连接sqlite3的配置示例
2016/05/27 PHP
laravel实现按月或天或小时统计mysql数据的方法
2019/10/09 PHP
js获取当前页面路径示例讲解
2014/01/08 Javascript
js导出txt示例代码
2014/01/14 Javascript
javaScript中两个等于号和三个等于号之间的区别介绍
2014/06/27 Javascript
JavaScript中的splice()方法使用详解
2015/06/09 Javascript
js+CSS实现模拟华丽的select控件下拉菜单效果
2015/09/01 Javascript
基于CSS3和jQuery实现跟随鼠标方位的Hover特效
2016/07/25 Javascript
jQuery3.0中的buildFragment私有函数详解
2016/08/16 Javascript
AngularJS实现路由实例
2017/02/12 Javascript
浅谈JS验证表单文本域输入空格的问题
2017/02/14 Javascript
JS实现基于Sketch.js模拟成群游动的蝌蚪运动动画效果【附demo源码下载】
2017/08/18 Javascript
Nodejs进阶之服务端字符编解码和乱码处理
2017/09/04 NodeJs
vue 利用路由守卫判断是否登录的方法
2018/09/29 Javascript
Python使用struct处理二进制的实例详解
2017/09/11 Python
对python 多个分隔符split 的实例详解
2018/12/20 Python
浅谈Python 递归算法指归
2019/08/22 Python
利用python中集合的唯一性实现去重
2020/02/11 Python
使用pymysql查询数据库,把结果保存为列表并获取指定元素下标实例
2020/05/15 Python
PyQt5实现仿QQ贴边隐藏功能的实例代码
2020/05/24 Python
Python多线程正确用法实例解析
2020/05/30 Python
浅析python实现动态规划背包问题
2020/12/31 Python
canvas之万花筒效果的简单实现(推荐)
2016/08/16 HTML / CSS
KENZO官网:高田贤三在法国创立的品牌
2019/05/16 全球购物
成人大专生实习期的自我评价
2013/10/02 职场文书
单位未婚证明范本
2014/01/18 职场文书
企业内部培训方案
2014/02/04 职场文书
最经典的大学生职业生涯规划范文
2014/03/05 职场文书
酒店管理求职信范文
2014/04/06 职场文书
2014年内部审计工作总结
2014/12/09 职场文书
幼儿园园长个人总结
2015/03/02 职场文书
使用MybatisPlus打印sql语句
2022/04/22 SQL Server