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


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 相关文章推荐
python实现异步回调机制代码分享
Jan 10 Python
Python中optionParser模块的使用方法实例教程
Aug 29 Python
发布你的Python模块详解
Sep 15 Python
python实现支付宝当面付(扫码支付)功能
May 30 Python
详解python函数的闭包问题(内部函数与外部函数详述)
May 17 Python
java判断三位数的实例讲解
Jun 10 Python
PyQt4编程之让状态栏显示信息的方法
Jun 18 Python
Tensorflow 卷积的梯度反向传播过程
Feb 10 Python
python with语句的原理与用法详解
Mar 30 Python
解决Alexnet训练模型在每个epoch中准确率和loss都会一升一降问题
Jun 17 Python
如何使用Python自动生成报表并以邮件发送
Oct 15 Python
在NumPy中深拷贝和浅拷贝相关操作的定义和背后的原理
Apr 14 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获取网站域名和地址的代码
2008/08/17 PHP
php/js获取客户端mac地址的实现代码
2013/07/08 PHP
详解WordPress中过滤链接与过滤SQL语句的方法
2015/12/18 PHP
php+mysql实现的无限分类方法类定义与使用示例
2020/05/27 PHP
var与Javascript变量隐式声明
2009/09/17 Javascript
javascript开发随笔二 动态加载js和文件
2011/11/25 Javascript
Jquery index()方法 获取相应元素索引值
2012/10/12 Javascript
JavaScript 数组详解
2013/10/10 Javascript
nodejs调用cmd命令实现复制目录
2015/05/04 NodeJs
js实现精美的银灰色竖排折叠菜单
2015/05/16 Javascript
JS简单实现浮动窗口效果示例
2016/09/07 Javascript
js判断文件格式及大小的简单实例(必看)
2016/10/11 Javascript
JS插件plupload.js实现多图上传并显示进度条
2016/11/29 Javascript
Node.js使用Koa搭建 基础项目
2018/01/08 Javascript
解决vue的变量在settimeout内部效果失效的问题
2018/08/30 Javascript
使用koa-log4管理nodeJs日志笔记的使用方法
2018/11/30 NodeJs
js核心基础之构造函数constructor用法实例分析
2019/05/11 Javascript
使用Nginx+uWsgi实现Python的Django框架站点动静分离
2016/03/21 Python
在Python中实现替换字符串中的子串的示例
2018/10/31 Python
python实现简单的文字识别
2018/11/27 Python
解决yum对python依赖版本问题
2019/07/05 Python
python获取引用对象的个数方式
2019/12/20 Python
python def 定义函数,调用函数方式
2020/06/02 Python
Python正则re模块使用步骤及原理解析
2020/08/18 Python
开发人员所需要知道的HTML5性能分析面面观
2012/07/05 HTML / CSS
Auchan Direct波兰:欧尚在线杂货店
2016/10/19 全球购物
Volcom法国官网:美国冲浪滑板品牌
2017/05/25 全球购物
印刷工程专业应届生求职信
2013/09/29 职场文书
大一自我鉴定范文
2013/10/04 职场文书
公关关系专员的自我评价分享
2013/11/20 职场文书
绿色环保标语
2014/06/12 职场文书
六一儿童节活动总结
2014/08/27 职场文书
管理失职检讨书范文
2015/05/05 职场文书
《绝招》教学反思
2016/02/20 职场文书
SQL Server中的游标介绍
2022/05/20 SQL Server
python实现学员管理系统(面向对象版)
2022/06/05 Python