.net MS SpeechRecognitionEngine未返回结果

.net MS SpeechRecognitionEngine未返回结果,.net,powershell,speech-recognition,sapi,.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.Ref

我正在尝试使用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的输出。 注册完成异步方法(
.RecognizeAsync()
)时,将调用处理程序,
$EventArgs
确实存在,但我无法访问变量的任何属性,甚至无法从
获取成员
获取输出

我是不是做错了什么?我会很感激任何意见,因为我现在有点发疯了

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

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

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


更新:SAPI似乎不能很好地处理单个字符(如果有的话)。下一步我可能会试试斯芬克斯。感谢布兰登花了这么多时间来帮助我。

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

Recognize()
正在阻塞。它会以你现在的方式进行一次识别动作。我没有任何使用Powershell的经验,所以如果我错了,请纠正我,但看起来您应该调用该函数、过程或脚本等。。。每次你想要得到认可

基本上:如果它听到“A”,就是它;您必须再次调用
Recognize
以获得“B”。用麦克风(
setInputOdeFaultAudioDevice
)试试。最后,
recognized[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
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()
方法:

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
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
系列引发的事件


要使识别引擎能够识别这些字符中的任何一个,则.wav文件只能是这些“单词”中的一个。因此,如果您的.wav文件不是单个字符(从“a”到“Z”)或单词“red”或“four”,那么它将无法识别任何内容。简而言之:你的wav文件里有什么?哦,很抱歉,我没有看到上面的评论。等等,评论我添加了一个答案来总结所有内容,生成一个字母表数组。感谢您的详细解释,特别是关于
recognize()
(没想到它会这样工作)。我将尝试处理
speechrecogned
事件并报告!酷。让我知道你是如何得到onOk的,我现在已经用
SpeechRecognized
的处理程序测试过了,但是还没有成功。实际上,我现在从
RecognizeCompleted
处理程序的EventArgs中获得了一些输出(结果字段为空),因此这可能只是因为它不识别任何内容。。。如果有人有兴趣尝试,我已经上传了我正在测试的一个wav文件:。你有没有其他建议让我试着让它认识到这一点?文件中似乎没有太多噪音,所以我很失望它没有识别任何东西。它在调用您的
SpeechRecogened
处理程序吗?您试过使用麦克风吗?只试过使用麦克风,但它无法识别任何内容,
RecognizeCompleted
处理程序在大约5-6秒内被调用,错误和结果字段为空,取消=False