Home Backend Development Python Tutorial 用Python编程实现语音控制电脑

用Python编程实现语音控制电脑

Jun 16, 2016 am 08:44 AM

电脑面前的你,是否也希望能让电脑听命于你?   当你累的时候,只需说一声“我累了”,电脑就会放着优雅的轻音乐来让你放松。 或许你希望你在百忙之中,能让电脑郎读最新的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. 定义中文语义库

复制代码 代码如下:
closeMainSystem ="关闭人机交互"
openEclipse ="我要写程序"
listenMusic ="我好累啊"
blog ="看博客"
php ="php"
java ="JAVA"


2. 定义相关语义操作逻辑

复制代码 代码如下:
defcallback(phrase, listener):
    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.程序运行主体搭建

复制代码 代码如下:
listener =speech.listenforanything(callback)
whilelistener.islistening():
     text =input()
     iftext =="不要语音了":
         listener.stoplistening()
         sys.exit()
     else:
         speech.say(text)

此段为运行主体,大意是开启语音监听,同时支持终端输入模式。如果你嗓子哑了的话,也可以打字来实现,哈哈~~

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
Python vs. C  : Applications and Use Cases Compared Python vs. C : Applications and Use Cases Compared Apr 12, 2025 am 12:01 AM

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

Python: Games, GUIs, and More Python: Games, GUIs, and More Apr 13, 2025 am 12:14 AM

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

Python vs. C  : Learning Curves and Ease of Use Python vs. C : Learning Curves and Ease of Use Apr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python and Time: Making the Most of Your Study Time Python and Time: Making the Most of Your Study Time Apr 14, 2025 am 12:02 AM

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python vs. C  : Exploring Performance and Efficiency Python vs. C : Exploring Performance and Efficiency Apr 18, 2025 am 12:20 AM

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.

Which is part of the Python standard library: lists or arrays? Which is part of the Python standard library: lists or arrays? Apr 27, 2025 am 12:03 AM

Pythonlistsarepartofthestandardlibrary,whilearraysarenot.Listsarebuilt-in,versatile,andusedforstoringcollections,whereasarraysareprovidedbythearraymoduleandlesscommonlyusedduetolimitedfunctionality.

Python: Automation, Scripting, and Task Management Python: Automation, Scripting, and Task Management Apr 16, 2025 am 12:14 AM

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

Learning Python: Is 2 Hours of Daily Study Sufficient? Learning Python: Is 2 Hours of Daily Study Sufficient? Apr 18, 2025 am 12:22 AM

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.

See all articles