Improved voice-control.ps1

This commit is contained in:
Markus Fleschutz 2021-01-09 12:21:23 +01:00
parent 0609414148
commit f70b65d3e2

View File

@ -6,17 +6,17 @@
.NOTES Author: Markus Fleschutz / License: CC0
#>
[double]$MinConfidence = 0.05
[double]$MinConfidence = 0.04
$PathToRepo = "$PSScriptRoot/.."
try {
write-output "Initializing the speech recognition engine..."
write-output "Init 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..."
write-output "Learn script files..."
$Items = get-childItem -path "$PathToRepo/Scripts/"
$Count = 0
foreach ($Item in $Items) {
@ -28,28 +28,26 @@ try {
$Count++
}
write-output "Learn internal command (exit)..."
$grammar2 = [System.Speech.Recognition.GrammarBuilder]::new()
$grammar2.Append("exit")
$speechRecogEng.LoadGrammar($grammar2)
write-output "Got $Count voice commands, listening now..."
$done = $false
do {
write-output "Listening now ($Count voice commands)..."
while ($true) {
$recognized = $speechRecogEng.Recognize()
write-host -nonewline "."
if ($recognized.confidence -lt $MinConfidence) { continue }
if ($recognized.confidence -lt $MinConfidence) { write-host -noNewline "?"; 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") {
foreach ($Item in $Items) {
$Name = $Item.Name.Split('.')[0]
if ($Name -eq $myWords) {
write-host "$Name ($($recognized.confidence) %)..."
& "./$Item"
break
}
} until ($done)
}
if ($myWords -match "exit") { write-host -noNewline "$Name ($($recognized.confidence) %)"; break }
}
exit 0
} catch {
write-error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"