You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
4.2 KiB

2 years ago
  1. # Example 8
  2. This example will showcase how to save the interpreted speech from Stretch's [ReSpeaker Mic Array v2.0](https://wiki.seeedstudio.com/ReSpeaker_Mic_Array_v2.0/) to a text file.
  3. <p align="center">
  4. <img src="https://raw.githubusercontent.com/hello-robot/stretch_tutorials/main/images/respeaker.jpg"/>
  5. </p>
  6. Begin by running the `respeaker.launch` file in a terminal.
  7. ```bash
  8. # Terminal 1
  9. roslaunch respeaker_ros sample_respeaker.launch
  10. ```
  11. Then run the [speech_text.py](https://github.com/hello-robot/stretch_tutorials/blob/main/src/speech_text.py) node.
  12. ```bash
  13. # Terminal 2
  14. cd catkin_ws/src/stretch_tutorials/src/
  15. python speech_text.py
  16. ```
  17. The ReSpeaker will be listening and will start to interpret speech and save the transcript to a text file. To stop shutdown the node, type **Ctrl** + **c** in the terminal.
  18. ### The Code
  19. ```python
  20. #!/usr/bin/env python
  21. import rospy
  22. import os
  23. from speech_recognition_msgs.msg import SpeechRecognitionCandidates
  24. class SpeechText:
  25. """
  26. A class that saves the interpreted speech from the ReSpeaker Microphone Array to a text file.
  27. """
  28. def __init__(self):
  29. """
  30. Initialize subscriber and directory to save speech to text file.
  31. """
  32. self.sub = rospy.Subscriber("speech_to_text", SpeechRecognitionCandidates, self.callback)
  33. self.save_path = '/home/hello-robot/catkin_ws/src/stretch_tutorials/stored_data'
  34. rospy.loginfo("Listening to speech.")
  35. def callback(self,msg):
  36. """
  37. A callback function that receives the speech transcript and appends the
  38. transcript to a text file.
  39. :param self: The self reference.
  40. :param msg: The SpeechRecognitionCandidates message type.
  41. """
  42. transcript = ' '.join(map(str,msg.transcript))
  43. file_name = 'speech.txt'
  44. completeName = os.path.join(self.save_path, file_name)
  45. with open(completeName, "a+") as file_object:
  46. file_object.write("\n")
  47. file_object.write(transcript)
  48. if __name__ == '__main__':
  49. rospy.init_node('speech_text')
  50. SpeechText()
  51. rospy.spin()
  52. ```
  53. ### The Code Explained
  54. Now let's break the code down.
  55. ```python
  56. #!/usr/bin/env python
  57. ```
  58. Every Python ROS [Node](http://wiki.ros.org/Nodes) will have this declaration at the top. The first line makes sure your script is executed as a Python script.
  59. ```python
  60. import rospy
  61. import os
  62. ```
  63. You need to import `rospy` if you are writing a ROS [Node](http://wiki.ros.org/Nodes).
  64. ```python
  65. from speech_recognition_msgs.msg import SpeechRecognitionCandidates
  66. ```
  67. Import `SpeechRecognitionCandidates` from the `speech_recgonition_msgs.msg` so that we can receive the interpreted speech.
  68. ```python
  69. def __init__(self):
  70. """
  71. Initialize subscriber and directory to save speech to text file.
  72. """
  73. self.sub = rospy.Subscriber("speech_to_text", SpeechRecognitionCandidates, self.callback)
  74. ```
  75. Set up a subscriber. We're going to subscribe to the topic "*speech_to_text*", looking for `SpeechRecognitionCandidates` messages. When a message comes in, ROS is going to pass it to the function "callback" automatically.
  76. ```python
  77. self.save_path = '/home/hello-robot/catkin_ws/src/stretch_tutorials/stored_data
  78. ```
  79. Define the directory to save the text file.
  80. ```python
  81. transcript = ' '.join(map(str,msg.transcript))
  82. ```
  83. Take all items in the iterable list and join them into a single string named transcript.
  84. ```python
  85. file_name = 'speech.txt'
  86. completeName = os.path.join(self.save_path, file_name)
  87. ```
  88. Define the file name and create a complete path directory.
  89. ```python
  90. with open(completeName, "a+") as file_object:
  91. file_object.write("\n")
  92. file_object.write(transcript)
  93. ```
  94. Append the transcript to the text file.
  95. ```python
  96. rospy.init_node('speech_text')
  97. SpeechText()
  98. ```
  99. The next line, `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 "/".
  100. Instantiate the `SpeechText()` class.
  101. ```python
  102. rospy.spin()
  103. ```
  104. Give control to ROS. This will allow the callback to be called whenever new
  105. messages come in. If we don't put this line in, then the node will not work,
  106. and ROS will not process any messages.