mirror of
https://github.com/fleschutz/PowerShell.git
synced 2025-03-26 14:26:10 +01:00
Improved voice-control.ps1
This commit is contained in:
parent
1be2c719e6
commit
0609414148
@ -9,44 +9,49 @@
|
||||
[double]$MinConfidence = 0.05
|
||||
$PathToRepo = "$PSScriptRoot/.."
|
||||
|
||||
write-output "Initializing the speech recognition engine..."
|
||||
$speechRecogEng = [System.Speech.Recognition.SpeechRecognitionEngine]::new();
|
||||
#$speechRecogEng.InitialSilenceTimeout = 15
|
||||
$speechRecogEng.SetInputToDefaultAudioDevice();
|
||||
try {
|
||||
write-output "Initializing the speech recognition engine..."
|
||||
$null = [Reflection.Assembly]::LoadWithPartialName("System.Speech")
|
||||
$speechRecogEng = [System.Speech.Recognition.SpeechRecognitionEngine]::new()
|
||||
#$speechRecogEng.InitialSilenceTimeout = 15
|
||||
$speechRecogEng.SetInputToDefaultAudioDevice()
|
||||
|
||||
write-output "Loading the grammar..."
|
||||
$Items = get-childItem -path "$PathToRepo/Scripts/"
|
||||
$Count = 0
|
||||
foreach ($Item in $Items) {
|
||||
$Name = $Item.Name.Split('.')[0]
|
||||
if ($Name -eq "") { continue }
|
||||
$grammar1 = [System.Speech.Recognition.GrammarBuilder]::new()
|
||||
$grammar1.Append($Name)
|
||||
$speechRecogEng.LoadGrammar($grammar1)
|
||||
$Count++
|
||||
write-output "Loading the grammar..."
|
||||
$Items = get-childItem -path "$PathToRepo/Scripts/"
|
||||
$Count = 0
|
||||
foreach ($Item in $Items) {
|
||||
$Name = $Item.Name.Split('.')[0]
|
||||
if ($Name -eq "") { continue }
|
||||
$grammar1 = [System.Speech.Recognition.GrammarBuilder]::new()
|
||||
$grammar1.Append($Name)
|
||||
$speechRecogEng.LoadGrammar($grammar1)
|
||||
$Count++
|
||||
}
|
||||
|
||||
$grammar2 = [System.Speech.Recognition.GrammarBuilder]::new()
|
||||
$grammar2.Append("exit")
|
||||
$speechRecogEng.LoadGrammar($grammar2)
|
||||
|
||||
write-output "Got $Count voice commands, listening now..."
|
||||
$done = $false
|
||||
do {
|
||||
$recognized = $speechRecogEng.Recognize()
|
||||
write-host -nonewline "."
|
||||
if ($recognized.confidence -lt $MinConfidence) { continue }
|
||||
$myWords = $recognized.text
|
||||
write-host -nonewline "$myWords ($($recognized.confidence) %)"
|
||||
if ($myWords -match "hello") {
|
||||
continue
|
||||
}
|
||||
if ($myWords -match "open notepad") {
|
||||
continue
|
||||
}
|
||||
if ($myWords -match "exit") {
|
||||
break
|
||||
}
|
||||
} until ($done)
|
||||
exit 0
|
||||
} catch {
|
||||
write-error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
||||
exit 1
|
||||
}
|
||||
|
||||
$grammar2 = [System.Speech.Recognition.GrammarBuilder]::new();
|
||||
$grammar2.Append("exit");
|
||||
$speechRecogEng.LoadGrammar($grammar2);
|
||||
|
||||
write-output "Got $Count voice commands, listening now..."
|
||||
$done = $false
|
||||
do {
|
||||
$recognized = $speechRecogEng.Recognize()
|
||||
write-host -nonewline "."
|
||||
if ($recognized.confidence -lt $MinConfidence) { continue }
|
||||
$myWords = $recognized.text
|
||||
write-host -nonewline "$myWords ($($recognized.confidence) %)"
|
||||
if ($myWords -match "hello") {
|
||||
continue
|
||||
}
|
||||
if ($myWords -match "open notepad") {
|
||||
continue
|
||||
}
|
||||
if ($myWords -match "exit") {
|
||||
break
|
||||
}
|
||||
} until ($done)
|
||||
|
||||
exit 0
|
||||
|
Loading…
Reference in New Issue
Block a user