搭建语音助手打开大括号中有路径的应用
Build a voice assistant to open the application with a path in the curly bracket
想着能不能加很多打开应用程序的路径,但是我的代码只在要求打开时才打开计算器chrome。它像这样显示我 The file /System/Applications/Google Chrome.app does not exist.
假设您想修复我的代码以使其更好。顺便说一下,我在 mac。谢谢。
我的代码:
import speech_recognition as sr
from gtts import gTTS
import playsound
import os
import subprocess as sp
import random
def take_command():
r = sr.Recognizer()
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source, duration=1)
print('listening...')
audio = r.listen(source)
try:
text = r.recognize_google(audio)
text = text.lower()
print(f"user: {text}") # print user app said
except sr.UnknownValueError:
speak('Sorry, I did not get that')
text = None
except sr.RequestError:
speak('Request error')
text = None
return text
def speak(audio_string):
tts = gTTS(text=audio_string, lang='en-uk', slow=False) # text to speech(voice)
recognizer = random.randint(1, 20000000)
audio_file = 'audio' + str(recognizer) + '.mp3'
tts.save(audio_file) # save as mp3
print('[1m' f"computer: {audio_string}") # print what app said
playsound.playsound(audio_file) # play the audio file
os.remove(audio_file) # remove audio file
paths = {
"chrome": ['open', '/System/Applications/Google Chrome.app'],
"excel": ['open', '/System/Applications/Microsoft Excel.app'],
"calculator": ['open', '/System/Applications/Calculator.app'],
}
if __name__ == '__main__':
while True:
query = take_command()
if str(query) in paths:
app = paths.get(query)
sp.call(app)
elif 'bye' in str(query):
exit()
else:
speak('Please repeat the command.')
终端机:
listening...
user: chrome
The file /System/Applications/Google Chrome.app does not exist.
listening...
user: calculator
listening...
user: bye
尝试将 Chrome 路径指定为:
paths = {
"chrome": ['open', '/Applications/Google Chrome.app'],
"excel": ['open', '/System/Applications/Microsoft Excel.app'],
"calculator": ['open', '/System/Applications/Calculator.app'],
}
这是默认的 chrome 安装位置。
想着能不能加很多打开应用程序的路径,但是我的代码只在要求打开时才打开计算器chrome。它像这样显示我 The file /System/Applications/Google Chrome.app does not exist.
假设您想修复我的代码以使其更好。顺便说一下,我在 mac。谢谢。
我的代码:
import speech_recognition as sr
from gtts import gTTS
import playsound
import os
import subprocess as sp
import random
def take_command():
r = sr.Recognizer()
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source, duration=1)
print('listening...')
audio = r.listen(source)
try:
text = r.recognize_google(audio)
text = text.lower()
print(f"user: {text}") # print user app said
except sr.UnknownValueError:
speak('Sorry, I did not get that')
text = None
except sr.RequestError:
speak('Request error')
text = None
return text
def speak(audio_string):
tts = gTTS(text=audio_string, lang='en-uk', slow=False) # text to speech(voice)
recognizer = random.randint(1, 20000000)
audio_file = 'audio' + str(recognizer) + '.mp3'
tts.save(audio_file) # save as mp3
print('[1m' f"computer: {audio_string}") # print what app said
playsound.playsound(audio_file) # play the audio file
os.remove(audio_file) # remove audio file
paths = {
"chrome": ['open', '/System/Applications/Google Chrome.app'],
"excel": ['open', '/System/Applications/Microsoft Excel.app'],
"calculator": ['open', '/System/Applications/Calculator.app'],
}
if __name__ == '__main__':
while True:
query = take_command()
if str(query) in paths:
app = paths.get(query)
sp.call(app)
elif 'bye' in str(query):
exit()
else:
speak('Please repeat the command.')
终端机:
listening...
user: chrome
The file /System/Applications/Google Chrome.app does not exist.
listening...
user: calculator
listening...
user: bye
尝试将 Chrome 路径指定为:
paths = {
"chrome": ['open', '/Applications/Google Chrome.app'],
"excel": ['open', '/System/Applications/Microsoft Excel.app'],
"calculator": ['open', '/System/Applications/Calculator.app'],
}
这是默认的 chrome 安装位置。