-->

C# Speech Recognizing Multiple Words together? (Re

2019-01-27 03:25发布

问题:

I'm building an application that recognizes multiple words from a user; thus putting together a sentence using the words recognized.

Here's what I have as of now:

namespace SentenceRecognitionFramework__v1_
{
    public partial class Form1 : Form
    {

        SpeechRecognitionEngine recog = new SpeechRecognitionEngine();
        SpeechSynthesizer sp = new SpeechSynthesizer();

        public Form1()
        {
            InitializeComponent();
        }

        private void btnListen_Click(object sender, EventArgs e)
        {
            Choices sList = new Choices();
            sList.Add(new String[] { "what","is", "a", "car" });

            Grammar gr = new Grammar(new GrammarBuilder(sList));

            recog.RequestRecognizerUpdate();
            recog.LoadGrammar(gr);
            recog.SpeechRecognized += sRecognize_SpeechRecognized;
            recog.SetInputToDefaultAudioDevice();
            recog.RecognizeAsync(RecognizeMode.Multiple);
            recog.SpeechRecognitionRejected += sRecognize_SpeechRecognitionRejected;
        }

        private void sRecognize_SpeechRecognitionRejected(object sender, SpeechRecognitionRejectedEventArgs e)
        {
            sentenceBox.Text = "Sorry, I couldn't recognize";
        }

        private void sRecognize_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            sentenceBox.Text = e.Result.Text.ToString();
        }
    }
}

HOWEVER, this code will only recognize one word at a time. Even if I edit my code to do this:

private void sRecognize_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            sentenceBox.Text = sentenceBox.Text + " " + e.Result.Text.ToString();
        }

The application cannot continuously recognize words when I utter the words "What is a car" continuously without having breaks when I speak them out.

What changes can I make in order for the program to recognize an entire sentence built using those words defined, without having to have speech breaks when uttering the sentence?

Output required:

I utter the sentence: What is a car

Application displays: What is a car

PERFECT Example: Google Speech Recognition Google develops a sentence using the words available in their word library

Thank you kindly :)

回答1:

It recognizes one word because you incorrectly constructed the grammar. Since you constructed the grammar consisting of choice of one of the words "what", "is", "a", "car" it exactly recognizes one of the words.

You probably want to read introduction into grammars and related documentation.

http://msdn.microsoft.com/en-us/library/hh378438(v=office.14).aspx

If you want to construct the grammar describing the phrase you can just use GrammarBuilder like this:

  Grammar gr = new Grammar(new GrammarBuilder("what is a car"));

This grammar will recognize a phrase.

To understand how Choices work you can read documentation on Choices:

http://msdn.microsoft.com/en-us/library/microsoft.speech.recognition.choices(v=office.14).aspx



回答2:

This answer may be a little late, but I have not found any other place with an actual answer to this problem. So to save others the the time and frustration, here is how I did it.

using System;
using System.Threading;
using System.Speech;
using System.Speech.Synthesis;
using System.Speech.Recognition;

namespace SpeachTest
{
public class GrammerTest
{
        static void Main()
        {
        Choices choiceList = new Choices();
        choiceList.Add(new string[]{"what", "is", "a", "car", "are", "you", "robot"} );

        GrammarBuilder builder = new GrammarBuilder();
        builder.Append(choiceList);
        Grammar grammar = new Grammar(new GrammarBuilder(builder, 0, 4) );   //Will recognize a minimum of 0 choices, and a maximum of 4 choices

            SpeechRecognizer speechReco = new SpeechRecognizer();
            speechReco.LoadGrammar(grammar);



        }
}
}

The key here is this line

new GrammarBuilder(builder, 0, 4)

This tells the speech recognizer to recognize, up to 4 repetitions of elements form builder, and a minimum of zero.

So it will now recognize

"what is a car"

And if you want more than 4 repetitions, just change new GrammarBuilder(builder, 0, 4)

to

new GrammarBuilder(builder, 0 "the number of repetitions you want")

See this for more info GrammarBuilder(builder, minRepeat, maxRepeat)