analitics

Pages

Wednesday, May 22, 2019

Python 3.7.3 : Using the win32com - part 001.

The tutorial is about win32com python module using the python 3.7.3.
First exameple is simple:
import sys
import win32com.client
from win32com.client import constants

speaker = win32com.client.Dispatch("SAPI.SpVoice")
print ("Type word or phrase, then enter.")
while 1:
      text_to_say = input("> ")
      speaker.Speak(text_to_say)
Let's run it.
The Microsoft Speech SDK to speak what you type in from the keyboard.
C:\Python373>python.exe speak_001.py
Type word or phrase, then enter.
> this is a test
The next example is more complex and uses speech recognition to create outputs.
You can change this example to execute tasks.
# This is a sample code for speech recognition using the MS Speech API

from win32com.client import constants
import win32com.client
import pythoncom
 
""" 
You will see a voice say: Started successfully
The you need to start the speech recognition tool.
After running this, then saying "One", "Two", "Three" or "Four" should
display "You said One" etc on the console.
"""
 
class SpeechRecognition:
    """ Initialize the speech recognition with the passed in list of words """
    def __init__(self, wordsToAdd):

        # For text-to-speech
        self.speaker = win32com.client.Dispatch("SAPI.SpVoice")

        # For speech recognition - first create a listener
        self.listener = win32com.client.Dispatch("SAPI.SpSharedRecognizer")

        # Then a recognition context
        self.context = self.listener.CreateRecoContext()

        # which has an associated grammar
        self.grammar = self.context.CreateGrammar()

        # Do not allow free word recognition - only command and control
        # recognizing the words in the grammar only
        self.grammar.DictationSetState(0)

        # Create a new rule for the grammar, that is top level (so it begins
        # a recognition) and dynamic (ie we can change it at runtime)
        self.wordsRule = self.grammar.Rules.Add("wordsRule",
                        constants.SRATopLevel + constants.SRADynamic, 0)

        # Clear the rule (not necessary first time, but if we're changing it
        # dynamically then it's useful)
        self.wordsRule.Clear()

        # And go through the list of words, adding each to the rule
        [ self.wordsRule.InitialState.AddWordTransition(None, word) for word in wordsToAdd ]

        # Set the wordsRule to be active
        self.grammar.Rules.Commit()
        self.grammar.CmdSetRuleState("wordsRule", 1)

        # Commit the changes to the grammar
        self.grammar.Rules.Commit()

        # And add an event handler that's called back when recognition occurs
        self.eventHandler = ContextEvents(self.context)

        # Announce we've started
        self.say("Started successfully")
    """Speak a word or phrase"""
    def say(self, phrase):
        self.speaker.Speak(phrase)
 
"""
The callback class that handles the events raised by the speech object.
"""

class ContextEvents(win32com.client.getevents("SAPI.SpSharedRecoContext")):

    """Called when a word/phrase is successfully recognized and is
        found in a currently open grammar """

    def OnRecognition(self, StreamNumber, StreamPosition, RecognitionType, Result):
        newResult = win32com.client.Dispatch(Result)
        print ("You said: ",newResult.PhraseInfo.GetText())
    
if __name__=='__main__':
    wordsToAdd = [ "One", "Two", "Three", "Four" ]
    speechReco = SpeechRecognition(wordsToAdd)
    while 1:
        pythoncom.PumpWaitingMessages()
The result can be see on my youtube channel: