1

我的语音识别出现错误,我不断收到“在进行识别之前必须加载至少一个语法”当您说出相应的链接名称时,我无法显示图像。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SpeechLib;
using System.IO;
using System.Speech.Recognition;
using System.Globalization;

namespace SimpleSpeechRecognition
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private SpeechRecognitionEngine recognizer;

        private void Form1_Load(object sender, EventArgs e)
        {
            speechListBox1.Items.Add("Dog");
            speechListBox1.Items.Add("Elephant");
            speechListBox1.SpeechEnabled = true;

            recognizer = new SpeechRecognitionEngine(new CultureInfo("en-GB"));
            recognizer.SetInputToDefaultAudioDevice();

            Choices choices = new Choices("Dog", "Elephant");
            GrammarBuilder m_GrammarBuilder = new GrammarBuilder(choices);
            Grammar m_Speech = new Grammar(m_GrammarBuilder);
            recognizer.LoadGrammar(m_Speech);

            recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
            recognizer.RecognizeAsync(RecognizeMode.Multiple);
        }

        void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            foreach (RecognizedWordUnit word in e.Result.Words)
            {
                switch (word.Text)
                {
                    case "Dog":
                        pictureBox1.Image = Image.FromFile("C:\\" + "dog.jpg");;
                        break;
                    case "Elephant":
                        pictureBox1.Image = Image.FromFile("C:\\" + "elephant.jpg");
                        break;
                }
            }
        }
        private void speechListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

            //MessageBox.Show(speechListBox1.SelectedItems[0].ToString());
            SayPhrase(speechListBox1.SelectedItems[0].ToString());
            //pictureBox1.Image = Image.FromFile("C:\\" + "dog.jpg");
            //pictureBox1.Image = Image.FromFile(((FileInfo)speechListBox1.SelectedItem).FullName);
            pictureBox1.Refresh();
        }


       private void SayPhrase(string PhraseToSay )
       {

                SpeechVoiceSpeakFlags SpFlags = new SpeechVoiceSpeakFlags();
                SpVoice Voice  = new SpVoice();
                Voice.Speak(PhraseToSay, SpFlags);

       }
    }
}
4

1 回答 1

2

错误不言自明:

语音引擎必须有一组“选择”来听,但是这些需要内置到适当的语法中,以便语音引擎可以听出来。

GrammarBuilder m_GrammarBuilder = new GrammarBuilder(choices);
Grammar m_Speech = (m_GrammarBuilder);

然后只需将语法加载到:

recognizer.LoadGrammar(m_Speech);

我认为这应该可以解决您的问题。还值得注意的是,您也可以通过 .UnloadGrammar() 函数卸载和加载不同的语法集。

此外,使用适当的文化信息初始化 SpeechRecognitionEngine 也是值得的。对于英语(英国),这是:

new SpeechRecognitionEngine(new CultureInfo("en-GB"))
于 2013-02-16T16:22:58.770 回答