我有一个项目要获得认可。它可以工作,但是如果我使用这个项目如何使用一个类并从其他类调用它的方法,我会遇到在线异常问题:
sre = new SpeechRecognitionEngine(ri.Id);
错误是:
No recognizer of the required ID found.
代码:
KinectAudioSource source = kinectSensor.AudioSource;
source.EchoCancellationMode = EchoCancellationMode.None; // No AEC for this sample
source.AutomaticGainControlEnabled = false; // Important to turn this off for speech recognition
// source.SystemMode = SystemMode.OptibeamArrayOnly;
speechRecognizer = CreateSpeechRecognizer();
using (Stream s = source.Start())
{
speechRecognizer.SetInputToAudioStream(s, new SpeechAudioFormatInfo(EncodingFormat.Pcm, 16000, 16, 1, 32000, 2, null));
Console.WriteLine("Recognizing speech. Say: 'purple', 'green' or 'blue'. Press ENTER to stop");
speechRecognizer.RecognizeAsync(RecognizeMode.Multiple);
Console.ReadLine();
Console.WriteLine("Stopping recognizer ...");
speechRecognizer.RecognizeAsyncStop();
}
private static SpeechRecognitionEngine CreateSpeechRecognizer()
{
RecognizerInfo ri = GetKinectRecognizer();
SpeechRecognitionEngine sre;
//if (ri == null) return 0;
sre = new SpeechRecognitionEngine(ri.Id);
var colors = new Choices();
colors.Add("red");
colors.Add("green");
colors.Add("blue");
var gb = new GrammarBuilder { Culture = ri.Culture };
gb.Append(colors);
// Create the actual Grammar instance, and then load it into the speech recognizer.
var g = new Grammar(gb);
sre.LoadGrammar(g);
sre.SpeechRecognized += SreSpeechRecognized;
sre.SpeechHypothesized += SreSpeechHypothesized;
sre.SpeechRecognitionRejected += SreSpeechRecognitionRejected;
return sre;
}
private static RecognizerInfo GetKinectRecognizer()
{
Func<RecognizerInfo, bool> matchingFunc = r =>
{
string value;
r.AdditionalInfo.TryGetValue("Kinect", out value);
return "True".Equals(value, StringComparison.InvariantCultureIgnoreCase) && "en-US".Equals(r.Culture.Name, StringComparison.InvariantCultureIgnoreCase);
};
return SpeechRecognitionEngine.InstalledRecognizers().Where(matchingFunc).FirstOrDefault();
}
最佳答案
我认为您的 GetKinectRecognizer() 方法不正确。
如果找到并且找到的值作为输出参数,TryGetValue() 不会返回 bool 值吗?您没有对 TryGetvalue() 返回的 bool 值做任何事情。
您是否希望 AdditionalInfo 字典具有等于“Kinect”的键和字符串“True”或“False”值?这就是您的代码似乎要寻找的内容。
此代码是否基于您可以指出的示例。我不太了解您在 matchingFunc 中进行的测试。您忽略了 TryGetvalue 的返回值,您正在寻找一个名为“Kinect”且字符串值为“True”的 AdditionalInfo 键,以及一个文化为“en-US”的识别器。
为什么不转储 SpeechRecognitionEngine.InstalledRecognizers() 的内容并确保它包含您认为它包含的内容。这是老派,但很有用:
foreach (RecognizerInfo ri in SpeechRecognitionEngine.InstalledRecognizers())
{
Debug.WriteLine(String.Format("Id={0}, Name={1}, Description={2}, Culture={3}", ri.Id, ri.Name, ri.Description, ri.Culture));
foreach(string key in ri.AdditionalInfo.Keys)
{
Debug.WriteLine(string.Format("{0} = {1}", key, ri.AdditionalInfo[key]));
}
Debug.WriteLine("-");
}
我没有安装 Kinect SDK,但在我的 Windows 7 机器上它显示:
Id=MS-1033-80-DESK, Name=MS-1033-80-DESK, Description=Microsoft Speech Recognizer 8.0 for Windows (English - US), Culture=en-US
VendorPreferred =
CommandAndControl =
Version = 8.0
Language = 409;9
Desktop =
SupportedLocales = 409;1009;3409;9
AudioFormats = 16;18;20;22;45;53;{6F50E21C-E30E-4B50-95E9-21E8F23D15BD}
SpeakingStyle = Discrete;Continuous
WildcardInCFG = Anywhere;Trailing
Dictation =
Hypotheses =
Alternates = CC;Dictation
windowsV6compatible =
Name = MS-1033-80-DESK
DictationInCFG = Anywhere;Trailing
UPSPhoneSet =
WordSequences = Anywhere;Trailing
Vendor = Microsoft
-
Id=MS-2057-80-DESK, Name=MS-2057-80-DESK, Description=Microsoft Speech Recognizer 8.0 for Windows (English - UK), Culture=en-GB
=
VendorPreferred =
CommandAndControl =
Version = 8.0
Language = 809
Desktop =
SupportedLocales = 809;C09;1409;1809;1C09;2009;2409;2809;2C09;3009;4009;4409;4809;9
AudioFormats = 16;18;20;22;45;53;{6F50E21C-E30E-4B50-95E9-21E8F23D15BD}
SpeakingStyle = Discrete;Continuous
WildcardInCFG = Anywhere;Trailing
Dictation =
Hypotheses =
Alternates = CC;Dictation
windowsV6compatible =
Name = MS-2057-80-DESK
DictationInCFG = Anywhere;Trailing
UPSPhoneSet =
WordSequences = Anywhere;Trailing
Vendor = Microsoft
-
-
确保您在 AdditionalInfo 字典中查找的值确实存在。然后制作您的 matchingFunc 来检查它。
关于c# - Kinect 语音识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10613559/