日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

树莓派制作语音对话机器人

發(fā)布時間:2023/12/20 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 树莓派制作语音对话机器人 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

樹莓派制作語音對話機器人

  • 一、材料準(zhǔn)備
  • 二、教程開始
    • 1、錄音
    • 2、語音識別
    • 3、圖靈回復(fù)
    • 4、語音合成
    • 5、播放
    • 6、整合
    • 7、運行

一、材料準(zhǔn)備

1、樹莓派一個
2、免驅(qū)動USB麥克風(fēng)
3、耳機

二、教程開始

1、錄音

插上麥克風(fēng)
我用到的是使用了arecord* 測試是否麥克風(fēng)能否使用。
使用錄音輸入如下命令

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

2、語音識別

建立語音識別文件夾輸入如下程序

sudo nano yuyinshibie.py

去百度語音官網(wǎng)申請語音識別


寫代碼進(jìn)去 將上面申請的ID和secret寫進(jìn)下面紅色區(qū)域

\# coding: utf-8import sys import json import urllib2 import base64 import requestsreload(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, “l(fā)en” : 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 Nonedef 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”)

代碼圖示根據(jù)圖改縮進(jìn)


識別完成之后呢我們就要開始第三步了我們要和機器人對話那么它一定得回復(fù)我們,對吧。為了能夠智能點,我們就用到了圖靈得接口圖靈真的非常好用能夠查天氣語音講故事講笑話下面附上第三步的代碼

3、圖靈回復(fù)

(1)去圖靈機器人官網(wǎng)注冊創(chuàng)建一個微信機器人

(2)創(chuàng)建圖靈機器人文件輸入代碼

sudo nano Turling.py

(3) 寫入代碼,在紅色處寫自己申請的API KEY

\# coding: utf-8import requestsimport jsonimport sysreload(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

根據(jù)圖片改代碼縮進(jìn)

4、語音合成

圖靈回復(fù)了之后 我們要讓它播放出來 就用到了百度的語音合成
(1)創(chuàng)建語音合成文件

sudo nano yuyinhecheng.py

(2)寫代碼

\# coding: utf-8import sys import urllib2 import json import os import yuyinshibiereload(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 urldef tts_main(filename,words,tok): voice_date = yuyinhecheng_api(tok,words)f = open(filename,"wb")f.write(voice_date)f.close()

根據(jù)圖片改縮進(jìn)

5、播放

語音合成之后我們要播放出來用到了mpg123為什么我會用這個呢因為它可以直接播放網(wǎng)頁上的音頻非常的好用

** 安裝mpg123**:

sudo apt-get install mpg123

安裝好了之后 我后面等用到了再說怎么用 現(xiàn)在先不說
現(xiàn)在錄音 語音識別 語音合成 播放 所需要的工具 代碼都準(zhǔn)備好了 下面就開始整合在一起

6、整合

創(chuàng)建最終文件

sudo nano yuyin.py

寫代碼

\# coding: utf-8import osimport timeimport yuyinhechengimport Turlingimport yuyinshibietok = yuyinshibie.get_access_token()switch = Truewhile 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 '關(guān)閉'.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=暫停結(jié)束&lan=zh&cuid=B8-27-EB-BA-24-14&ctp=1&tok="+tok+"&per=3"? os.system('mpg123 "%s"'%url)? continueelse: ? tex = Turling.Tuling(info)? url = yuyinhecheng.yuyinhecheng_api(tok,tex)? os.system('mpg123 "%s"'%url)? time.sleep(0.5)

根據(jù)圖改縮進(jìn)

7、運行

最后就可以運行機器人了

輸入代碼

sudo python yuyin.py

就可以和機器人對話啦

總結(jié)

以上是生活随笔為你收集整理的树莓派制作语音对话机器人的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。