百度语音识别+图灵机器人+python实现智能对话(小爱机器人)

您所在的位置:网站首页 图灵机器人认证要多久 百度语音识别+图灵机器人+python实现智能对话(小爱机器人)

百度语音识别+图灵机器人+python实现智能对话(小爱机器人)

2023-12-09 14:45| 来源: 网络整理| 查看: 265

百度语音识别+图灵机器人+python实现智能对话 1、展示效果

在这里插入图片描述

对于简单的对话需要普通话比较标准,当然可以设置成四川话,不过对于名字也可能出错,因为近音词太多了。

2、实现过程 2.1、首先获取语音

通俗点讲就是你说话生成语音文件

def save_wave_file(filepath, data): wf = wave.open(filepath, 'wb') wf.setnchannels(channels) wf.setsampwidth(sampwidth) wf.setframerate(framerate) wf.writeframes(b''.join(data)) wf.close() def my_record(): pa = PyAudio() #打开一个新的音频stream stream = pa.open(format=paInt16, channels=channels, rate=framerate, input=True, frames_per_buffer=num_samples) my_buf = [] #存放录音数据 t = time.time() print('正在录音...') while time.time() < t + 4: # 设置录音时间(秒) #循环read,每次read 2000frames string_audio_data = stream.read(num_samples) my_buf.append(string_audio_data) print('录音结束.') save_wave_file(FILEPATH, my_buf) stream.close()

必须要通过语音文件进行对话才行,所以首先转换成语音文件。

2.2、获取文件内容

就是语音文件转换成文字,这里调用了百度语音识别

def listen(): # 读取录音文件 with open(FILEPATH, 'rb') as fp: voices = fp.read() try: # 参数dev_pid:1536普通话(支持简单的英文识别)、1537普通话(纯中文识别)、1737英语、1637粤语、1837四川话、1936普通话远场 result = client.asr(voices, 'wav', 16000, {'dev_pid': 1837, }) # result = CLIENT.asr(get_file_content(path), 'wav', 16000, {'lan': 'zh', }) # print(result) # print(result['result'][0]) # print(result) result_text = result["result"][0] print("you said: " + result_text) return result_text except KeyError: print("faild") 2.3、通过接口图灵机器人对话

注册图灵机器人之后会有一个语音接口,这里是用爬虫对他对话的

class TuringChatMode(object): #初始化API请求地址 def __init__(self): # API接口地址 self.turing_url = 'http://www.tuling123.com/openapi/api?' #定义人机交互方法 def botInteraction (self,text): url_data = dict( key = 'e7ea86036040426e8a9d123176bfe12f', info = text, userid = 'yjc', ) self.request = Request(self.turing_url + urlencode(url_data))#设置并实例化Request try: w_data = urlopen(self.request)#发送请求 except URLError: raise Exception("No internet connection available to transfer txt data") #断言了请求URL异常 except: raise KeyError("Server wouldn't respond (invalid key or quota has been maxed out)") # 其他情况断言提示服务相应次数已经达到上限 response_text = w_data.read().decode('utf-8') #print(response_text) json_result = json.loads(response_text)#将json格式进行解析 return json_result['text'] 3.4、获取图灵机器人的对话之后转成语音播报 engine = pyttsx3.init() engine.say(botMsg) engine.runAndWait()

我是同的win实现的,所以这个如果用到硬件上面的话可以看看怎么写代码

3、代码 import time import wave from pyaudio import PyAudio, paInt16 import pyttsx3 import json from aip import AipSpeech from urllib.request import urlopen,Request from urllib.error import URLError from urllib.parse import urlencode APP_ID = '21715' API_KEY = 'O0gzDUHKkciBa60V1' SECRET_KEY = 'Psji0dC90D1OehYh63ZaQu' client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) framerate = 16000 # 采样率 num_samples = 2000 # 采样点 channels = 1 # 声道 sampwidth = 2 # 采样宽度2bytes FILEPATH = './myvoices.wav' class TuringChatMode(object): #初始化API请求地址 def __init__(self): # API接口地址 self.turing_url = 'http://www.tuling123.com/openapi/api?' #定义人机交互方法 def botInteraction (self,text): url_data = dict( key = 'e7ea86036040426e8a9d123176bfe12f', info = text, userid = 'yjc', ) self.request = Request(self.turing_url + urlencode(url_data))#设置并实例化Request try: w_data = urlopen(self.request)#发送请求 except URLError: raise Exception("No internet connection available to transfer txt data") #断言了请求URL异常 except: raise KeyError("Server wouldn't respond (invalid key or quota has been maxed out)") # 其他情况断言提示服务相应次数已经达到上限 response_text = w_data.read().decode('utf-8') #print(response_text) json_result = json.loads(response_text)#将json格式进行解析 return json_result['text'] def save_wave_file(filepath, data): wf = wave.open(filepath, 'wb') wf.setnchannels(channels) wf.setsampwidth(sampwidth) wf.setframerate(framerate) wf.writeframes(b''.join(data)) wf.close() #录音 def my_record(): pa = PyAudio() #打开一个新的音频stream stream = pa.open(format=paInt16, channels=channels, rate=framerate, input=True, frames_per_buffer=num_samples) my_buf = [] #存放录音数据 t = time.time() print('正在录音...') while time.time() < t + 4: # 设置录音时间(秒) #循环read,每次read 2000frames string_audio_data = stream.read(num_samples) my_buf.append(string_audio_data) print('录音结束.') save_wave_file(FILEPATH, my_buf) stream.close() def listen(): # 读取录音文件 with open(FILEPATH, 'rb') as fp: voices = fp.read() try: # 参数dev_pid:1536普通话(支持简单的英文识别)、1537普通话(纯中文识别)、1737英语、1637粤语、1837四川话、1936普通话远场 result = client.asr(voices, 'wav', 16000, {'dev_pid': 1837, }) # result = CLIENT.asr(get_file_content(path), 'wav', 16000, {'lan': 'zh', }) # print(result) # print(result['result'][0]) # print(result) result_text = result["result"][0] print("you said: " + result_text) return result_text except KeyError: print("faild") if __name__ == '__main__': turing = TuringChatMode() while True: my_record() msg = listen() if msg == "退出。": exit() else: botMsg = turing.botInteraction(msg) print("图灵BOT回复我:",botMsg) engine = pyttsx3.init() engine.say(botMsg) engine.runAndWait()


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3