Getting started with Python - Speech Recognition

Python voice

Principle of voice control

Speech control is divided into speech recognition and speech reading

We use speech module to implement speech module (python 2.7)

SAPI is Microsoft Speech API, which is a voice interface launched by Microsoft. Careful people will find that since WINXP, there has been a voice recognition function on the system, but there is very little space for use. He did not give some user-friendly customized solutions, and the only voice control command appears to be quite threatening.  
 
  • Python pywin32 , enabling Python to call the WIN32COM interface, select the corresponding version to download (distinguish 32-bit / 64 bit), and double-click directly to run

  • Install speech module: pip install speech

Realize a simple small program to control the computer to do things:

  • First, a test file

This is only for starting and shutting down the voice system

import speech
while True:
    phrase =speech.input()
    speech.say("You said %s"%phrase)
    if phrase =="turn off":
        break

  • Self made Chinese Library
phrase = {"closeMainSystem" : "Turn off human-computer interaction"
        , "film" : "I want to see a movie"
        , "listenMusic" : "I'm tired."
        , "blog" : "Read blog"
        , "cmd" : "cmd" }
  • Design voice corresponding computer operation
def callback(phr, phrase):
    if phr == phrase["closeMainSystem"]:
        speech.say("Goodbye. Human computer interaction will be closed soon, thank you for using")
        speech.stoplistening() 
        sys.exit()
    elif phr == phrase["film"]:
        speech.say("Opening Youku for you")
        webbrowser.open_new("http://www.youku.com/")
    elif phr == phrase["listenMusic"]:
        speech.say("About to launch Douban radio station for you")
        webbrowser.open_new("http://douban.fm/")
    elif phr == phrase["blog"]:
        speech.say("About to enter Dreamforce.me")
        webbrowser.open_new("http://www.cnblogs.com/darksouls/")
    elif phr == phrase["cmd"]:
        speech.say("About to open CMD")
        os.popen("C:\Windows\System32\cmd.exe")

    # You can continue to write the corresponding operations in the corresponding self-made Chinese library with elif
  • main program
while True:
    phr = speech.input()
    speech.say("You said %s" % phr)
    callback(phr, phrase)

  • Full code
# _*_ coding:utf-8 _*_

import os
import sys
import speech
import webbrowser

phrase = {"closeMainSystem" : "Turn off human-computer interaction"
        , "film" : "I want to see a movie"
        , "listenMusic" : "I'm tired."
        , "blog" : "Read blog"
        , "cmd" : "cmd" }


def callback(phr, phrase):
    if phr == phrase["closeMainSystem"]:
        speech.say("Goodbye. Human computer interaction will be closed soon, thank you for using")
        speech.stoplistening() 
        sys.exit()
    elif phr == phrase["film"]:
        speech.say("Opening Youku for you")
        webbrowser.open_new("http://www.youku.com/")
    elif phr == phrase["listenMusic"]:
        speech.say("About to launch Douban radio station for you")
        webbrowser.open_new("http://douban.fm/")
    elif phr == phrase["blog"]:
        speech.say("About to enter Dreamforce.me")
        webbrowser.open_new("http://www.cnblogs.com/darksouls/")
    elif phr == phrase["cmd"]:
        speech.say("About to open CMD")
        os.popen("C:\Windows\System32\cmd.exe")

    # You can continue to write the corresponding operations in the corresponding self-made Chinese library with elif

while True:
    phr = speech.input()
    speech.say("You said %s" % phr)
    callback(phr, phrase)

We found a voice recognition framework on the Internet:

from win32com.client import constants
import os
import win32com.client
import pythoncom

speaker = win32com.client.Dispatch("SAPI.SPVOICE")


class SpeechRecognition:
    def __init__(self, wordsToAdd):
    self.speaker = win32com.client.Dispatch("SAPI.SpVoice")
    self.listener = win32com.client.Dispatch("SAPI.SpSharedRecognizer")
    self.context = self.listener.CreateRecoContext()
    self.grammar = self.context.CreateGrammar()
    self.grammar.DictationSetState(0)
    self.wordsRule = self.grammar.Rules.Add(
        "wordsRule", constants.SRATopLevel + constants.SRADynamic, 0)
    self.wordsRule.Clear()
    [self.wordsRule.InitialState.AddWordTransition(
        None, word) for word in wordsToAdd]
    self.grammar.Rules.Commit()
    self.grammar.CmdSetRuleState("wordsRule", 1)
    self.grammar.Rules.Commit()
    self.eventHandler = ContextEvents(self.context)
    self.say("Started successfully")
    def say(self, phrase):
        self.speaker.Speak(phrase)
class ContextEvents(win32com.client.getevents("SAPI.SpSharedRecoContext")):
    def OnRecognition(self, StreamNumber, StreamPosition, RecognitionType, Result):
        newResult = win32com.client.Dispatch(Result)
        print("You're talking ", newResult.PhraseInfo.GetText())
        speechstr=newResult.PhraseInfo.GetText()
        # The following is the corresponding speech recognition information
        if  speechstr=="Zhang San":
            speaker.Speak("lisi")
        elif  speechstr=="Hello":
            speaker.Speak("hello world")
        elif  speechstr=="Happy National Day":
            speaker.Speak("Happy   nationalday")
        elif  speechstr=="happy new year":
            speaker.Speak("happy  New Year")
        elif  speechstr=="Li Si":
            speaker.Speak("a  beauty baby")
        elif  speechstr=="Wang Wu":
            speaker.Speak("a  little boy")
        elif  speechstr=="Zhao Liu":
            speaker.Speak("a  boy  can  coding")
        else:
            pass

if __name__ == '__main__':

    speaker.Speak("Speech recognition on")
    wordsToAdd = ["Zhang San",
                  "Hello",
                  "Happy National Day",
                  "happy new year",
                  "Li Si",
                  "Wang Wu",
                  "Zhao Liu",]
    speechReco = SpeechRecognition(wordsToAdd)
    while True:
        pythoncom.PumpWaitingMessages()

Tags: Python Windows pip

Posted on Mon, 04 May 2020 16:46:18 -0400 by Illusionist