Example 8
This example will showcase how to save the interpreted speech from Stretch's ReSpeaker Mic Array v2.0 to a text file.
Begin by running the respeaker.launch
file in a terminal.
# Terminal 1
roslaunch respeaker_ros sample_respeaker.launch
# Terminal 2
cd catkin_ws/src/stretch_tutorials/src/
python speech_text.py
The Code
#!/usr/bin/env python
import rospy
import os
from speech_recognition_msgs.msg import SpeechRecognitionCandidates
class SpeechText:
"""
A class that saves the interpreted speech from the ReSpeaker Microphone Array to a text file.
"""
def __init__(self):
"""
Initialize subscriber and directory to save speech to text file.
"""
self.sub = rospy.Subscriber("speech_to_text", SpeechRecognitionCandidates, self.callback)
self.save_path = '/home/hello-robot/catkin_ws/src/stretch_tutorials/stored_data'
rospy.loginfo("Listening to speech.")
def callback(self,msg):
"""
A callback function that receives the speech transcript and appends the
transcript to a text file.
:param self: The self reference.
:param msg: The SpeechRecognitionCandidates message type.
"""
transcript = ' '.join(map(str,msg.transcript))
file_name = 'speech.txt'
completeName = os.path.join(self.save_path, file_name)
with open(completeName, "a+") as file_object:
file_object.write("\n")
file_object.write(transcript)
if __name__ == '__main__':
rospy.init_node('speech_text')
SpeechText()
rospy.spin()
The Code Explained
Now let's break the code down.
#!/usr/bin/env python
import rospy
import os
rospy
if you are writing a ROS Node.
from speech_recognition_msgs.msg import SpeechRecognitionCandidates
SpeechRecognitionCandidates
from the speech_recgonition_msgs.msg
so that we can receive the interpreted speech.
def __init__(self):
"""
Initialize subscriber and directory to save speech to text file.
"""
self.sub = rospy.Subscriber("speech_to_text", SpeechRecognitionCandidates, self.callback)
SpeechRecognitionCandidates
messages. When a message comes in, ROS is going to pass it to the function "callback" automatically.
self.save_path = '/home/hello-robot/catkin_ws/src/stretch_tutorials/stored_data
transcript = ' '.join(map(str,msg.transcript))
file_name = 'speech.txt'
completeName = os.path.join(self.save_path, file_name)
with open(completeName, "a+") as file_object:
file_object.write("\n")
file_object.write(transcript)
rospy.init_node('speech_text')
SpeechText()
rospy.init_node(NAME, ...)
, is very important as it tells rospy the name of your node -- until rospy has this information, it cannot start communicating with the ROS Master. NOTE: the name must be a base name, i.e. it cannot contain any slashes "/".
Instantiate the SpeechText()
class.
rospy.spin()