Using Google AIY voice, part 94.
I wrote a useful function, so that any of my Raspberry Pi machines could send a string to the Google Assistant as a message to be spoken aloud, and it worked very well. Here it is...
# A function to send a string to PiAssistant for output as speech
import paramiko
ssh = paramiko.SSHClient()
ssh.load_host_keys(filename='/home/pi/.ssh/known_hosts')
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
server, username, password = ('PiAssistant', 'pi', 'your_password-goes_here')
def say_this_with_assistant(say):
ssh.connect(server, username=username, password=password)
command = "python ~/AIY-projects-python/src/aiy/voice/tts.py \"" + say + "\" --lang en-GB --volume 10 --pitch 60"
ssh.exec_command(command)
ssh.close()
return
print("Starting...\n")
say_this_with_assistant("Normal service will be resumed as soon as possible. ")
print("Finished.\n")
The snag occurs when two, or more, machines want to say something at the same time. The PiAssistant calmly multitasks, and says the strings at the same time, which sounds interesting, but tends to be incomprehensible. I hoped that voice/tts.py would have some way of letting other programs know whether it was busy, but it doesn't. However, the low level Linux ps command can tell when voice/tts.py is running, so I added a check for that as a Paramiko command, like this...
# A function to send a string to PiAssistant for output as speech when it's free
import paramiko
import time
ssh = paramiko.SSHClient()
ssh.load_host_keys(filename='/home/pi/.ssh/known_hosts')
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
def say_when_free(say):
server, username, password = ('PiAssistant', 'pi', 'your_password-goes_here')
ssh.connect(server, username=username, password=password)
command = "ps -ef | grep -v grep | grep \"voice.tts\" | wc -l"
while True:
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(command)
answer = str(ssh_stdout.read())
if answer[2] == "0":
break
command = "python ~/AIY-projects-python/src/aiy/voice/tts.py \"" + say + "\" --lang en-GB --volume 10 --pitch 60"
ssh.exec_command(command)
ssh.close()
return
print("Starting...\n")
say_when_free("I waited until I was allowed to say this. ")
print("Finished.\n")
I'm not going to claim any of this as properly written Python, the best way to do what is required, or even particularly original, but it does currently seem to be the only description of how to do this that's on the internet.
No comments:
Post a Comment