0

我正在使用 Windows-XP 机器和 Python 2.7 上的 NAO 机器人。

我想检测语音中的标记。整个事情都奏效了,但不幸的是,我现在不得不面对 10 秒的延迟,并且没有检测到我的事件(未调用回调函数)。

首先,我的主要功能:

from naoqi import ALProxy, ALBroker
from speechEventModule import SpeechEventModule
myString = "Put that \\mrk=1\\ there."
NAO_IP = "192.168.0.105" 
NAO_PORT = 9559
memory = ALProxy("ALMemory", NAO_IP, NAO_PORT)
tts = ALProxy("ALTextToSpeech", NAO_IP, NAO_PORT)
tts.enableNotifications()

myBroker = ALBroker("myBroker",
   "0.0.0.0",   # listen to anyone
   0,           # find a free port and use it
   NAO_IP,         # parent broker IP
   NAO_PORT)       # parent broker port

global SpeechEventListener
SpeechEventListener = SpeechEventModule("SpeechEventListener", memory)
memory.subscribeToEvent("ALTextToSpeech/CurrentBookMark", "SpeechEventListener", "onBookmarkDetected")
tts.say(initialString)

这里是我的演讲事件模块:

from naoqi import ALModule
from naoqi import ALProxy

NAO_IP = "192.168.0.105" 
NAO_PORT = 9559

SpeechEventListener = None
leds = None
memory = None

class SpeechEventModule(ALModule):
    def __init__(self, name, ext_memory):
        ALModule.__init__(self, name)
        global memory
        memory = ext_memory
        global leds 
        leds = ALProxy("ALLeds",NAO_IP, NAO_PORT)        

    def onBookmarkDetected(self, key, value, message):
        print "Event detected!"
        print "Key: ", key
        print "Value: " , value
        print "Message: " , message

        if(value == 1):
            global leds
            leds.fadeRGB("FaceLeds", 0x00FF0000, 0.2)
        if(value == 2):
            global leds
            leds.fadeRGB("FaceLeds", 0x000000FF, 0.2)

请问有人有同样的问题吗?有人可以给我一个建议吗?

提前致谢!

4

3 回答 3

1

以下是使用更新的 Naoqi 版本的方法:

import qi
import argparse


class SpeechEventListener(object):
    """ A class to react to the ALTextToSpeech/CurrentBookMark event """

    def __init__(self, session):
        super(SpeechEventListener, self).__init__()
        self.memory = session.service("ALMemory")
        self.leds = session.service("ALLeds")
        self.subscriber = self.memory.subscriber("ALTextToSpeech/CurrentBookMark")
        self.subscriber.signal.connect(self.onBookmarkDetected)
        # keep this variable in memory, else the callback will be disconnected

    def onBookmarkDetected(self, value):
        """ callback for event ALTextToSpeech/CurrentBookMark """
        print "Event detected!"
        print "Value: " , value  # key and message are not useful here

        if value == 1:
            self.leds.fadeRGB("FaceLeds", 0x00FF0000, 0.2)
        if value == 2:
            self.leds.fadeRGB("FaceLeds", 0x000000FF, 0.2)


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--ip", type=str, default="127.0.0.1",
                        help="Robot IP address. On robot or Local Naoqi: use '127.0.0.1'.")
    parser.add_argument("--port", type=int, default=9559,
                        help="Naoqi port number")
    args = parser.parse_args()

    # Initialize qi framework
    connection_url = "tcp://" + args.ip + ":" + str(args.port)
    app = qi.Application(["SpeechEventListener", "--qi-url=" + connection_url])
    app.start()
    session = app.session
    speech_event_listener = SpeechEventListener(session)

    tts = session.service("ALTextToSpeech")
    # tts.enableNotifications() --> this seems outdated
    while True:
        raw_input("Say something...")
        tts.say("Put that \\mrk=1\\ there.")
于 2016-01-08T16:33:20.100 回答
0

您正在订阅模块外的事件。如果我没有错,您必须将其放入__init__方法中。

class SpeechEventModule(ALModule):

    def __init__(self, name, ext_memory):
        ALModule.__init__(self, name)
        memory = ALProxy("ALMemory")
        leds = ALProxy("ALLeds")

无论如何,请检查您的 main 函数是否一直运行(如果您发现键盘中断会更好),否则您的程序将在他捕捉到任何关键字之前结束。

try:
    while True:
        time.sleep(1)
except KeyboardInterrupt:
    print
    print "Interrupted by user, shutting down"
    myBroker.shutdown()
    sys.exit(0)

看看这个教程,它可能会有所帮助。

于 2013-08-07T13:25:02.627 回答
0

也许你应该尝试手动绑定回调,这样做:

def __init__(self, name, ext_memory):
    ALModule.__init__(self, name)
    self.BIND_PYTHON( self.getName(),"onBookmarkDetected" );

这就是我在某些 Choregraphe 框中使用回调时所做的。

于 2013-12-13T17:14:35.340 回答