Speech Recognition可显示列表框中的图片

本文关键字:列表 Recognition 显示 Speech | 更新日期: 2023-09-27 17:59:36

我的语音识别有一个错误,我一直得到"在进行识别之前必须加载至少一个语法"当你说出它对应的链接名称时,我无法获得要显示的图像。

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

Speech Recognition可显示列表框中的图片

错误不言自明:

语音引擎必须有一组"选项"来监听,但这些选项需要内置到适当的语法中,以便语音引擎监听。

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

然后只需加载语法:

recognizer.LoadGrammar(m_Speech);

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

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

new SpeechRecognitionEngine(new CultureInfo("en-GB"))