Voice control computer with Python programming

  • 2020-04-02 13:34:56
  • OfStack

The computer in front of you, whether also hope can let the computer obey you?     When you are tired, just say "I am tired" and the computer will play soft and elegant music to relax you. Maybe you wish you had a computer to read the latest NBA scores from your busy schedule... .everything is so comfortable.

I'm here to tell you, don't lose heart, we can really make one.
Do a speech recognition? I believe many people will come here with two mindsets, one is curiosity, the other is to avoid.

You don't need to know much about programming skills, you don't even need to know about natural language processing, and this article is not as complicated as you might think. IF speech recognition is just an implemented interface, the rest of the logic is just simple elements like if-else.


Achieve the principle of voice control

Speech control is divided into speech recognition and speech reading two parts.

These two parts require natural language processing skills and an extremely complex set of algorithms, but this article will skip over them, and if you're interested in just algorithms and natural linguistics, you'll have to move on.

As early as the 1990s, IBM launched a very powerful speech recognition system, the vio voice, and since then related products have emerged in an endless stream of evolution and evolution. We will use SAPI to implement the speech module.

What is SAPI?

SAPI is Microsoft Speech API, is the voice interface launched by Microsoft company, and careful people will find that since WINXP, the system has already had the function of voice recognition, but very little use, he did not give some humanized custom program, the only voice control command is quite chicken. So the task of this article is to use SAPI for personalized speech recognition.

In preparation, you need to install at least the following tools:

Python2.7       http://www.python.org/

With 2.7, Python2.7 has by far the largest number of tools and applications in the Python family and is relatively stable.

Win32Com   http://starship.python.net/~skippy/win32/Downloads.html


Python Win32 enhancement tool, can make Python call WIN32COM interface, this tool makes Python extremely powerful

Researched. Py       http://pypi.python.org/pypi/speech/

This is a very compact encapsulation module, it's optional here, of course I don't recommend repeating the wheel, let's go, it only supports Python2.6 at the moment, but don't be discouraged, Python2.6 and Python2.7 code is compatible, there are no exceptions.

Please follow the sequence of installation from top to bottom.

The development phase

After you have installed the above related tools, you are ready to develop:

Start with a simple environment debugging:


whileTrue:
    phrase =speech.input()
    speech.say("You said %s"%phrase)
    ifphrase =="turn off":
        break

The code above starts the speech recognizer, and the system will repeat the speech you have entered. When the "turn off" occurs, the recognition system will be automatically turned off.
If you pass the test correctly, we can start the extension development.

1. Define Chinese semantic library

closeMainSystem =" Turn off human-computer interaction "
openEclipse =" I'm going to write a program "
listenMusic =" I am so tired "
blog =" The blog "
php ="php"
java ="JAVA"


2. Define the relevant semantic operation logic

defcallback(phrase, listener):
    print(": %s"%phrase)
    ifphrase ==closeMainSystem:
        speech.say("Goodbye.  Human-computer interaction is about to close, thanks for using ")
        listener.stoplistening()
        sys.exit()
    elifphrase ==openEclipse:
        speech.say(" Would you like to write PYTHON or JAVA The program? ")
        speech.listenforanything(callback)
    elifphrase ==listenMusic:
        speech.say(" About to launch douban radio for you ")
        webbrowser.open_new("http://douban.fm/")
    elifphrase ==blog:
        speech.say(" Is about to enter Dreamforce.me")
        webbrowser.open_new("http://dreamforce.me/")
    elifphrase ==php:
        speech.say(" Start the PHP The writer ")
        os.popen("E:IDEphp_eclipseeclipseeclipse.exe")
    elifphrase ==php:
        speech.say(" Start the JAVA The writer ")
        os.popen("E:IDEphp_eclipseeclipseeclipse.exe")


Os.popen is an asynchronous opener that does not open a separate SHELL window or block the current process.
Speech.say () is a call to SAPI for parameter reading.
Webbrowser. Open_new () is to open a web page.

3. Build the main body of the program

listener =speech.listenforanything(callback)
whilelistener.islistening():
     text =input()
     iftext ==" No more speech ":
         listener.stoplistening()
         sys.exit()
     else:
         speech.say(text)

This section is the main part of the operation. It basically turns on voice monitoring and supports terminal input mode. If you lose your voice, you can type it, too


Related articles: