python實現人工智能語音助手

一.環境搭建:

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

2.使用Anaconda裏的包模塊:json

pycharm右下邊

 

 導入anaconda解釋器

 

 切換python解釋器

 

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

1.建立應用api

 

 

 記住AppID、API Key、Secret Keyapp

2.爲應用獲取語音識別調用次數ide

 

 

 

 

 

 3.建立圖靈機器人post

激活要錢

 

 記住apikeyurl

三.代碼復現

 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

四.過程

收集音頻——轉文本——調用圖靈機器人(返回文本回復)——文本轉音頻(調用系統自帶的功能)spa

相關文章
相關標籤/搜索