基于nao机器人实现人机语音交互

基于nao机器人实现人机语音交互

其实本文的人机语音交互跟之前写的文章大致是差不多的。只不过这个功能移植到nao上面去了。可以增加知识库等等功能,识别率也算是不错的。
1、调用图灵机器人实现对话

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']

2、录音

def main(robot_IP, robot_PORT=9559): //录音
  global tts, audio, record, aup 
  tts = ALProxy("ALTextToSpeech", robot_IP, robot_PORT)
  audio = ALProxy("ALAudioDevice", robot_IP, robot_PORT)
  record = ALProxy("ALAudioRecorder", robot_IP, robot_PORT)
  aup = ALProxy("ALAudioPlayer", robot_IP, robot_PORT)
  print 'start recording...'
  record.startMicrophonesRecording(record_path, 'wav', 16000, (0,0,1,0))
  time.sleep(5)
  record.stopMicrophonesRecording()
  print 'record over'
  fileID = aup.playFile(record_path, 0.7, 0)  

3、调用百度API实现语音转文字

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('。','')
    print("you said: " + result_text)
    return result_text
  except KeyError:
    print("faild")

4、完整代码

import argparse
from naoqi import ALProxy
import time
import wave
tts = audio = record = aup = None 
record_path = '/home/nao/record.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 main(robot_IP, robot_PORT=9559): //录音
    global tts, audio, record, aup 
    tts = ALProxy("ALTextToSpeech", robot_IP, robot_PORT)
    audio = ALProxy("ALAudioDevice", robot_IP, robot_PORT)
    record = ALProxy("ALAudioRecorder", robot_IP, robot_PORT)
    aup = ALProxy("ALAudioPlayer", robot_IP, robot_PORT)
    print 'start recording...'
    record.startMicrophonesRecording(record_path, 'wav', 16000, (0,0,1,0))
    time.sleep(5)
    record.stopMicrophonesRecording()
    print 'record over'
    fileID = aup.playFile(record_path, 0.7, 0)  
    

    
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('。','')
    print("you said: " + result_text)
    return result_text
  except KeyError:
    print("faild")
    
if __name__ == "__main__":  //主函数
    parser = argparse.ArgumentParser()
    parser.add_argument("--ip", type=str, default="192.168.1.89", help="Robot ip address")
    parser.add_argument("--port", type=int, default=9559, help="Robot port number")
    args = parser.parse_args()
    turing = TuringChatMode()
    tts = ALProxy("ALTextToSpeech", robot_IP, robot_PORT)
    while True:
        main(args.ip, args.port)
        msg = listen()
        botMsg = turing.botInteraction(msg)
        bot = str (botMsg)
        tts.say(bot)

猜你喜欢

转载自blog.csdn.net/qq_45125250/article/details/108658202