如何在Windows 10 UWP中正确实现语音识别

4
到目前为止,我在微软的网站上找到的语音识别例子都没有成功。我也看过这个网站 - 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();
    }

1
请编辑您的问题并提供错误的详细信息。mtaulty.com的链接可能在几年后失效,因此最好将详细信息放在一个地方(即stackoverflow)中。 - kennyzx
当用户被UI提示说话(我说了些什么),但它无法识别我所说的内容时,它会认为我已经说了些什么,但默认输出为空字符串“”。 - Quinn Stabenow
@kennyzx,我已经添加了代码-感谢您的推荐。 - Quinn Stabenow
3个回答

3

我找到了答案。我的电脑没有启用Cortana,因此一开始我没有收到错误消息。在使用了一个拥有Cortana的电脑之后,我发现我使用的网络存在问题。切换了网络后,一切正常。我的错误是"语音识别出错:无法访问网络",通过切换到非安全的WiFi连接来解决了这个问题。


1

如果我错了,请纠正我,但在调用CompileConstraintsAsync之前,建议将SpeechRecognitionTopicConstraint添加到SpeechRecognizer的约束集合中。这里有一个很有帮助的教程这里


我包含的示例没有SpeechRecognitionTopicConstraint,但我也尝试过微软网站上带有该约束条件的示例 - 仍然不起作用。 https://learn.microsoft.com/en-us/windows/uwp/input-and-devices/speech-recognition(第三个示例) - Quinn Stabenow
你是否遇到了与被识别的内容相同的问题,还是出现了其他错误? - Priyank
两者都有同样的问题——它们都无法识别说了什么,但它们可以识别出“有东西”被说了。 - Quinn Stabenow

1

在这里,您可以找到来自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/


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接