voice_to_text_node now posts to voice_to_text, and data json includes whether message is partial or full text

master
Jake Wilkinson 2025-11-09 22:32:33 +08:00
parent 0550422a9d
commit b8331565bd
1 changed files with 17 additions and 12 deletions

View File

@ -29,27 +29,31 @@ latest_result = {"text": ""}
class VoicePublisher(ThreadedNode):
def __init__(self):
super().__init__(package_name)
self.string_pub = self.create_publisher(String, package_name + '/test_topic', 10)
self.string_pub = self.create_publisher(String, package_name + '/voice_to_text', 10)
self.prev_partial = ""
self.prev_text = ""
self.prev_partial = None
self.prev_text = None
self.speech_thread = threading.Thread(target=self.speech_loop, daemon=True)
self.speech_thread.start()
def publish_if_changed(self, key, new_value):
if key == "partial" and new_value != self.prev_partial:
self.prev_partial = new_value
self._publish(new_value)
elif key == "text" and new_value != self.prev_text:
self.prev_text = new_value
self._publish(new_value)
if key == "partial":
if new_value and new_value != self.prev_partial:
self.prev_partial = new_value
self._publish("partial", new_value)
elif key == "text":
if new_value != self.prev_text:
self.prev_text = new_value
self._publish("text", new_value)
def _publish(self, data):
def _publish(self, kind, value):
msg = String()
msg.data = data
msg.data = json.dumps({"type": kind, "data": value})
self.string_pub.publish(msg)
self.get_logger().info(f'Published: {data}')
self.get_logger().info(f'Published: {msg.data}')
def speech_loop(self):
model = Model(model_path)
@ -82,6 +86,7 @@ class VoicePublisher(ThreadedNode):
def main():
rclpy.init()
node = VoicePublisher()