1. 程式人生 > 實用技巧 >python實現人工智慧語音助手

python實現人工智慧語音助手

一.環境搭建:

1.安裝pycharm和Anaconda(安裝過程幾乎點下一步即可,實在怕出問題去問度娘)

2.使用Anaconda裡的包模組:

二.百度語音(STT)和圖靈機器人

1.建立應用

記住AppID、API Key、Secret Key

2.為應用獲取語音識別呼叫次數

3.建立圖靈機器人

記住apikey

三.程式碼復現

 1 import speech_recognition as sr
 2 import win32com.client
 3 from aip import AipSpeech
 4 import requests
 5 import
json 6 7 speaker = win32com.client.Dispatch("SAPI.SpVoice") 8 9 #使用語音識別包錄製音訊 10 def my_record(rate=16000): 11 r = sr.Recognizer() 12 with sr.Microphone(sample_rate=rate) as source: 13 print("please say something") 14 audio = r.listen(source) 15 16 with open("recording.wav
","wb") as f: 17 f.write(audio.get_wav_data()) 18 print("錄音完成") 19 20 #音訊檔案轉文字:採用百度的語音識別python-SDK 21 22 APP_ID = 'your_ID' 23 API_KEY = 'your_KEY' 24 SECRET_KEY = 'your_SECERT_KEY' 25 client = AipSpeech(APP_ID,API_KEY,SECRET_KEY) 26 path = 'recording.wav' 27 28 #將語音轉文字STT 29 def listen():
30 #讀取錄音檔案 31 with open(path,'rb') as fp: 32 voices = fp.read() 33 try: 34 result = client.asr(voices,'wav',16000,{'dev_pid':1537,}) 35 result_text = result["result"][0] 36 print("you said:"+result_text) 37 return result_text 38 except KeyError: 39 print("KeyError") 40 speaker.Speak("我沒有聽清楚,請再說一遍...") 41 42 #呼叫圖靈機器人 43 turing_api_key = "your_key" 44 api_url = "http://openapi.tuling123.com/openapi/api/v2" 45 headers = {'Content-Type':'application/json;charset=UTF-8'} 46 47 # 圖靈機器人回覆 48 def Turing(text_words=""): 49 req = { 50 "reqType": 0, 51 "perception": { 52 "inputText": { 53 "text": text_words 54 }, 55 56 "selfInfo": { 57 "location": { 58 "city": "長沙", 59 "province": "湖南", 60 "street": "中意三路" 61 } 62 } 63 }, 64 "userInfo": { 65 "apiKey": turing_api_key, # 你的圖靈機器人apiKey 66 "userId": "687948" # 使用者唯一標識(隨便填, 非金鑰) 67 } 68 } 69 70 req["perception"]["inputText"]["text"] = text_words 71 response = requests.request("post", api_url, json=req, headers=headers) 72 response_dict = json.loads(response.text) 73 74 result = response_dict["results"][0]["values"]["text"] 75 print("AI Robot said: " + result) 76 return result 77 78 # 語音合成,輸出機器人的回答 79 while True: 80 my_record() 81 request = listen() 82 response = Turing(request) 83 speaker.Speak(response)
View Code

四.過程

收集音訊——轉文字——呼叫圖靈機器人(返回文本回復)——文字轉音訊(呼叫系統自帶的功能)