PowerShell Speech Recognition

So I got bored this weekend and started looking into “System.Speech”.  First it started with making my computer talk and then I realized I could talk to my computer by utilizing “System.Speech.Recognition.SpeechRecognitionEngine”.  Below are the results of my boredom, and with this script you can give your computer verbal commands.  Enjoy.

[void][System.Reflection.Assembly]::LoadWithPartialName("System.Speech");

##Setup the speaker, this allows the computer to talk
$speaker = [System.Speech.Synthesis.SpeechSynthesizer]::new();
$speaker.SelectVoice("Microsoft Zira Desktop");

##Setup the Speech Recognition Engine, this allows the computer to listen
$speechRecogEng = [System.Speech.Recognition.SpeechRecognitionEngine]::new();

##Setup the verbal commands hello and exit
$grammar = [System.Speech.Recognition.GrammarBuilder]::new();
$grammar2 = [System.Speech.Recognition.GrammarBuilder]::new();
$grammar.Append("hello");
$grammar2.Append("exit");
$speechRecogEng.LoadGrammar($grammar);
$speechRecogEng.LoadGrammar($grammar2);

$speechRecogEng.InitialSilenceTimeout = 15
$speechRecogEng.SetInputToDefaultAudioDevice();
$cmdBoolean = $false;

while (!$cmdBoolean) {
    $speechRecognize = $speechRecogEng.Recognize();
    $conf = $speechRecognize.Confidence;
    $myWords = $speechRecognize.text;
    if ($myWords -match "hello" -and [double]$conf -gt 0.85) {
        $speaker.Speak("Hello");
    }
    if ($myWords -match "exit" -and [double]$conf -gt 0.85) {
        $speaker.Speak("Goodbye");
        $cmdBoolean = $true;
    }
}

 

Advertisements

One thought on “PowerShell Speech Recognition

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s