14

以下是让powershell发言的方法。Powershell可以说,但如果我说话可以写吗?

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

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

如果我在我的录音机中说“嘿,我可以说”,它会转换成文本吗?

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

+0

不知道它是否可以听,但感谢展示它如何说话,upvoted – 2012-02-20 13:30:18

+0

请注意,这是两个退出不同的问题。 – Joey 2012-02-20 13:43:53

回答

9

看起来你可以用System.Speech.Recognition。这是写在PowerShell中甚至使用例子:

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

这个链接去404,所以我挖了出来的方式退机。

控制你的声音你的电脑...和PowerShell

由Joel“Jaykul”贝内特25军2009年

你有没有想能问您的计算机问题,并把它回答你你有没有想过如果你的电脑可能更像是运行星际迷航企业,响应语音查询和命令?你玩过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-objectSystem.Speech.Synthesis.SpeechSynthesizer 
   $Global:SpeechModuleListener =new-objectSystem.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-objectSystem.Speech.Recognition.Choices 
   foreach($choice in$Script:SpeechModuleMacros.GetEnumerator()){ 
      New-ObjectSystem.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-ObjectSystem.Speech.Recognition.GrammarBuilder"$Computer, " 
   $builder.Append((New-ObjectSystem.Speech.Recognition.SemanticResultKey"Commands",  
                                                         $choices.ToGrammarBuilder())) 
   $grammar = new-objectSystem.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。你把它传递给一个散列表,它将字符串映射到scriptblocks,如果你使用-Listenswitch就是它的全部。您也可以手动调用Start-Listening,当然,我提供了Say功能以使电脑更容易说话...

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

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

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命令,包括HttpRest命令来获取Web数据,或者Windows自动化的WASP命令,或PowerBoots命令来显示以大文本输出,或cmdlet来控制X10或ZWave设备......你知道,任何东西

2

语音识别仍然是实验性的一项技术。有一些.Net框架resources,即使是example。不过,不要指望很快就会创建一个PowerShiri。

1

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

Exchange使用UM的“语音邮件预览”选项进行此操作。根据演讲者的不同,结果可能会有所不同,从相当不错。

相关问题