【问题标题】:Speech Recognition using Microsoft.Speech or System.Speech Namespace使用 Microsoft.Speech 或 System.Speech 命名空间的语音识别
【发布时间】:2015-03-30 07:29:15
【问题描述】:

我正在尝试在 C# 桌面应用程序中使用语音识别,如果用户说“按 3”或“按 4”,那么该数字应该写在图表上(比如标签)。我能够识别出用户说出的单词“press”,但除此之外。请帮忙。以下是我的示例代码:

    string txtSpoken = "";
    string[] words = new string[10];

    public Form1()
    {
        InitializeComponent();

        SpeechRecognitionEngine _recognizer = new SpeechRecognitionEngine();
        _recognizer.LoadGrammar(new Grammar(new GrammarBuilder("press")) { Name = "pressGrammar" }); // load a grammar
        _recognizer.SpeechRecognized += _recognizer_SpeechRecognized;
        _recognizer.SetInputToDefaultAudioDevice(); // set the input of the speech recognizer to the default audio device
        _recognizer.RecognizeAsync(RecognizeMode.Multiple); // recognize speech asynchronous
        // _recognizer.Recognize();
    }

    void _recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
        string txt = e.Result.Text;
        this.Invoke(new MethodInvoker(() =>
        {
            listBox1.Items.Add("I heard you say: "
                + txt);
        })); // WinForm specific

        if (e.Result.Text == "press") // e.Result.Text contains the recognized text
        {
            textBox1.Text = "3";
            label1.Text += " 3 ";
            //   MessageBox.Show("The test was successful!");
        }

        txtSpoken += e.Result.Text;
        MessageBox.Show(txtSpoken);


        if (txt.IndexOf("press") >= 0)
        {
            words = txt.Split(' ');
        }
    }

【问题讨论】:

  • txt的内容是什么?

标签: c#


【解决方案1】:

问题是,您的语法仅包含单词 press。为了使用户语音和语法之间发生匹配,用户必须准确说出您的语法中的元素之一。 我建议你创建Choices,例如这样:

Choices inputs = new Choices();
inputs.Add(new string[] {"press 3", "press 4"});

GrammarBuilder gb = new GrammarBuilder();
gb.Append(inputs);
Grammar g = new Grammar(gb);
_recognizer.LoadGrammar(g);

更多信息请关注this tutorial

【讨论】:

  • 感谢您的建议。我修改了我的代码,现在它在 VB.Net 中
  • 这是代码: Dim _recognizer As New SpeechRecognitionEngine() Dim inputs = New Microsoft.Speech.Recognition.Choices() inputs.add("probingdepth") inputs.add("pocketDepth") Dim gb = New Microsoft.Speech.Recognition.GrammarBuilder() gb.Append(inputs) Dim p = New Microsoft.Speech.Recognition.Grammar(gb) p.Name = "ProbingDepth" _recognizer.LoadGrammar(p)
  • 我收到以下错误:重载解析失败,因为没有缩小转换就无法调用可访问的“新”:“公共子新(grammarInfo As Microsoft.Speech.Recognition.GrammarInfo)”:参数匹配参数“grammarInfo”从“Object”缩小到“Microsoft.Speech.Recognition.GrammarInfo”。 'Public Sub New(builder As Microsoft.Speech.Recognition.GrammarBuilder)':参数匹配参数 'builder' 从 'Object' 缩小到 'Microsoft.Speech.Recognition.GrammarBuilder'。你能帮忙吗?提前致谢。
  • @Awadesh 不幸的是,我不知道如何在 VB 中编写它。我建议您将其保留在 C# 中。我认为声明中有些东西。看看this answer
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-23
  • 1970-01-01
  • 1970-01-01
  • 2010-12-18
  • 2016-04-03
相关资源
最近更新 更多