用Python编程实现语音控制电脑
电脑面前的你,是否也希望能让电脑听命于你? 当你累的时候,只需说一声“我累了”,电脑就会放着优雅的轻音乐来让你放松。 或许你希望你在百忙之中,能让电脑郎读最新的NBA比分赛况….一切都是那么惬意。
在此告诉你,不要灰心,我们真的可以做一个。
做一个语音识别? 我相信很多人到这里会有两个心态,一是好奇,二是避之千里。
其实不然,你可以不用懂太多的编程技能,你甚至也可以不用懂自然语言处理技术,这篇文章虽然实现了语音操控但是绝没有你们想象的那么复杂。 如果仅仅把语音识别作为一个实现了的接口的话,剩下的逻辑就仅仅是IF-ELSE这些简单的元素了。
实现语音操控的原理
语音操控分为 语音识别和语音朗读两部分。
这两部分本来是需要自然语言处理技能相关知识以及一系列极其复杂的算法才能搞定,可是这篇文章将会跳过此处,如果你只是对算法和自然语言学感兴趣的话,就只有请您移步了,下面没有一个字会讲述到这些内容。
早在上世纪90年代的时候,IBM就推出了一款极为强大的语音识别系统-vio voice , 而其后相关产品层出不穷,不断的进化和演变着。 我们这里将会使用SAPI实现语音模块。
什么是SAPI?
SAPI是微软Speech API , 是微软公司推出的语音接口,而细心的人会发现从WINXP开始,系统上就已经有语音识别的功能了,可是用武之地相当之少,他并没有给出一些人性化的自定义方案,仅有的语音操控命令显得相当鸡胁。 那么这篇文章的任务就是利用SAPI进行个性化的语音识别。
准备阶段,你至少需要安装以下的工具:
Python2.7 http://www.python.org/
强烈建诡使用2.7,至今Python2.7拥有Python系列为数最多的工具和应用支持,同时也相对比较稳定。
Win32Com http://starship.python.net/~skippy/win32/Downloads.html
Python Win32增强工具,可以使Python调用WIN32COM接口,这个工具的出现使得Python变得无比强大
Speech.py http://pypi.python.org/pypi/speech/
这个是极为精简的封装模块,此处为可选项,当然我不建议重复造轮子,还是下吧,目前只支持Python2.6,但不用灰心,Python2.6和Python2.7的代码是兼容,不会有异常。
安装过程请依至上而下的顺序。
开发阶段
当你安装了上述的相关工具后,你就可以进行开发了:
先进行一个简单的环境调试:
whileTrue:
phrase =speech.input()
speech.say("You said %s"%phrase)
ifphrase =="turn off":
break
上述代码是启动语音识别器,同时系统将会重复你所录入的语音,当遇到“turn off”时,就会自动关闭识别系统。
如果你通过测试无误的话,我们就可以开始进行扩展开发了。
1. 定义中文语义库
openEclipse ="我要写程序"
listenMusic ="我好累啊"
blog ="看博客"
php ="php"
java ="JAVA"
2. 定义相关语义操作逻辑
print(": %s"%phrase)
ifphrase ==closeMainSystem:
speech.say("Goodbye. 人机交互即将关闭,谢谢使用")
listener.stoplistening()
sys.exit()
elifphrase ==openEclipse:
speech.say("请问您要写PYTHON还是JAVA程序?")
speech.listenforanything(callback)
elifphrase ==listenMusic:
speech.say("即将为你启动豆瓣电台")
webbrowser.open_new("http://douban.fm/")
elifphrase ==blog:
speech.say("即将进入Dreamforce.me")
webbrowser.open_new("http://dreamforce.me/")
elifphrase ==php:
speech.say("启动PHP编写器")
os.popen("E:\IDE\php_eclipse\eclipse\eclipse.exe")
elifphrase ==php:
speech.say("启动JAVA编写器")
os.popen("E:\IDE\php_eclipse\eclipse\eclipse.exe")
其中,os.popen是异步开启程序,此操作不会单独开启一个SHELL窗口,也不会阻塞当前进程。
speech.say() 是调用SAPI进行参数朗读。
webbrowser.open_new()是打开网页。
3.程序运行主体搭建
whilelistener.islistening():
text =input()
iftext =="不要语音了":
listener.stoplistening()
sys.exit()
else:
speech.say(text)
此段为运行主体,大意是开启语音监听,同时支持终端输入模式。如果你嗓子哑了的话,也可以打字来实现,哈哈~~

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

This tutorial demonstrates how to use Python to process the statistical concept of Zipf's law and demonstrates the efficiency of Python's reading and sorting large text files when processing the law. You may be wondering what the term Zipf distribution means. To understand this term, we first need to define Zipf's law. Don't worry, I'll try to simplify the instructions. Zipf's Law Zipf's law simply means: in a large natural language corpus, the most frequently occurring words appear about twice as frequently as the second frequent words, three times as the third frequent words, four times as the fourth frequent words, and so on. Let's look at an example. If you look at the Brown corpus in American English, you will notice that the most frequent word is "th

This article explains how to use Beautiful Soup, a Python library, to parse HTML. It details common methods like find(), find_all(), select(), and get_text() for data extraction, handling of diverse HTML structures and errors, and alternatives (Sel

Dealing with noisy images is a common problem, especially with mobile phone or low-resolution camera photos. This tutorial explores image filtering techniques in Python using OpenCV to tackle this issue. Image Filtering: A Powerful Tool Image filter

This article compares TensorFlow and PyTorch for deep learning. It details the steps involved: data preparation, model building, training, evaluation, and deployment. Key differences between the frameworks, particularly regarding computational grap

Python, a favorite for data science and processing, offers a rich ecosystem for high-performance computing. However, parallel programming in Python presents unique challenges. This tutorial explores these challenges, focusing on the Global Interprete

This tutorial demonstrates creating a custom pipeline data structure in Python 3, leveraging classes and operator overloading for enhanced functionality. The pipeline's flexibility lies in its ability to apply a series of functions to a data set, ge

Serialization and deserialization of Python objects are key aspects of any non-trivial program. If you save something to a Python file, you do object serialization and deserialization if you read the configuration file, or if you respond to an HTTP request. In a sense, serialization and deserialization are the most boring things in the world. Who cares about all these formats and protocols? You want to persist or stream some Python objects and retrieve them in full at a later time. This is a great way to see the world on a conceptual level. However, on a practical level, the serialization scheme, format or protocol you choose may determine the speed, security, freedom of maintenance status, and other aspects of the program

Python's statistics module provides powerful data statistical analysis capabilities to help us quickly understand the overall characteristics of data, such as biostatistics and business analysis. Instead of looking at data points one by one, just look at statistics such as mean or variance to discover trends and features in the original data that may be ignored, and compare large datasets more easily and effectively. This tutorial will explain how to calculate the mean and measure the degree of dispersion of the dataset. Unless otherwise stated, all functions in this module support the calculation of the mean() function instead of simply summing the average. Floating point numbers can also be used. import random import statistics from fracti
