MS SpeechRecognitionEngine不返回结果

时间:2014-12-03 17:16:51

标签: .net powershell speech-recognition sapi

我正在尝试使用Powershell进行一些简单的语音识别(来自.wav文件)。我正在使用Microsoft.Speech.Recognition.SpeechRecognitionEngine。可悲的是,我有一些严重的问题,但首先是我的代码:

[System.Reflection.Assembly]::LoadFrom("C:\Program Files\Microsoft SDKs\Speech\v11.0\Assembly\Microsoft.Speech.dll")
[System.Reflection.Assembly]::LoadWithPartialName("System.Speech")


$cult = New-Object System.Globalization.CultureInfo("en-US")

$listener = New-Object Microsoft.Speech.Recognition.SpeechRecognitionEngine($cult)
$listener.SetInputToWaveFile("C:\Users\user\Downloads\audio.wav")

$arr = @("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q" ,"r", "s", "t", "u","v","w","x","y","z","four","red")
$text = New-Object Microsoft.Speech.Recognition.Choices
$text.Add($arr)
$toGram = New-Object Microsoft.Speech.Recognition.GrammarBuilder($text)
$toGram.Culture = $cult
$gram = New-Object Microsoft.Speech.Recognition.Grammar($toGram)
$listener.LoadGrammar($gram)

Register-ObjectEvent $listener RecognizeCompleted -SourceIdentifier "RecognizeCompleted" -Action {if($EventArgs){$EventArgs.Result.Text; write-host $EventArgs.Result.Confidence} else {write-host "nope"} }
$listener.RecognizeAsync()

我的问题是,当我使用.Recognize()时,我根本没有输出,甚至没有输出0结果。 注册完成Async方法(.RecognizeAsync())时,会调用Handler并且$EventArgs确实存在,但我无法访问变量的任何属性,甚至无法从Get-Member获取输出。

我在这里做了一些明显错误的事吗?我很感激任何意见,因为我现在有点生气......

我也愿意接受MS Speech API的任何替代方案(任何能用英语进行基本语音识别的命令行工具都可以。)

更新: wave文件包含一系列字母或数字。例如“3 D 6 H Y”

更新:我感谢编辑,但我不欣赏有人删除代码!谢谢!不要这样做!

更新:似乎SAPI不能很好地处理单个字符(如果无论如何)。我可能接下来会尝试狮身人面像。非常感谢brandon投入这么多时间来帮助我。

1 个答案:

答案 0 :(得分:2)

这是我删除的评论,因为它是答案的一部分:

Recognize()正在阻止。它执行一次单一识别操作,每次调用您现在拥有的方式。我没有任何关于Powershell的经验,所以如果我错了,请纠正我,但看起来你已经调用了这个函数或程序或脚本等...每次你想要识别。

基本上:如果它听到" A"那就是它;您必须再次致电Recognize以获得" B"。尝试使用麦克风(SetInputToDefaultAudioDevice)。最后,Recognize[Async]()会引发SpeechRecognized事件,您可以在其中检索结果,而这看起来并不像您处理的那样。

您可能希望改为调用RecognizeAsync,因此引擎可以在同一操作中处理多个语音文本。然而,它可以通过两种方式完成。

同样,因为我不熟悉Powershell,所以这里有一些伪/ c#代码可以让你走上正轨:

Recognize()方法:

function InitializeRecognizer
    setup your recognizer and audio input, .wav file etc.
    add the handler for the SpeechRecognized event.
    call the Recognize method

function SpeechRecognizedHandler
    read the EventArgs data to get the speech element
    do your output or logic
    if we want to listen to some more stuff
        call Recognize() again

RecognizeAsync()方法:

function InitializeRecognizer
    setup your recognizer and audio input, .wav file etc.
    add the handler for the SpeechRecognized event.
    call the RecognizeAsync() method

function SpeechRecognizedHandler
    read the EventArgs data to get the speech element
    do your output or logic
    (Note: you may have to call RecognizeAsyncCancel()
       or something similar here if you run into issues 
       where it's recognizing stuff in a weird order)

这是指向RecognizeAsync() MSDN文档的链接,该文档将向您显示Recognize家庭提出的事件。

http://msdn.microsoft.com/en-us/library/system.speech.recognition.speechrecognitionengine.recognizeasync%28v=vs.110%29.aspx