问题描述
我正在尝试创建一个小脚本,该脚本将使用语音命令启动所需的程序。它已经可以正常工作了,但是最后,当脚本假设停止工作时,它会导致 python 崩溃......弹出消息“Python 已停止工作”。当一切正常时很烦人,但最终还是出了点问题。也许你可以帮我找出错误。珍惜你的时间。代码如下:
from gtts import gTTS
import playsound
import os
import speech_recognition as sr
#Create switch statement for launching programs
def programs_to_launch_list(programs_set_name):
editing = [r"C:\\Program Files\\Adobe\\Adobe Premiere Pro 2020\\Adobe Premiere Pro.exe",r"C:\\Program Files\\Red Giant\\PluralEyes 4\\PluralEyes 4.exe"]
recording = [r"C:\\Program Files\\obs-studio\\bin\\64bit\\obs64.exe",r"C:\\Program Files (x86)\\Audacity\\audacity.exe"]
internet = [r"C:\\Program Files\\Mozilla Firefox\\firefox.exe",r"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"]
switcher={
'editing':editing,'internet':internet,'recording':recording
}
return switcher.get(programs_set_name,False)
#fn to say and print the given phrase (TextToSpeech)
def tell(phrase):
file = 'phrase.mp3'
tts = gTTS(phrase)
tts.save(file)
playsound.playsound(file)
print(phrase)
os.remove(file)
def transcribeSpeech(recogniser,microphone):
if not isinstance(recogniser,sr.Recognizer):
raise TypeError('recognizer is not of type Recognizer')
elif not isinstance(mic,sr.Microphone):
raise TypeError('mic is not of type Microphone')
tell('Tell me what set of software do I need to launch! In the mic please!')
with microphone as source:
audio = recogniser.listen(source)
try:
text=recogniser.recognize_google(audio)
return text
except sr.RequestError:
#Unable to recognise speech!
return "ERROR1"
except:
#Other error
return "ERROR2"
if __name__ == '__main__':
#init speech recognizer
mrRobot = sr.Recognizer()
#init your mic
mic = sr.Microphone()
text = str(transcribeSpeech(mrRobot,mic))
if text=="ERROR1" or text=="ERROR2":
tell("Some error occured...")
else:
#If there's a list for voice command
if programs_to_launch_list(text):
list_of_programs_to_launch = programs_to_launch_list(text)
#Launch every program in the list
for program_path in list_of_programs_to_launch:
os.startfile(program_path)
tell('All the software from ' + str(text) + ' already launched')
else:
#Else if there's no appropriate command/set of programs
tell("Sorry... There was some error. Maybe you've said the command unclearly. Give me a break! I'm just a tiny script,not an AI!")
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)