语音机器人
1 from aip import AipSpeech 2 import requests 3 import json 4 import speech_recognition as sr 5 import win32com.client 6 from selenium import webdriver 7 import time 8 9 10 11 speaker = win32com.client.Dispatch("SAPI.SpVoice") 12 13 def watch_movie(a): 14 driver.get('https://www.iqiyi.com') 15 time.sleep(2) 16 17 movie = driver.find_element_by_id('J-header-search-input') 18 movie.send_keys(a) 19 20 21 def my_record(rate=16000): 22 r = sr.Recognizer() 23 with sr.Microphone(sample_rate=rate) as source: 24 print("please say something") 25 audio = r.listen(source) 26 27 with open("myvoices.wav", "wb") as f: 28 f.write(audio.get_wav_data()) 29 30 31 APP_ID = '20315758' 32 API_KEY = 'pPMQbHo7ccNrGo7XvLjyupEi' 33 SECRET_KEY = 'MfFOtlK8m9SFUIlrUpCRl7bZl65datG3' 34 client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) 35 path = 'myvoices.wav' 36 37 38 def listen(): 39 with open(path, 'rb') as fp: 40 voices = fp.read() 41 try: 42 result = client.asr(voices, 'wav', 16000, {'dev_pid': 1537, }) 43 result_text = result["result"][0] 44 print("我 : " + result_text) 45 return result_text 46 except KeyError: 47 print("KeyError") 48 speaker.Speak("我没有听清楚,请再说一遍...") 49 50 51 turing_api_key = "f502483535bb4f9eac11b822d9307b06" 52 api_url = "http://openapi.tuling123.com/openapi/api/v2" 53 headers = {'Content-Type': 'application/json;charset=UTF-8'} 54 55 56 def Turing(text_words=""): 57 req = { 58 "reqType": 0, 59 "perception": { 60 "inputText": { 61 "text": text_words 62 }, 63 64 "selfInfo": { 65 "location": { 66 "city": "潮州", 67 "province": "广东", 68 "street": "潮安" 69 } 70 } 71 }, 72 "userInfo": { 73 "apiKey": turing_api_key, # 你的图灵机器人apiKey 74 "userId": "Nieson" # 用户唯一标识(随便填, 非密钥) 75 } 76 } 77 78 req["perception"]["inputText"]["text"] = text_words 79 response = requests.request("post", api_url, json=req, headers=headers) 80 response_dict = json.loads(response.text) 81 82 result = response_dict["results"][0]["values"]["text"] 83 print("大白: " + result) 84 print('........') 85 return result 86 87 88 print('.........') 89 while True: 90 my_record() 91 request = listen() 92 response = Turing(request) 93 speaker.Speak(response) 94 if request=='再见。': 95 break 96 if request[:5]=='打开爱奇艺': 97 movie = request[6:-1] 98 driver = webdriver.Chrome() 99 driver.get('https://so.iqiyi.com/so/q_'+movie+'?source=default&sr=1348372637207') 100 time.sleep(3) 101 102 103 104 105 106 print(' \n欢迎下次使用!') 107
1.基本原理:
(1)语音生成音频文件,录音并以当前时间保存到voices文件中
(2)音频文件转文字:采用百度语音识别
(3)与机器人对话:调用图灵机器人,回复对话
(4)用requests库,爬取机器人的回复,输出机器人回答(回复‘再见’时,结束程序)
2.用到的库 :api (图灵机器人,百度语音),requests ,json (爬虫),
speech_recognition(语音识别)