在启动时运行语音识别python脚本

时间:2020-03-30 18:37:42

标签: python

我有一个python脚本,可以侦听某些关键字并执行诸如重启PC或更新PC之类的操作,我将python文件放入了应用程序自动启动(MX Linux)中,但似乎无法正常工作,当我运行该文件时,它可以正常工作,但我想在计算机启动时在后台运行该文件,谢谢。

import speech_recognition as s_r
import subprocess
import time
import os


r = s_r.Recognizer()
my_mic_device = s_r.Microphone(device_index=7)

def start():
    response = None
    audio = None
    subprocess.Popen("clear")
    print("---READY---")

    with my_mic_device as source:
        r.adjust_for_ambient_noise(source)
        audio = r.listen(source, phrase_time_limit=2)

    try:
        response = r.recognize_google(audio)
        response = response.lower()
    except s_r.UnknownValueError:
        print("Unknown value")
    except s_r.RequestError:
        print("Request error")

    if response == "firefox":
        subprocess.Popen("firefox")
        start()
    elif response == "off":
        os.system("sudo poweroff")
    elif response == "reboot":
        os.system("sudo reboot")
    elif response == "terminal":
        subprocess.Popen("terminator")
        start()
    else:
        start()

start()

0 个答案:

没有答案