经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » 树莓派 » 查看文章
基于树莓派的语音对话机器人
来源:jb51  时间:2019/6/17 10:56:25  对本文有异议

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

第一部分代码

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

第二部分代码

  1. # coding: utf-8
  2. import sys
  3. import json
  4. import urllib2
  5. import base64
  6. import requests
  7. reload(sys)
  8. sys.setdefaultencoding(“utf-8”)
  9. def get_access_token():
  10. url = https://openapi.baidu.com/oauth/2.0/token”
  11. body = {
  12. grant_type”:”client_credentials”,
  13. client_id :”此处填写自己的client_id”,
  14. client_secret”:”此处填写自己的client_secret”,
  15. }
  16. r = requests.post(url,data=body,verify=True)
  17. respond = json.loads(r.text)
  18. return respond["access_token"]
  19. def yuyinshibie_api(audio_data,token):
  20. speech_data = base64.b64encode(audio_data).decode(“utf-8”)
  21. speech_length = len(audio_data)
  22. post_data = {
  23. format : wav”,
  24. rate : 16000,
  25. channel : 1,
  26. cuid : B8-27-EB-BA-24-14”,
  27. token : token,
  28. speech : speech_data,
  29. len : speech_length
  30. }
  31. url = "http://vop.baidu.com/server_api"
  32. json_data = json.dumps(post_data).encode("utf-8")
  33. json_length = len(json_data)
  34. #print(json_data)
  35. req = urllib2.Request(url, data=json_data)
  36. req.add_header("Content-Type", "application/json")
  37. req.add_header("Content-Length", json_length)
  38. #print("asr start request\n")
  39. resp = urllib2.urlopen(req)
  40. #print("asr finish request\n")
  41. resp = resp.read()
  42. resp_data = json.loads(resp.decode("utf-8"))
  43. if resp_data["err_no"] == 0:
  44. return resp_data["result"]
  45. else:
  46. print(resp_data)
  47. return None
  48. def asr_main(filename,tok):
  49. try:
  50. f = open(filename, rb”)
  51. audio_data = f.read()
  52. f.close()
  53. resp = yuyinshibie_api(audio_data,tok)
  54. return resp[0]
  55. except Exception,e:
  56. print e:”,e
  57. return “识别失败”.encode(“utf-8”)

第三部分代码

  1. # coding: utf-8
  2. import requests
  3. import json
  4. import sys
  5. reload(sys)
  6. sys.setdefaultencoding("utf-8")
  7. def Tuling(words):
  8. Tuling_API_KEY = "此处填写自己的Turling KEY"
  9. body = {"key":Tuling_API_KEY,"info":words.encode("utf-8")}
  10. url = "http://www.tuling123.com/openapi/api"
  11. r = requests.post(url,data=body,verify=True)
  12. if r:
  13. date = json.loads(r.text)
  14. print date["text"]
  15. return date["text"]
  16. else:
  17. return None

第四部分代码

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

第五部分代码

  1. # coding: utf-8
  2. import os
  3. import time
  4. import yuyinhecheng
  5. import Turling
  6. import yuyinshibie
  7. tok = yuyinshibie.get_access_token()
  8. switch = True
  9. while switch:
  10. os.system('sudo arecord -D "plughw:1" -f S16_LE -r 16000 -d 3 /home/pi/Desktop/voice.wav')
  11. time.sleep(0.5)
  12. info = yuyinshibie.asr_main("/home/pi/Desktop/voice.wav",tok)
  13. if '关闭'.encode("utf-8") in info:
  14. while True:
  15. os.system('sudo arecord -D "plughw:1" -f S16_LE -r 16000 -d 10 /home/pi/Desktop/voice.wav')
  16. time.sleep(10)
  17. info = yuyinshibie.asr_main("/home/pi/Desktop/voice.wav",tok)
  18. if '开启'.encode("utf-8") in info:
  19. break
  20. url = "http://tsn.baidu.com/text2audio?tex=开启成功&lan=zh&cuid=B8-27-EB-BA-24-14&ctp=1&tok="+tok+"&per=3"
  21. os.system('mpg123 "%s"'%url)
  22. elif '暂停'.encode("utf-8") in info:
  23. url = "http://tsn.baidu.com/text2audio?tex=开始暂停&lan=zh&cuid=B8-27-EB-BA-24-14&ctp=1&tok="+tok+"&per=3"
  24. os.system('mpg123 "%s"'%url)
  25. time.sleep(10)
  26. url = "http://tsn.baidu.com/text2audio?tex=暂停结束&lan=zh&cuid=B8-27-EB-BA-24-14&ctp=1&tok="+tok+"&per=3"
  27. os.system('mpg123 "%s"'%url)
  28. continue
  29. else:
  30. tex = Turling.Tuling(info)
  31. url = yuyinhecheng.yuyinhecheng_api(tok,tex)
  32. os.system('mpg123 "%s"'%url)
  33. time.sleep(0.5)

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

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号