了解如何管理音频输入质量导致的语音识别准确性问题。
重要 API:SpeechRecognizer、RecognitionQualityDegrading、SpeechRecognitionAudioProblem
评估音频输入质量
当语音识别处于活动状态时,请使用语音识别器的 RecognitionQualityDegrading 事件来判断是否有一个或多个音频问题在干扰语音输入。 事件参数(SpeechRecognitionQualityDegradingEventArgs)提供 问题 属性,该属性描述音频输入检测到的问题。
识别可能会受到太多背景噪音、静音麦克风和扬声器音量或速度的影响。
在这里,我们配置语音识别器并开始监听 RecognitionQualityDegrading 事件。
private async void WeatherSearch_Click(object sender, RoutedEventArgs e)
{
// Create an instance of SpeechRecognizer.
var speechRecognizer = new Windows.Media.SpeechRecognition.SpeechRecognizer();
// Listen for audio input issues.
speechRecognizer.RecognitionQualityDegrading += speechRecognizer_RecognitionQualityDegrading;
// Add a web search grammar to the recognizer.
var webSearchGrammar = new Windows.Media.SpeechRecognition.SpeechRecognitionTopicConstraint(Windows.Media.SpeechRecognition.SpeechRecognitionScenario.WebSearch, "webSearch");
speechRecognizer.UIOptions.AudiblePrompt = "Say what you want to search for...";
speechRecognizer.UIOptions.ExampleText = "Ex. 'weather for London'";
speechRecognizer.Constraints.Add(webSearchGrammar);
// Compile the constraint.
await speechRecognizer.CompileConstraintsAsync();
// Start recognition.
Windows.Media.SpeechRecognition.SpeechRecognitionResult speechRecognitionResult = await speechRecognizer.RecognizeWithUIAsync();
//await speechRecognizer.RecognizeWithUIAsync();
// Do something with the recognition result.
var messageDialog = new Windows.UI.Popups.MessageDialog(speechRecognitionResult.Text, "Text spoken");
await messageDialog.ShowAsync();
}
管理语音识别体验
使用 问题 属性提供的说明来帮助用户改善识别条件。
在这里,我们为与检查低音量级别相关的 RecognitionQualityDegrading 事件创建了一个处理程序。 然后,我们使用 SpeechSynthesizer 对象来建议用户尝试说话更响亮。
private async void speechRecognizer_RecognitionQualityDegrading(
Windows.Media.SpeechRecognition.SpeechRecognizer sender,
Windows.Media.SpeechRecognition.SpeechRecognitionQualityDegradingEventArgs args)
{
// Create an instance of a speech synthesis engine (voice).
var speechSynthesizer =
new Windows.Media.SpeechSynthesis.SpeechSynthesizer();
// If input speech is too quiet, prompt the user to speak louder.
if (args.Problem == Windows.Media.SpeechRecognition.SpeechRecognitionAudioProblem.TooQuiet)
{
// Generate the audio stream from plain text.
Windows.Media.SpeechSynthesis.SpeechSynthesisStream stream;
try
{
stream = await speechSynthesizer.SynthesizeTextToStreamAsync("Try speaking louder");
stream.Seek(0);
}
catch (Exception)
{
stream = null;
}
// Send the stream to the MediaElement declared in XAML.
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, () =>
{
this.media.SetSource(stream, stream.ContentType);
});
}
}
相关文章
示例