Skip to content

Instantly share code, notes, and snippets.

@rsirres
Created January 13, 2015 09:22
Show Gist options
  • Save rsirres/69f8dd44ad149cb103e9 to your computer and use it in GitHub Desktop.
Save rsirres/69f8dd44ad149cb103e9 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
# Author : Sirres Raphael
# Date: 16.12.2014
# Description: Launch Server which serves a web page that performs the text recognition with the Web Speech API (Google Chrome).
# Recognized text is send to the server which on the other hand fowards the command to the NAO.
#
# How to use:  0. (Install pip: easy_install pip) Dont need this if pip is already installed  
# 0. (Install bottle: pip install bottle)
# 1. Adapt the IP address in the main function.
# 2. Run the following command: python speech.py
# 3. Next, browse with Google Chrome (version > 25) to the following internet address: http://localhost:8080
# 4. Press the Play demo button and allow Google Chrome to use the Web Speech API
# 5. Say one of the following commands: "sit down", "stand up", "stop", "follow"
import os
from bottle import route, run, template, static_file, request, post
from posture import NaoPosture
import rospy
from std_msgs.msg import String
pos,nao_controller=None,None
root_folder = os.path.dirname(os.path.realpath(__file__))
STAND_UP = "stand up"
SIT_DOWN = "sit down"
FOLLOW = "follow"
STOP = "stop"
# Topic for un/subscribe the ar_marker form the NAO Controller Node
# Sidenote: The NAO Controller is in charge of tracking the marker
NAO_CONTROLLER_TOPIC="V_R"
def stand_up():
print "Stand Up"
pos.stand()
def sit_down():
print "Sit down"
pos.sit()
# We send the string "follow"/"stop" to the NAO_Controller node which (un)subscribe from/to the ar_pose node
def follow():
print "follow"
nao_controller.publish(FOLLOW)
def stop():
print "stop"
nao_controller.publish(STOP)
mapping = {
STAND_UP : stand_up,
SIT_DOWN : sit_down,
STOP : stop,
FOLLOW : follow
}
# Relax the requirements of detecting a command.
# For instance, it is enough (in this case) to just hear "sit" or "stand" to perform the action
def normalize_string(keyword):
keyword = keyword.strip()
res = keyword
if keyword in STAND_UP: res = STAND_UP
if keyword in SIT_DOWN: res = SIT_DOWN
if FOLLOW in keyword: res = FOLLOW
if STOP in keyword: res = STOP
return res
# This route receives the string recognized by the Web speech api
# and calls the corresponding function
@post("/")
def speech():
speech = request.forms.get("text")
norm_speech = normalize_string(speech)
if norm_speech in mapping:
mapping[norm_speech]()
# Serve static assets
@route("/")
@route('<filename>')
def server_static(filename="speech.html"):
return static_file(filename, root=root_folder)
def main():
#Adapt IP address
global pos
pos=NaoPosture("10.151.0.87")
#Publisher sends the commands "stop" and "follow" in order to subscribe/unsubscribe from the ar_pose node
#This way the robot should either ignore or follow the marker.
global nao_controller
nao_controller = rospy.Publisher(NAO_CONTROLLER_TOPIC, String, queue_size=10)
rospy.init_node('Voice_Recognition_Node', anonymous=True)
run(host='localhost', port=8080, reloader=False)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment