为什么无法在取景器中打开桌面?

时间:2019-01-31 16:54:06

标签: python python-3.x python-2.7

所以我正在创建一个Siri类型的程序,而我在Mac上,当我对着麦克风讲话并打招呼时,它没问题。但是当我说“打开桌面”时,它并没有在取景器中打开桌面。我真的很困,所以有人可以告诉我我做错了什么吗?我正在使用python2.7

代码:

import speech_recognition as sr
import pyttsx3
import os

speech = sr.Recognizer()

try:
    engine = pyttsx3.init()
except ImportError:
    print('Requested driver is not found')
except RuntimeError:
    print('Driver fails to initialize')

voices = engine.getProperty('voices')

engine.setProperty('voice', 'com.apple.speech.synthesis.voice.Alex')
rate = engine.getProperty('rate')
engine.setProperty('rate', rate)


def speak_text_terminal(terminal):
    engine.say(terminal)
    engine.runAndWait()


def read_voice_terminal():
    voice_text = ''
    print('Listening: ')
    with sr.Microphone() as source:
        audio = speech.listen(source)
    try:
        voice_text = speech.recognize_google(audio)
    except sr.UnknownValueError:
        pass
    except sr.RequestError as e:
        print('Network error!')
    return voice_text

if __name__ == '__main__':

    speak_text_terminal("Hello This is the AI! How can I help?")

    while True:

        voice_note = read_voice_terminal()
        print("terminal: {}".format(voice_note))

        if 'hello' in voice_note:
            speak_text_terminal("Hello How can i help you?")
            continue
        elif 'open' in voice_note:
            os.system('Finder C:// {}'.format(voice_note.replace('Open', '')))
            continue
        elif 'by' in voice_note:
            speak_text_terminal('Goodbye Mate!')
            exit()

1 个答案:

答案 0 :(得分:0)

如评论中所述,您的路径是错误的,也是命令:

os.system('open ~/Desktop/')

编辑:更完整的解决方案:

os.system('open ~/{}'.format(voice_note.lower().replace('open', '').capitalize())