簡體   English   中英

C#語音同時識別多個單詞? (認識一個句子)

[英]C# Speech Recognizing Multiple Words together? (Recognize a sentence)

我正在構建一個可以識別來自用戶的多個單詞的應用程序; 從而使用識別的單詞組合一個句子。

這是我現在所擁有的:

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();
        }
    }
}

但是,此代碼一次只能識別一個單詞。 即使我編輯我的代碼來做到這一點:

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

當我連續說出“什么是汽車”這個詞時,應用程序無法連續識別單詞,而當我說出它們時沒有中斷。

為了讓程序識別使用這些定義的單詞構建的整個句子,我可以進行哪些更改,而不必在說出句子時出現語音中斷

需要輸出:

我說出一句話:什么是汽車

應用展示:什么是汽車

完美示例:谷歌語音識別谷歌使用其詞庫中可用的單詞開發一個句子

非常感謝你 :)

它可以識別一個單詞,因為您錯誤地構建了語法。 由於您構建了由選擇“what”、“is”、“a”、“car”其中一個詞組成的語法,因此它可以准確識別其中一個詞。

您可能想閱讀語法和相關文檔的介紹。

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

如果你想構建描述短語的語法,你可以像這樣使用 GrammarBuilder:

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

該語法將識別一個短語。

要了解 Choices 的工作原理,您可以閱讀有關 Choices 的文檔:

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

這個答案可能有點晚了,但我還沒有找到任何其他地方對這個問題有實際的答案。 因此,為了節省其他人的時間和挫折感,我就是這樣做的。

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);



        }
}
}

這里的關鍵是這條線

新的 GrammarBuilder(builder, 0, 4)

這告訴語音識別器識別builder中最多 4 次重復的元素,並且最少為 0。

所以它現在會識別

"what is a car"

如果您想要超過 4 次重復,只需更改new GrammarBuilder(builder, 0, 4)

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

有關更多信息,請參閱此GrammarBuilder(builder, minRepeat, maxRepeat)

您應該使用richTextBox而不是textbox然后附加e.result.text ,例如:

richtextbox.append(e.result.text)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM