如何在 Windows 10 UWP 中正确实现语音识别
How to correctly implement Speech Recognition in Windows 10 UWP
到目前为止,我对在 Microsoft 网站上找到的语音识别示例一无所知。我也查看了这个网站 - https://mtaulty.com/2016/02/08/text-to-speech-and-more-with-windows-10-uwp-project-oxford/,我尝试使用给出的示例,但它仍然不起作用。正在发生的事情是 SpeechRecognitionConfidence 被拒绝(它没有接收到我说的任何内容)。在你问之前,是的,我有一个工作的麦克风,并且在设置中启用了所有内容。
这里有什么我遗漏的简单的东西吗?
如果您不太理解我的问题,请滚动到我在上面链接的页面底部,用户 nhwilly1011 遇到了与我相同的问题。
async void Button_Click_2(object sender, RoutedEventArgs e)
{
this.recognizer = new SpeechRecognizer();
await this.recognizer.CompileConstraintsAsync();
this.recognizer.Timeouts.InitialSilenceTimeout = TimeSpan.FromSeconds(5);
this.recognizer.Timeouts.EndSilenceTimeout = TimeSpan.FromSeconds(20);
this.recognizer.UIOptions.AudiblePrompt = "Say whatever you like, I'm listening";
this.recognizer.UIOptions.ExampleText = "The quick brown fox jumps over the lazy dog";
this.recognizer.UIOptions.ShowConfirmation = true;
this.recognizer.UIOptions.IsReadBackEnabled = true;
this.recognizer.Timeouts.BabbleTimeout = TimeSpan.FromSeconds(5);
var result = await this.recognizer.RecognizeWithUIAsync();
if (result != null)
{
StringBuilder builder = new StringBuilder();
builder.AppendLine(
$"I have {result.Confidence} confidence that you said [{result.Text}] " +
$"and it took {result.PhraseDuration.TotalSeconds} seconds to say it " +
$"starting at {result.PhraseStartTime:g}");
var alternates = result.GetAlternates(10);
builder.AppendLine(
$"There were {alternates?.Count} alternates - listed below (if any)");
if (alternates != null)
{
foreach (var alternate in alternates)
{
builder.AppendLine(
$"Alternate {alternate.Confidence} confident you said [{alternate.Text}]");
}
}
this.txtResults.Text = builder.ToString();
}
}
SpeechRecognizer recognizer;
微软的例子我也试过了,也不行--
private async void Button_Click_1(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();
}
如果我遗漏了什么,请纠正我,但在调用 CompileConstraintsAsync
之前,建议将 SpeechRecognitionTopicConstraint
添加到 SpeechRecognizer
的 Constraints 集合中。
这是我发现的一个有用的演练,here。
我找到答案了。我的计算机未启用 Cortana,因此我最初没有收到错误消息。在使用装有 Cortana 的计算机后,我发现我使用的网络存在问题。切换网络后,一切都按预期工作。我的错误是语音识别错误:"Can't access the network" 并通过切换到不安全的 WiFi 连接得到修复。
HERE 你可能会从 Windows.Media.SpeechRecognition
找到唤醒词语音识别
public MAINPage()
{
this.InitializeComponent();
this.Loaded += OnLoaded;
}
Windows.Media.SpeechRecognition.SpeechRecognizer recognizer;
public async void OnLoaded(object sender, RoutedEventArgs args)
{
this.recognizer = new SpeechRecognizer();
var commands = new Dictionary<string, int>()
{
[Constants.WAKEUP_INIT_WORD] = -90, //define your start word
[Constants.WAKEUP_STOP_WORD] = 90 //define your stop word
};
this.recognizer.Constraints.Add(new SpeechRecognitionListConstraint(
commands.Keys));
await this.recognizer.CompileConstraintsAsync();
this.recognizer.ContinuousRecognitionSession.ResultGenerated +=
async (s, e) =>
{
if ((e.Result != null) && (commands.ContainsKey(e.Result.Text)))
{
await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
//e.Result.Confidence
double confidence = e.Result.RawConfidence;
if (e.Result.Confidence == SpeechRecognitionConfidence.Medium || e.Result.Confidence == SpeechRecognitionConfidence.High || confidence >0.85)
{
if (e.Result.Text == Constants.WAKEUP_INIT_WORD)
{
recordButton_Click(sender, args);
stopRecordButton.IsEnabled = true;
recordButton.IsEnabled = false;
}
if (e.Result.Text == Constants.WAKEUP_STOP_WORD)
{
stopRecordButton_Click(sender, args);
recordButton.IsEnabled = true;
stopRecordButton.IsEnabled = false;
}
}
}
);
this.recognizer.ContinuousRecognitionSession.Resume();
}
};
await this.recognizer.ContinuousRecognitionSession.StartAsync(
SpeechContinuousRecognitionMode.PauseOnRecognition);
}
enter code here
更多品种在这里https://mtaulty.com/2016/02/08/text-to-speech-and-more-with-windows-10-uwp-project-oxford/
到目前为止,我对在 Microsoft 网站上找到的语音识别示例一无所知。我也查看了这个网站 - https://mtaulty.com/2016/02/08/text-to-speech-and-more-with-windows-10-uwp-project-oxford/,我尝试使用给出的示例,但它仍然不起作用。正在发生的事情是 SpeechRecognitionConfidence 被拒绝(它没有接收到我说的任何内容)。在你问之前,是的,我有一个工作的麦克风,并且在设置中启用了所有内容。
这里有什么我遗漏的简单的东西吗?
如果您不太理解我的问题,请滚动到我在上面链接的页面底部,用户 nhwilly1011 遇到了与我相同的问题。
async void Button_Click_2(object sender, RoutedEventArgs e)
{
this.recognizer = new SpeechRecognizer();
await this.recognizer.CompileConstraintsAsync();
this.recognizer.Timeouts.InitialSilenceTimeout = TimeSpan.FromSeconds(5);
this.recognizer.Timeouts.EndSilenceTimeout = TimeSpan.FromSeconds(20);
this.recognizer.UIOptions.AudiblePrompt = "Say whatever you like, I'm listening";
this.recognizer.UIOptions.ExampleText = "The quick brown fox jumps over the lazy dog";
this.recognizer.UIOptions.ShowConfirmation = true;
this.recognizer.UIOptions.IsReadBackEnabled = true;
this.recognizer.Timeouts.BabbleTimeout = TimeSpan.FromSeconds(5);
var result = await this.recognizer.RecognizeWithUIAsync();
if (result != null)
{
StringBuilder builder = new StringBuilder();
builder.AppendLine(
$"I have {result.Confidence} confidence that you said [{result.Text}] " +
$"and it took {result.PhraseDuration.TotalSeconds} seconds to say it " +
$"starting at {result.PhraseStartTime:g}");
var alternates = result.GetAlternates(10);
builder.AppendLine(
$"There were {alternates?.Count} alternates - listed below (if any)");
if (alternates != null)
{
foreach (var alternate in alternates)
{
builder.AppendLine(
$"Alternate {alternate.Confidence} confident you said [{alternate.Text}]");
}
}
this.txtResults.Text = builder.ToString();
}
}
SpeechRecognizer recognizer;
微软的例子我也试过了,也不行--
private async void Button_Click_1(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();
}
如果我遗漏了什么,请纠正我,但在调用 CompileConstraintsAsync
之前,建议将 SpeechRecognitionTopicConstraint
添加到 SpeechRecognizer
的 Constraints 集合中。
这是我发现的一个有用的演练,here。
我找到答案了。我的计算机未启用 Cortana,因此我最初没有收到错误消息。在使用装有 Cortana 的计算机后,我发现我使用的网络存在问题。切换网络后,一切都按预期工作。我的错误是语音识别错误:"Can't access the network" 并通过切换到不安全的 WiFi 连接得到修复。
HERE 你可能会从 Windows.Media.SpeechRecognition
找到唤醒词语音识别 public MAINPage()
{
this.InitializeComponent();
this.Loaded += OnLoaded;
}
Windows.Media.SpeechRecognition.SpeechRecognizer recognizer;
public async void OnLoaded(object sender, RoutedEventArgs args)
{
this.recognizer = new SpeechRecognizer();
var commands = new Dictionary<string, int>()
{
[Constants.WAKEUP_INIT_WORD] = -90, //define your start word
[Constants.WAKEUP_STOP_WORD] = 90 //define your stop word
};
this.recognizer.Constraints.Add(new SpeechRecognitionListConstraint(
commands.Keys));
await this.recognizer.CompileConstraintsAsync();
this.recognizer.ContinuousRecognitionSession.ResultGenerated +=
async (s, e) =>
{
if ((e.Result != null) && (commands.ContainsKey(e.Result.Text)))
{
await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
//e.Result.Confidence
double confidence = e.Result.RawConfidence;
if (e.Result.Confidence == SpeechRecognitionConfidence.Medium || e.Result.Confidence == SpeechRecognitionConfidence.High || confidence >0.85)
{
if (e.Result.Text == Constants.WAKEUP_INIT_WORD)
{
recordButton_Click(sender, args);
stopRecordButton.IsEnabled = true;
recordButton.IsEnabled = false;
}
if (e.Result.Text == Constants.WAKEUP_STOP_WORD)
{
stopRecordButton_Click(sender, args);
recordButton.IsEnabled = true;
stopRecordButton.IsEnabled = false;
}
}
}
);
this.recognizer.ContinuousRecognitionSession.Resume();
}
};
await this.recognizer.ContinuousRecognitionSession.StartAsync(
SpeechContinuousRecognitionMode.PauseOnRecognition);
}
enter code here
更多品种在这里https://mtaulty.com/2016/02/08/text-to-speech-and-more-with-windows-10-uwp-project-oxford/