我正在尝试在Visual C#中在Windows 10(使用Cortana)中构建语音识别。 这是我使用旧System.Speech.Recognition进行语音识别的代码的一部分,效果很好,但它只支持英语。
obj.owner == request.user
如何在Windows 10中使用新的语音识别来实现?
答案 0 :(得分:2)
使用Microsoft Speech Platform SDK v11.0( Microsoft.Speech.Recognition )。
它的工作方式与System.Speech类似,但您可以使用意大利语(separeted install)并使用SRGS Grammar。我无需麻烦地使用kinect( SetInputToAudioStream )和默认输入设备( SetInputToDefaultAudioDevice )。
它也可以离线工作,因此无需像Cortana那样在线。 使用SRGS语法,您可以为命令获得相当高的复杂度
<强>更新强> 以下是我初始化识别器的方法
private RecognizerInfo GetRecognizer(string culture, string recognizerId)
{
try
{
foreach (var recognizer in SpeechRecognitionEngine.InstalledRecognizers())
{
if (!culture.Equals(recognizer.Culture.Name, StringComparison.OrdinalIgnoreCase)) continue;
if (!string.IsNullOrWhiteSpace(recognizerId))
{
string value;
recognizer.AdditionalInfo.TryGetValue(recognizerId, out value);
if ("true".Equals(value, StringComparison.OrdinalIgnoreCase))
return recognizer;
}
else
return recognizer;
}
}
catch (Exception e)
{
log.Error(m => m("Recognizer not found"), e);
}
return null;
}
private void InitializeSpeechRecognizer(string culture, string recognizerId, Func<Stream> audioStream)
{
log.Debug(x => x("Initializing SpeechRecognizer..."));
try
{
var recognizerInfo = GetRecognizer(culture, recognizerId);
if (recognizerInfo != null)
{
recognizer = new SpeechRecognitionEngine(recognizerInfo.Id);
//recognizer.LoadGrammar(VoiceCommands.GetCommandsGrammar(recognizerInfo.Culture));
recognizer.LoadGrammar(grammar);
recognizer.SpeechRecognized += SpeechRecognized;
recognizer.SpeechRecognitionRejected += SpeechRejected;
if (audioStream == null)
{
log.Debug(x => x("...input on DefaultAudioDevice"));
recognizer.SetInputToDefaultAudioDevice();
}
else
{
log.Debug(x => x("SpeechRecognizer input on CustomAudioStream"));
recognizer.SetInputToAudioStream(audioStream(), new SpeechAudioFormatInfo(EncodingFormat.Pcm, 16000, 16, 1, 32000, 2, null));
}
}
else
{
log.Error(x => x(Properties.Resources.SpeechRecognizerNotFound, recognizerId));
throw new Exception(string.Format(Properties.Resources.SpeechRecognizerNotFound, recognizerId));
}
log.Debug(x => x("...complete"));
}
catch (Exception e)
{
log.Error(m => m("Error while initializing SpeechEngine"), e);
throw;
}
}
答案 1 :(得分:0)
Cortana API使用示例为here。您可以复制它并根据您的需要开始修改。它会与用户创建一个对话框。您无法使用Cortana API完全重现System.Speech代码,因为它是为其他目的而设计的。如果您仍然只想识别几个字,可以继续使用System.Speech API。
System.Speech API支持其他语言,而不仅仅是英语。您可以在此处找到更多信息: