python实现人工智能语音助手

时间:2024-02-22 08:14:38

一.环境搭建:

1.安装pycharm和Anaconda(安装过程几乎点下一步即可,实在怕出问题去问度娘)

2.使用Anaconda里的包模块:

pycharm右下边

 

 导入anaconda解释器

 

 切换python解释器

 

 二.百度语音(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

四.过程

收集音频——转文本——调用图灵机器人(返回文本回复)——文本转音频(调用系统自带的功能)