用语言控制你的计算机(Windows系统)

用语言控制你的计算机(Windows系统)

  • 编译环境
  • 实现效果
  • 程序编写
    • 定义关于调用计算机应用类
    • 定义关于语音识别类
    • 定义识别后的输出和动作
    • 总程序
  • 引用场景

编译环境

使用已激活的windous操作系统(任何版本)(以后可能会有Mac教程),并且保证设备带有麦克风。
后安装 speech_recognition库和 os, pyautogui, pyaudio, webbrowser库,在 www.python.org 下载后,通过 cmd 安装(需先安装pip和wheel)。
如发现程序复制后显示密钥不匹配,需自行获取。

实现效果

通过麦克风运用 speech_recognition库和密钥来进行语音识别,并输出在屏幕上,同时使计算机做出相应判断(打开应用)。
在这里插入图片描述
(程序流程图)

程序编写

通过python原装IDLE编译器编写,请事先下好各库。

定义关于调用计算机应用类

通过 os库对系统中的应用的源文件进行调用,并逐个定义成为一个函数备用。

import speech_recognition as sr
import os, pyautogui, pyaudio, webbrowser
def openCalc():
    os.startfile("calc.exe")
def closeCalc():
    os.system("TASKKILL /F /IM calc.exe") 
def openNotepad():
    os.startfile("notepad.exe")
def closeNotepad():
    os.system("TASKKILL /F /IM notepad.exe")
def openMspaint():
    os.startfile("mspaint.exe")
def closeMspaint():
    os.system("TASKKILL /F /IM mspaint.exe")
def weather(city):
    webbrowser.open("https://openweathermap.org/find?q=" + city)
    

最后一条是在浏览器中查找所在城市的天气情况(需获得浏览器许可)。如想增加新应用则如上所示继续定义即可。

定义关于语音识别类

链接到 speech_recognition库, 获取密钥(会过期,如运行报错,请获取新密钥)然后询问并等待回答。

r = sr.Recognizer()
with sr.Microphone() as source:
    userInput = ""
    while "EXIT" not in userInput :
        print("What would you like to do?")
        command = r.listen(source)
        userInput = r.recognize_bing(command, language="en-us",
                                     key="c029db88e02c4b0ebc6f8018548d6cd4")
        userInput = userInput.upper()
        print (userInput)
        

如要调节语音,可将定义调为中文后,将 "en-us" 改为 "zh-cn"(简体)或 "zh-tw"(繁体)。

定义识别后的输出和动作

当识别有结果后,通过刚才设定的开关引用定义来控制计算机开关识别到的应用名。

if "OPEN CALCULATOR" in userInput:
            openCalc()
            userInput = ""
        elif "CLOSE CALCULATOR" in userInput:
            closeCalc()
            userInput = ""
        elif "OPEN NOTEPAD" in userInput:
            openNotepad()
            userInput = ""
        elif "CLOSE NOTEPAD" in userInput:
            closeNotepad()
            userInput = ""
        elif "OPEN PAINT" in userInput:
            openMspaint()
            userInput = ""
        elif "CLOSE PAINT" in userInput:
            closeMspaint()
            userInput = ""
        elif "WEATHER" in userInput:
            print("What city do you want weather for?")
            try:
                command = r.listen(source)
                city = r.recognize_bing(command, language="en-us",
                                     key="c029db88e02c4b0ebc6f8018548d6cd4")[:-1]
            except sr.UnknownValueError:
                None
            print(city)
            weather(city)
            

运行完成后循环,直至关闭程序。

总程序

import speech_recognition as sr
import os, pyautogui, pyaudio, webbrowser
def openCalc():
    os.startfile("calc.exe")
def closeCalc():
    os.system("TASKKILL /F /IM calc.exe") 
def openNotepad():
    os.startfile("notepad.exe")
def closeNotepad():
    os.system("TASKKILL /F /IM notepad.exe")
def openMspaint():
    os.startfile("mspaint.exe")
def closeMspaint():
    os.system("TASKKILL /F /IM mspaint.exe")
def weather(city):
    webbrowser.open("https://openweathermap.org/find?q=" + city)
r = sr.Recognizer()
with sr.Microphone() as source:
    userInput = ""
    while "EXIT" not in userInput :
        print("What would you like to do?")
        command = r.listen(source)
        userInput = r.recognize_bing(command, language="en-us",
                                     key="c029db88e02c4b0ebc6f8018548d6cd4")
        userInput = userInput.upper()
        print (userInput)
        if "OPEN CALCULATOR" in userInput:
            openCalc()
            userInput = ""
        elif "CLOSE CALCULATOR" in userInput:
            closeCalc()
            userInput = ""
        elif "OPEN NOTEPAD" in userInput:
            openNotepad()
            userInput = ""
        elif "CLOSE NOTEPAD" in userInput:
            closeNotepad()
            userInput = ""
        elif "OPEN PAINT" in userInput:
            openMspaint()
            userInput = ""
        elif "CLOSE PAINT" in userInput:
            closeMspaint()
            userInput = ""
        elif "WEATHER" in userInput:
            print("What city do you want weather for?")
            try:
                command = r.listen(source)
                city = r.recognize_bing(command, language="en-us",
                                     key="c029db88e02c4b0ebc6f8018548d6cd4")[:-1]
            except sr.UnknownValueError:
                None
            print(city)
            weather(city) 
            

应用场景

此程序作为语音助手训练项目。并不可代替一些自带语音助手(如Cortana)。但此程序支持大幅度的自定义,如添加功能,修改设置,控制应用,提醒等,只需在程序开头定义,在程序结尾新建判断是否检测到定义的语句,并执行定义的流程即可,有很大的可修改性。

后可能会有关于自己训练程序的文章~ 六年级凉了本人,请支持~

发布了2 篇原创文章 · 获赞 5 · 访问量 158

猜你喜欢

转载自blog.csdn.net/weixin_45739505/article/details/105542317
今日推荐