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.

142 lines
4.3 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
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/noetic/images/respeaker.jpg"/>
  5. </p>
  6. Begin by running the `respeaker.launch` file in a terminal.
  7. ```{.bash .shell-prompt}
  8. roslaunch respeaker_ros respeaker.launch
  9. ```
  10. Then run the [speech_text.py](https://github.com/hello-robot/stretch_tutorials/blob/noetic/src/speech_text.py) node. In a new terminal, execute:
  11. ```{.bash .shell-prompt}
  12. cd catkin_ws/src/stretch_tutorials/src/
  13. python3 speech_text.py
  14. ```
  15. The ReSpeaker will be listening and will start to interpret speech and save the transcript to a text file. To shut down the node, type `Ctrl` + `c` in the terminal.
  16. ### The Code
  17. ```python
  18. #!/usr/bin/env python3
  19. import rospy
  20. import os
  21. from speech_recognition_msgs.msg import SpeechRecognitionCandidates
  22. class SpeechText:
  23. """
  24. A class that saves the interpreted speech from the ReSpeaker Microphone Array to a text file.
  25. """
  26. def __init__(self):
  27. """
  28. Initialize subscriber and directory to save speech to text file.
  29. """
  30. self.sub = rospy.Subscriber("speech_to_text", SpeechRecognitionCandidates, self.callback)
  31. self.save_path = '/home/hello-robot/catkin_ws/src/stretch_tutorials/stored_data'
  32. rospy.loginfo("Listening to speech.")
  33. def callback(self,msg):
  34. """
  35. A callback function that receives the speech transcript and appends the
  36. transcript to a text file.
  37. :param self: The self reference.
  38. :param msg: The SpeechRecognitionCandidates message type.
  39. """
  40. transcript = ' '.join(map(str,msg.transcript))
  41. file_name = 'speech.txt'
  42. completeName = os.path.join(self.save_path, file_name)
  43. with open(completeName, "a+") as file_object:
  44. file_object.write("\n")
  45. file_object.write(transcript)
  46. if __name__ == '__main__':
  47. rospy.init_node('speech_text')
  48. SpeechText()
  49. rospy.spin()
  50. ```
  51. ### The Code Explained
  52. Now let's break the code down.
  53. ```python
  54. #!/usr/bin/env python3
  55. ```
  56. 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 Python3 script.
  57. ```python
  58. import rospy
  59. import os
  60. ```
  61. You need to import `rospy` if you are writing a ROS [Node](http://wiki.ros.org/Nodes).
  62. ```python
  63. from speech_recognition_msgs.msg import SpeechRecognitionCandidates
  64. ```
  65. Import `SpeechRecognitionCandidates` from the `speech_recgonition_msgs.msg` so that we can receive the interpreted speech.
  66. ```python
  67. def __init__(self):
  68. """
  69. Initialize subscriber and directory to save speech to text file.
  70. """
  71. self.sub = rospy.Subscriber("speech_to_text", SpeechRecognitionCandidates, self.callback)
  72. ```
  73. 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.
  74. ```python
  75. self.save_path = '/home/hello-robot/catkin_ws/src/stretch_tutorials/stored_data
  76. ```
  77. Define the directory to save the text file.
  78. ```python
  79. transcript = ' '.join(map(str,msg.transcript))
  80. ```
  81. Take all items in the iterable list and join them into a single string named transcript.
  82. ```python
  83. file_name = 'speech.txt'
  84. completeName = os.path.join(self.save_path, file_name)
  85. ```
  86. Define the file name and create a complete path directory.
  87. ```python
  88. with open(completeName, "a+") as file_object:
  89. file_object.write("\n")
  90. file_object.write(transcript)
  91. ```
  92. Append the transcript to the text file.
  93. ```python
  94. rospy.init_node('speech_text')
  95. SpeechText()
  96. ```
  97. 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.
  98. !!! note
  99. 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.