使用已激活的windous操作系统(任何版本)(以后可能会有Mac教程),并且保证设备带有麦克风。 通过麦克风运用 speech_recognition库和密钥来进行语音识别,并输出在屏幕上,同时使计算机做出相应判断(打开应用)。 通过python原装IDLE编译器编写,请事先下好各库。 通过 os库对系统中的应用的源文件进行调用,并逐个定义成为一个函数备用。 最后一条是在浏览器中查找所在城市的天气情况(需获得浏览器许可)。如想增加新应用则如上所示继续定义即可。 链接到 speech_recognition库, 获取密钥(会过期,如运行报错,请获取新密钥)然后询问并等待回答。 如要调节语音,可将定义调为中文后,将 当识别有结果后,通过刚才设定的开关引用定义来控制计算机开关识别到的应用名。 运行完成后循环,直至关闭程序。 此程序作为语音助手训练项目。并不可代替一些自带语音助手(如Cortana)。但此程序支持大幅度的自定义,如添加功能,修改设置,控制应用,提醒等,只需在程序开头定义,在程序结尾新建判断是否检测到定义的语句,并执行定义的流程即可,有很大的可修改性。 后可能会有关于自己训练程序的文章~ 六年级凉了本人,请支持~
用语言控制你的计算机(Windows系统)
编译环境
后安装 speech_recognition库和 os, pyautogui, pyaudio, webbrowser库,在 www.python.org 下载后,通过 cmd 安装(需先安装pip和wheel)。
如发现程序复制后显示密钥不匹配,需自行获取。实现效果
(程序流程图)程序编写
定义关于调用计算机应用类
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)
"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)
应用场景
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算