Question

Après le moyen de faire parler à Powershell.

Add-Type -AssemblyName System.Speech
$synthesizer = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer
$synthesizer.Speak('Hey, I can speak!')

Je voudrais en fait que faire face. Si je parle, peut Powershell convertir en lettres.

Si je dis dans mon enregistreur sonore « Hey, je peux parler », va le convertir en texte?

Si possible s'il vous plaît me guider comment y parvenir?

Était-ce utile?

La solution

On dirait que vous pouvez avec System.Speech.Recognition. Voici l'utilisation même exemple écrit dans PowerShell:

http://huddledmasses.org/control- votre-pc-avec-votre-voix et Powershell /

Ce lien est allé 404 donc je creusais hors de la machine arrière à sens unique.

contrôler votre PC avec votre voix ... et PowerShell

Par Bennett Joel 'Jaykul' le 25 juin 2009

Avez-vous déjà voulu être en mesure de poser vos questions d'ordinateur et faites-vous répondre à haute voix? Avez-vous déjà demandé si votre ordinateur pourrait être plus comme ceux en cours d'exécution de la Star Trek Enterprise, répondre aux questions et vos commandes vocales? Avez-vous joué avec la domotique ou ZWave X10 et je pensais que la commande vocale de vos appareils serait une prochaine étape évidente?

Bon, ok ... Je ne vais pas vous montrer comment allumer les lumières ou le travail avec la domotique - mais c'est la principale chose qui me fait penser à ce genre de choses de reconnaissance vocale. Quel geek ne veut pas marcher dans le salon et dire « Ordinateur: Lights On » et le faire fonctionner

?

Au lieu de cela, dans un premier temps à tout cela, permettez-moi de vous montrer comment utiliser PowerShell pour faire des scripts simples de reconnaissance de commande vocale ... qui peut déclencher un script PowerShell vous soin d'écrire! Le code qui suit est vraiment un module, mais vous pouvez juste la source dot comme un script, et il faut vraiment PowerShell 2.0 pour les événements, bien qu'il serait trivial de le refactoriser au travail sur PowerShell 1.0 en utilisant la bibliothèque PSEventing de Oisin.

$null = [Reflection.Assembly]::LoadWithPartialName("System.Speech")

## Create the two main objects we need for speech recognition and synthesis
if (!$global:SpeechModuleListener) {
    ## For XP's sake, don't create them twice...
    $global:SpeechModuleSpeaker = New-Object System.Speech.Synthesis.SpeechSynthesizer
    $global:SpeechModuleListener = New-Object System.Speech.Recognition.SpeechRecognizer
}

$script:SpeechModuleMacros = @{}
## Add a way to turn it off
$script:SpeechModuleMacros.Add("Stop Listening", {$script:listen = $false; Suspend-Listening})
$script:SpeechModuleComputerName = ${env:ComputerName}

function Update-SpeechCommands {
    #.Synopsis 
    #  Recreate the speech recognition grammar
    #.Description
    #  This parses out the speech module macros, 
    #  and recreates the speech recognition grammar and semantic results, 
    #  and then updates the SpeechRecognizer with the new grammar, 
    #  and makes sure that the ObjectEvent is registered.
    $choices = New-Object System.Speech.Recognition.Choices
    foreach ($choice in $script:SpeechModuleMacros.GetEnumerator()) {
        New-Object System.Speech.Recognition.SemanticResultValue $choice.Key, $choice.Value.ToString() |
            ForEach-Object { $choices.Add($_.ToGrammarBuilder()) }
    }

    if ($VerbosePreference -ne "SilentlyContinue") {
        $script:SpeechModuleMacros.Keys |
            ForEach-Object { Write-Host"$Computer, $_" -Fore Cyan }
    }

    $builder = New-Object System.Speech.Recognition.GrammarBuilder"$Computer, "
    $builder.Append((New-ObjectSystem.Speech.Recognition.SemanticResultKey"Commands", $choices.ToGrammarBuilder()))
    $grammar = New-Object System.Speech.Recognition.Grammar $builder
    $grammar.Name = "Power VoiceMacros"

    ## Take note of the events, but only once (make sure to remove the old one)
    Unregister-Event"SpeechModuleCommandRecognized" -ErrorAction SilentlyContinue
    $null = Register-ObjectEvent $grammar SpeechRecognized `
                -SourceIdentifier"SpeechModuleCommandRecognized" `
                -Action {iex $event.SourceEventArgs.Result.Semantics.Item("Commands").Value}

    $global:SpeechModuleListener.UnloadAllGrammars()
    $global:SpeechModuleListener.LoadGrammarAsync($grammar)
}

function Add-SpeechCommands {
    #.Synopsis
    #  Add one or more commands to the speech-recognition macros, and update the recognition
    #.Parameter CommandText
    #  The string key for the command to remove
    [CmdletBinding()]
    Param([hashtable]$VoiceMacros,[string]$Computer=$Script:SpeechModuleComputerName)

    ## Add the new macros
    $script:SpeechModuleMacros += $VoiceMacros 
    ## Update the default if they change it, so they only have to do that once.
    $script:SpeechModuleComputerName = $Computer 
    Update-SpeechCommands
}

function Remove-SpeechCommands {
    #.Synopsis
    #  Remove one or more command from the speech-recognition macros, and update the recognition
    #.Parameter CommandText
    #  The string key for the command to remove
    Param([string[]]$CommandText)
    foreach ($command in $CommandText) {
        $script:SpeechModuleMacros.Remove($Command)
    }
    Update-SpeechCommands
}

