changed voice-to-text to post on change
parent
98399fc62e
commit
0550422a9d
|
|
@ -28,20 +28,28 @@ latest_result = {"text": ""}
|
|||
|
||||
class VoicePublisher(ThreadedNode):
|
||||
def __init__(self):
|
||||
super().__init__(package_name, default_rate=5.0)
|
||||
super().__init__(package_name)
|
||||
self.string_pub = self.create_publisher(String, package_name + '/test_topic', 10)
|
||||
|
||||
self.latest_partial = {"partial": ""}
|
||||
self.latest_result = {"text": ""}
|
||||
self.prev_partial = ""
|
||||
self.prev_text = ""
|
||||
|
||||
self.speech_thread = threading.Thread(target=self.speech_loop, daemon=True)
|
||||
self.speech_thread.start()
|
||||
|
||||
def on_tick(self):
|
||||
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)
|
||||
|
||||
def _publish(self, data):
|
||||
msg = String()
|
||||
msg.data = self.latest_partial.get("partial", "") or self.latest_result.get("text", "")
|
||||
msg.data = data
|
||||
self.string_pub.publish(msg)
|
||||
self.get_logger().info(f'Published: {msg.data}')
|
||||
self.get_logger().info(f'Published: {data}')
|
||||
|
||||
def speech_loop(self):
|
||||
model = Model(model_path)
|
||||
|
|
@ -59,12 +67,13 @@ class VoicePublisher(ThreadedNode):
|
|||
data = q.get()
|
||||
if rec.AcceptWaveform(data):
|
||||
result = json.loads(rec.Result())
|
||||
self.latest_result = result
|
||||
print(".", result)
|
||||
text = result.get("text", "")
|
||||
self.publish_if_changed("text", text)
|
||||
print(".", text)
|
||||
else:
|
||||
partial = json.loads(rec.PartialResult())
|
||||
self.latest_partial = partial
|
||||
print("...", partial.get("partial", ""), end='\r')
|
||||
partial = json.loads(rec.PartialResult()).get("partial", "")
|
||||
self.publish_if_changed("partial", partial)
|
||||
print("...", partial, end='\r')
|
||||
|
||||
def destroy_node(self):
|
||||
super().destroy_node()
|
||||
|
|
@ -72,6 +81,7 @@ class VoicePublisher(ThreadedNode):
|
|||
|
||||
|
||||
|
||||
|
||||
def main():
|
||||
rclpy.init()
|
||||
node = VoicePublisher()
|
||||
|
|
|
|||
Loading…
Reference in New Issue