學習如何管理因音訊輸入品質而導致語音辨識準確度的問題。
評估音訊輸入品質
當語音辨識啟動時,利用語音辨識器的 「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);
});
}
}
相關文章
範例