function Clear-SpeechCommands {
    #.Synopsis
    #  Removes all commands from the speech-recognition macros, and update the recognition
    #.Parameter CommandText
    #  The string key for the command to remove
    $script:SpeechModuleMacros = @{}
    ## Default value: A way to turn it off
    $script:SpeechModuleMacros.Add("Stop Listening", {Suspend-Listening})
    Update-SpeechCommands
}

function Start-Listening {
    #.Synopsis
    #  Sets the SpeechRecognizer to Enabled
    $global:SpeechModuleListener.Enabled = $true
    Say "Speech Macros are $($Global:SpeechModuleListener.State)"
    Write-Host "Speech Macros are $($Global:SpeechModuleListener.State)"
}

function Suspend-Listening {
    #.Synopsis
    #  Sets the SpeechRecognizer to Disabled
    $global:SpeechModuleListener.Enabled = $false
    Say "Speech Macros are disabled"
    Write-Host "Speech Macros are disabled"
}

function Out-Speech {
    #.Synopsis
    #  Speaks the input object
    #.Description
    #  Uses the default SpeechSynthesizer settings to speak the string representation of the InputObject
    #.Parameter InputObject
    #  The object to speak 
    #  NOTE: this should almost always be a pre-formatted string,
    #        most objects don't render to very speakable text.
    Param([Parameter(ValueFromPipeline=$true)][Alias("IO")]$InputObject)
    $null = $global:SpeechModuleSpeaker.SpeakAsync(($InputObject | Out-String))
}

function Remove-SpeechXP {
    #.Synopis
    #  Dispose of the SpeechModuleListener and SpeechModuleSpeaker
    $global:SpeechModuleListener.Dispose(); $global:SpeechModuleListener = $null
    $global:SpeechModuleSpeaker.Dispose();  $global:SpeechModuleSpeaker = $null
}

Set-Alias asc Add-SpeechCommands
Set-Alias rsc Remove-SpeechCommands
Set-Alias csc Clear-SpeechCommands
Set-Alias say Out-Speech
Set-Alias listen Start-Listener
Export-ModuleMember -Function * -Alias * -VariableSpeechModuleListener, SpeechModuleSpeaker

Il est fondamentalement juste une fonction que vous devez craindre ici: New-VoiceCommands. Vous passez une table de hachage qui associe des chaînes à scriptblocks, et si vous utilisez le -Listenswitch qui est tout ce qu'il ya à faire. Vous pouvez également callStart-écoute manuellement, et bien sûr, je l'ai fourni la fonction Say pour le rendre plus facile d'avoir l'ordinateur parler ...

Une fois l'ordinateur « écoute » ... vous venez de dire le nom de lui, suivi d'un de vos commandes. Je aime ça parce qu'il assure que je ne lance pas les scripts par accident, mais vous pouvez supprimer la chaîne de ${Env:ComputerName}, depuis le début de la GrammarBuilder si vous pensez qu'il est pas nécessaire, ou vous pouvez coder en dur à autre chose que le nom de votre ordinateur , comme par exemple « Hal, s'il vous plaît, je vous en prie ... » ou « ordinateur, s'il vous plaît » ou quoi?

Vous pouvez faire beaucoup de choses avec ce ... vraiment ... mais pour vous donner un exemple que vous pouvez facilement comprendre, je vais faire quelque chose de très simple, et que mon ordinateur juste répondre à un quelques questions de base en parlant de nouveau à moi, puis ajoutez quelques commandes pour avoir démarrer une application ou une page Web.

Add-SpeechCommands @{
   "What time is it?" = { Say "It is $(Get-Date -f "h:mm tt")" }
   "What day is it?"  = { Say $(Get-Date -f "dddd, MMMM dd") }
   "What's running?"  = {
      $proc = ps | sort ws -desc
      Say $("$($proc.Count) processes, including $($proc[0].name), which is using " +
            "$([int]($proc[0].ws/1mb)) megabytes of memory")
   }
} -Computer "Laptop" -Verbose 

Add-SpeechCommands @{
    "Run Notepad"= { & "C:\Programs\DevTools\Notepad++\notepad++.exe"}
}
Add-SpeechCommands @{
    "Check Gee Mail" = { Start-Process"https://mail.google.com" }
}

Vous voyez la facilité qui est? Vous pouvez utiliser « Say » pour parler de texte (bien que someText obtiendra de meilleurs résultats que d'autres), et vous pouvez appeler toutes les autres commandes PowerShell, y compris les commandes HttpRest pour extraire des données Web ou les commandes WASP pour l'automatisation des fenêtres, ou des commandes de PowerBoots à afficher sortie dans un grand texte ou cmdlets pour contrôler les dispositifs X10 ou ... vous savez Z-Wave, quoi que ce soit?

Autres conseils

La reconnaissance vocale est toujours la technologie d'une expérimentation. Il y a quelques framework .Net ressources , même avec un exemple . Ne vous attendez pas à créer un moment PowerShiri bientôt, cependant.

La technologie est un peu passé « expérimental », mais il est loin d'être fiable.

Bourse le fait maintenant avec l'option « Messagerie vocale Preview » de messagerie unifiée. Les résultats peuvent varier d'une assez bonne à hilarante, selon le haut-parleur.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top