基于nao机器人实现语音对话(智能版本)
程序员文章站
2022-07-14 22:03:08
...
nao机器人实现语音对话
1、语音获取
nao耳麦有一个功能,它可以通过声音大小判断能力值,也就是声音越大能量越大。所以我们此次项目主要运用的就是nao 的这个功能,来展开实现的。下面是流程图。
- 功能流程图
我们可以看见上面的流程图,从录音开始到录音结束逻辑还是比较复杂的,而且还有一些我没画出来,这只是大概。 - 录音的代码
def recorder(self):
self.audio_recorder.stopMicrophonesRecording()
time.sleep(0.2)
energy = self.energy()
print(energy['left'])
self.audio_recorder.startMicrophonesRecording(record_path, "wav", 16000, (0,0,1,0))
print("record begin")
wait = 0
global flag
flag = 0
global flag_one
flag_one = 0
while 1:
energy = self.energy()
time.sleep(0.1)
if energy['left'] < 400 and wait <5:
print("no body:",energy['left'],float(wait))
wait +=0.1
elif energy['left'] >600:
print("have people:",energy['left'],wait)
wait = 4.7
continue
elif wait >= 5:
print("record over ")
self.audio_recorder.stopMicrophonesRecording()
break
else:
continue
if round(wait,1) == 4.0:
print("int wait:",int(wait))
self.answer_nao.say("你还有什么要说的吗,没有我要休眠咯")
self.audio_recorder.stopMicrophonesRecording()
time.sleep(1)
self.audio_recorder.startMicrophonesRecording(record_path,"wav",16000,(0,0,1,0))
wait = 3
flag = 1
while 1:
energy = self.energy()
time.sleep(0.1)
if energy['left'] < 400 and wait <5:
print("no body:",energy['left'],float(wait))
wait +=0.1
elif energy['left'] >600:
print("have people:",energy['left'],wait)
wait = 4.7
continue
elif round(wait,1) == 4.8:
self.answer_nao.say("慢走,期待下次与您相遇")
self.audio_recorder.stopMicrophonesRecording()
return
elif wait >= 5:
print("record over ")
self.audio_recorder.stopMicrophonesRecording()
break
else:
continue
msg = listen()
msg = str(msg)
print(msg)
time.sleep(1)
if "没" in msg:
self.answer_nao.say("拜拜")
time.sleep(2)
break
elif msg == "None":
self.answer_nao.say("很高心跟您对话,期待再次与您相见")
time.sleep(2)
break
elif msg == "":
self.answer_nao.say("期待再次和您相遇,再见")
time.sleep(2)
break
else:
flag_one = 2
botMsg = turing.botInteraction(msg)
test = str(botMsg)
answerNao.say(test)
time.sleep(0.5)
audio.recorder()
if flag == 1:
break
nao一共有四个声道,下面的代码是获取声音能量值
- 获取能量值
def energy(self):
energy = dict()
energy['left'] = self.audio_device.getLeftMicEnergy()
energy['right'] = self.audio_device.getRightMicEnergy()
energy['front'] = self.audio_device.getFrontMicEnergy()
energy['rear'] = self.audio_device.getRearMicEnergy()
return energy
上面代码就是通过声音能量值来判断是否有人说话,从而判断是否录音的。里面逻辑需要大家自己看看,我说不太清楚。。。
2、录音翻译成文本
得到录音需要用百度语音合成API将音频转换成文字。实现工程如下。
大家先去申请语音识别的API有了API KEY 和API 密码之后才行。
- 代码
from aip import AipSpeech
APP_ID = '21xxxxx'
API_KEY = 'O0gzDUHKkciBa60Vxxxxx'
SECRET_KEY = 'Psji0dC90D1OehYh63ZaQuc7xxxxxxx'
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
def listen():
with open(record_path, 'rb') as fp:
voices = fp.read()
try:
result = client.asr(voices, 'wav', 16000, {'dev_pid': 1537, })
result_text = result["result"][0]
result_text = result_text.replace(',','')
result_text = result_text.replace('.','')
return result_text
except KeyError:
print("faild")
3、将文本传给华为云
这里主要是将问题传给华为云知识库,看是否能找到匹配的问题,比如我们做的是农业相关的,我就在华为云知识库中添加农业相关的知识。
- 华为云机器人
- 代码
后续添加,代码在机器人里
点击进来我们就可以看见知识库了。可以自己添加。也可以添加技能,这个就要自己对华为云机器人熟悉了,我就不够多的阐述。
4、传给图灵机器人
如果华为云知识库没有的问题就可以传给图灵机器人人了,因为图灵机器人闲聊比华为云机器人好一些。
创建之后也可以添加问题,不过没有华为云的好。
- 代码
def __init__(self):
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))
try:
w_data = urlopen(self.request)
except URLError:
raise Exception("No internet connection available to transfer txt data")
except:
raise KeyError("Server wouldn't respond (invalid key or quota has been maxed out)")
response_text = w_data.read().decode('utf-8')
json_result = json.loads(response_text)
return json_result['text']
然后就可以语音把传回来的就可以拿回来语音播报了。
5、源码
源码回头跟华为代码一起发
上一篇: FRP中NAO机器人内网穿透记录