20

下面是让powershell说话的方法。

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

其实我想做相反的事情。如果我说话,powershell可以将其转换为字母。

如果我在录音机中说“嘿,我会说话”,它会转换成文字吗?

如果可能,请指导我如何实现它?

4

3 回答 3

10

看起来你可以用System.Speech.Recognition. 这里甚至是用 PowerShell 编写的示例用法:

http://huddledmasses.org/control-your-pc-with-your-voice-and-powershell/

这个链接变成了 404,所以我把它从机器后面挖了出来。

用你的声音控制你的电脑……和 PowerShell

作者:Joel 'Jaykul' Bennett 于 2009 年 6 月 25 日

你有没有想过能够问你的电脑问题并让它大声回答你?你有没有想过你的电脑是否可以更像运行星际迷航企业版的电脑,响应语音查询和命令?您是否玩过 ZWave 或 X10 家庭自动化,并认为您的设备的语音控制将是一个明显的下一步?

好吧,好吧……我不会向你展示如何打开灯或使用家庭自动化——但这是让我不断思考语音识别的主要内容。哪个极客不想走进客厅并说“计算机:灯亮”并让它工作?

相反,作为所有这些的第一步,让我向您展示如何使用 PowerShell 执行简单的语音命令识别脚本……它可以触发您想要编写的任何 PowerShell 脚本!下面的代码实际上是一个模块,尽管您可以将其作为脚本进行点源,并且它确实需要 PowerShell 2.0 来处理事件,尽管使用 Oisin 的 PSEventing 库重构它以在 PowerShell 1.0 上工作是微不足道的。

$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

这里基本上只需要担心一个功能:New-VoiceCommands。您将一个哈希表传递给它,该哈希表将字符串映射到脚本块,如果您使用 -Listenswitch,这就是它的全部内容。你也可以手动调用Start-Listening,当然,我提供了Say功能,让电脑更容易说话……

一旦计算机“听”...您只需说出它的名称,然后是您的命令之一。我喜欢这样,因为它确保我不会意外运行脚本,但是${Env:ComputerName},如果您认为没有必要,您可以从 GrammarBuilder 的开头删除字符串,或者您可以将其硬编码为计算机名称以外的其他名称,比如说“哈尔,拜托,我求求你……”或“电脑,拜托”之类的?

你可以用这个做很多事情......任何事情,真的......但是给你一个你可以很容易理解的例子,我会做一些非常简单的事情,让我的电脑只回答几个基本问​​题回复我,然后添加一些命令让它启动应用程序或网页。

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" }
}

你知道这有多容易吗?您可以使用“说”来说出任何文本(尽管某些文本会比其他文本获得更好的结果),并且您可以调用任何其他 powershell 命令,包括获取 Web 数据的 HttpRest 命令,或用于 Windows 自动化的 WASP 命令,或用于显示的 PowerBoots 命令以大文本输出,或用于控制 X10 或 ZWave 设备的 cmdlet……你知道吗?

于 2012-02-20T14:24:33.853 回答
2

语音识别仍然是一项实验性技术。有一些 .Net 框架资源,甚至还有一个示例。不过,不要指望很快就能创建 PowerShiri。

于 2012-02-20T13:42:19.657 回答
1

该技术有点过去“实验性”,但它远非可靠。

Exchange 现在使用 UM 的“语音邮件预览”选项执行此操作。根据演讲者的不同,结果可能从非常好到搞笑不等。

于 2012-02-20T17:09:52.997 回答