语音识别引擎无法识别语音

时间:2018-02-11 10:07:27

标签: c# speech-recognition

我试图创建一个动态语音识别器但由于某种原因它无法正常工作。我试图使用emulaterecognize函数,应用程序工作正常,但我说话时它不起作用。这意味着正确添加了单词列表,并且语音识别事件正常运行,但如果没有模拟识别,则永远不会调用它。任何帮助,将不胜感激。以下是我正在使用的代码。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Speech;
using System.Speech.Recognition;

namespace HotKeyApp
{
    public partial class Form1 : Form
    {
        //initialize speech recognizer
        SpeechRecognitionEngine sre = new SpeechRecognitionEngine(new     System.Globalization.CultureInfo("en-US"));
        //initialize grammer builder
        GrammarBuilder gb = new GrammarBuilder();
        //choices will contain the words from the first column
        Choices jargon = new Choices();
        //words will contain the array to give choices
        string[] words;
        //A speech recognition grammar is a set of rules or constraints that define what a speech recognition engine can recognize as meaningful input.
        Grammar g;

    private int columns = 2;
    private int rows;
    Dictionary<string, string> HotKeys = new Dictionary<string, string>();

    public Form1()
    {
        InitializeComponent();

    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
        MessageBox.Show("ping");
        //to implement loop through the words array if the match call approporaite method
        for (int i = 0; i < words.Length; i++)
        {
            if (e.Result.Text == words[i])
            {
                MessageBox.Show(words[i]);
            }
        }
    }

    private void btnCreate_Click(object sender, EventArgs e)
    {
        //get number of rows/words
        rows = Convert.ToInt32(txtNum.Text);
        //words length is equal to number of rows
        words = new string[rows];

        GenerateTable(columns, rows);

    }
    private void GenerateTable(int columnCount, int rowCount)
    {
        //Clear out the existing row and column styles
        myGridView.Rows.Clear();
        myGridView.Columns.Clear();

        myGridView.Columns.Add("WordColumn", "Word");
        myGridView.Columns.Add("HotKeyColumn", "HotKey");

        //loop as many times as need to create the rows
        for (int y = 0; y < rowCount; y++)
        {
            myGridView.Rows.Add();
        }
    }

    private void btnSubmit_Click(object sender, EventArgs e)
    {
        int i = 0;
        foreach (DataGridViewRow r in myGridView.Rows)
        {
            string Instructions = r.Cells[0].Value.ToString();
            string Command = r.Cells[1].Value.ToString();
            HotKeys.Add(Instructions, Command);
            words[i] = Instructions;
            i++;
        }
        //give jargon the words array
        jargon.Add(words);
        //give the grammer builder the jargon choices
        gb.Append(jargon);
        //build grammer, load grammer, enable voice recognition
        g = new Grammar(gb);
        sre.RequestRecognizerUpdate();
        sre.LoadGrammarAsync(g);

        sre.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(SpeechRecognized);
        //set sre to use default audio device
        sre.SetInputToDefaultAudioDevice();

        sre.RecognizeAsync(RecognizeMode.Multiple);
        MessageBox.Show("Recognition enabled");
        // Register a handler for the SpeechRecognized event.
        //sre.EmulateRecognize("Hello");
    }
}
}

尝试将其转换为控制台应用程序并且其工作但我需要它在Windows窗体应用程序中。这是控制台代码:

class Program
    {
        static SpeechRecognitionEngine sre;
        //words will contain the array to give choices
        static string[] words;

    static void Main(string[] args)
    {
        //initialize speech recognizer
        sre = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));
        //initialize grammer builder
        GrammarBuilder gb = new GrammarBuilder();
        //choices will contain the words from the first column
        Choices jargon = new Choices();
        //A speech recognition grammar is a set of rules or constraints that define what a speech recognition engine can recognize as meaningful input.
        Grammar g;

        string input;

        Console.WriteLine("Input words seperated by comma ,");
        input = Console.ReadLine();
        words = input.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        foreach (string s in words)
        {
            Console.WriteLine(s);
        }
        Console.ReadKey();

        //give jargon the words array
        jargon.Add(words);
        //give the grammer builder the jargon choices
        gb.Append(jargon);
        //build grammer, load grammer, enable voice recognition
        g = new Grammar(gb);
        sre.RequestRecognizerUpdate();
        sre.LoadGrammarAsync(g);
        //set sre to use default audio device
        sre.SetInputToDefaultAudioDevice();

        sre.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(SpeechRecognized);

        sre.RecognizeAsync(RecognizeMode.Multiple);

        Console.ReadLine();


    }
    static void SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
        Console.WriteLine("Recognized Word");
        //to implement loop through the words array if the match call approporaite method
        for (int i = 0; i < words.Length; i++)
        {
            if (e.Result.Text == words[i])
            {
                Console.WriteLine(words[i]);
            }
        }
    }
}

0 个答案:

没有答案