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


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 str与repr的区别
Mar 23 Python
Python基于Tkinter实现的记事本实例
Jun 17 Python
浅谈Python的Django框架中的缓存控制
Jul 24 Python
python实现按行分割文件
Jul 22 Python
Python 二叉树的层序建立与三种遍历实现详解
Jul 29 Python
Python 实现黑客帝国中的字符雨的示例代码
Feb 20 Python
python实现手势识别的示例(入门)
Apr 15 Python
python模拟哔哩哔哩滑块登入验证的实现
Apr 24 Python
浅谈Python中threading join和setDaemon用法及区别说明
May 02 Python
Win10用vscode打开anaconda环境中的python出错问题的解决
May 25 Python
python中pivot()函数基础知识点
Jan 03 Python
pytorch实现加载保存查看checkpoint文件
Jul 15 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中遇到BOM、编码导致json_decode函数无法解析问题
2014/07/02 PHP
PHP关于htmlspecialchars、strip_tags、addslashes的解释
2014/07/04 PHP
php远程下载类分享
2016/04/13 PHP
100行PHP代码实现socks5代理服务器
2016/04/28 PHP
Laravel4中的Validator验证扩展用法详解
2016/07/26 PHP
老生常谈php中传统验证与thinkphp框架(必看篇)
2017/06/10 PHP
php实现构建排除当前元素的乘积数组方法
2018/10/06 PHP
Javascript优化技巧(文件瘦身篇)
2008/01/28 Javascript
js中关于String对象的replace使用详解
2011/05/24 Javascript
读jQuery之十四 (触发事件核心方法)
2011/08/23 Javascript
javascript trim函数在IE下不能用的解决方法
2014/09/12 Javascript
微信小程序 Page()函数详解
2016/10/17 Javascript
深入理解Angularjs中的$resource服务
2016/12/31 Javascript
BootStrap的select2既可以查询又可以输入的实现代码
2017/02/17 Javascript
详解Vue组件之间的数据通信实例
2017/06/17 Javascript
Vuex 使用及简单实例(计数器)
2018/08/29 Javascript
浅谈JS的原型和继承
2019/05/08 Javascript
微信小程序wx.request拦截器使用详解
2019/07/09 Javascript
vue+高德地图实现地图搜索及点击定位操作
2020/09/09 Javascript
python 中文字符串的处理实现代码
2009/10/25 Python
关于python的list相关知识(推荐)
2017/08/30 Python
python实现机械分词之逆向最大匹配算法代码示例
2017/12/13 Python
下载python中Crypto库报错:ModuleNotFoundError: No module named ‘Crypto’的解决
2018/04/23 Python
Python如何使用k-means方法将列表中相似的句子归类
2019/08/08 Python
Python实现直播推流效果
2019/11/26 Python
在Python中预先初始化列表内容和长度的实现
2019/11/28 Python
英国内衣连锁店:Boux Avenue
2018/01/24 全球购物
会计岗位职责范本
2014/03/07 职场文书
高考寄语大全
2014/04/08 职场文书
技术岗位竞聘演讲稿
2014/05/16 职场文书
2014年国庆节活动总结
2014/08/26 职场文书
2014年材料员工作总结
2014/11/19 职场文书
《观潮》教学反思
2016/02/17 职场文书
教你怎么用python实现字符串转日期
2021/05/24 Python
Python图片检索之以图搜图
2021/05/31 Python
详解在OpenCV中如何使用图像像素
2022/03/03 Python