如何用Python脚本向Ubuntu上的虚拟NAOqi机器人发送文本语音输入?
How to Send Text Input to a Virtual NAO Robot via Python (Like Choregraphe's Dialogue Window)
To replicate Choregraphe's dialogue window functionality—sending text that the robot treats as spoken input—you’ll use the official qi framework (Python SDK for NAOqi) and interact with the ALDialog service, which handles the robot’s dialogue processing under the hood.
Step 1: Install the Qi Python SDK
First, install the SDK matching your NAOqi version. For most recent releases, use pip:
pip install qi
Note: If you’re working with an older NAOqi version (pre-2.5), you may need the legacy naoqi package instead.
Step 2: Complete Python Script Example
This script connects to your virtual robot, sets up a basic dialogue topic, and sends text input just like Choregraphe:
import qi import sys def main(): # Connect to your running naoqi-bin instance session = qi.Session() try: session.connect("tcp://127.0.0.1:9001") print("Successfully connected to the virtual robot!") except RuntimeError: print(f"Failed to connect to 127.0.0.1:9001. Is naoqi-bin running with the correct port?") sys.exit(1) # Access the ALDialog service al_dialog = session.service("ALDialog") al_dialog.setLanguage("English") # Define a simple dialogue topic (replace with your Choregraphe topics if needed) test_topic = """ topic: ~BasicChat() language: enu u:(hello hi) Hey there! Nice to chat with you. u:(how are you doing) I'm in perfect shape, thanks for asking! u:(goodbye see you) Catch you later! Have a great day. """ # Load and activate the topic topic_id = al_dialog.loadTopicContent(test_topic) al_dialog.activateTopic(topic_id) al_dialog.subscribe("PythonTextInput") # Send text input (this mimics typing in Choregraphe's dialogue window) user_input = "How are you doing" print(f"Sending text: {user_input}") al_dialog.inputText(user_input) # Wait for the robot to process and respond qi.sleep(2) # Clean up resources al_dialog.unsubscribe("PythonTextInput") al_dialog.deactivateTopic(topic_id) al_dialog.unloadTopic(topic_id) if __name__ == "__main__": main()
Key Notes
ALDialog.inputText(): This is the critical method—it sends text directly to the robot’s dialogue system, just as if it was recognized from speech. The robot will generate a spoken response (viaALTextToSpeech) or trigger any linked behaviors from your dialogue topic.- Use Existing Choregraphe Topics: Instead of defining a new topic, you can export topics from Choregraphe as
.topfiles, then load them withal_dialog.loadTopic("/path/to/your/topic.top"). - Language Matching: Ensure the language set with
setLanguage()matches your topic’s language (e.g., "French", "Spanish") to avoid processing errors.
Troubleshooting Tips
- Double-check that your
naoqi-bincommand uses the same IP (-b 127.0.0.1) and port (-p 9001) as the script. - If you get a "service not found" error for
ALDialog, confirm the dialogue service is enabled (it’s on by default for virtual robots). - For legacy NAOqi versions, replace
qi.Session()withnaoqi.ALProxyconnections—theinputTextmethod works identically.
内容的提问来源于stack exchange,提问作者Muhammad Rahman
相关产品推荐
相关产品推荐

