.NET中的语音识别无法正常工作

时间:2012-01-17 12:49:38

标签: c# speech-recognition speech

我正在使用一个简单的语音识别应用程序来通过并行端口控制继电器,这是它应该如何工作的基本程序。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Speech.Synthesis;
using Microsoft.Speech.Recognition;

namespace speechHardware
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a new SpeechRecognitionEngine instance.
          var  sre = new SpeechRecognitionEngine();
          SpeechSynthesizer s = new SpeechSynthesizer();
          Console.WriteLine("starting recognizer.......");
          s.Speak("starting recognizer.");

          // Create a simple grammar that recognizes "light on", "light off", or "fan on","fan off".
            Choices colors = new Choices();
            Console.WriteLine("option list.......");
            colors.Add("light on");
            colors.Add("light off");
            colors.Add("fan on");
            colors.Add("fan off");

            GrammarBuilder gb = new GrammarBuilder();
            gb.Append(colors);
            Console.WriteLine("starting grammer builder.......");

            // Create the actual Grammar instance, and then load it into the speech recognizer.
            Grammar g = new Grammar(gb);
            sre.LoadGrammar(g);

            // Register a handler for the SpeechRecognized event.
            sre.SpeechRecognized += SreSpeechRecognized;
            //sre.SetInputToWaveFile("C:\Users\Raghavendra\Documents\MATLAB\test.wav");
          sre.SetInputToDefaultAudioDevice();
            Console.WriteLine("input device recognised.......");         
            s.Speak("input device recognised.");
         sre.RecognizeAsync(RecognizeMode.Multiple);
            Console.ReadLine();
            Console.WriteLine("stopping recognizer.....");
            sre.RecognizeAsyncStop();

        }
        static void SreSpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            SpeechSynthesizer s = new SpeechSynthesizer();
            Console.WriteLine("\nSpeech Recognized: \t{0}" + e.Result.Confidence, e.Result.Text);

            if (e.Result.Confidence < 0.85)
                return;

            switch (e.Result.Text)
            {
                case "light on":
                    light(1);                    
                    s.Speak("the light has been turned on.");
                    break;
                case "light off":
                    light(0);
                    s.Speak("the light has been turned off.");
                    break;
                case "fan on":
                    fan(1);
                    s.Speak("the fan has been turned on.");
                    break;
                case "fan off":
                    fan(0);
                    s.Speak("the fan has been turned off.");
                    break;
                default:

                    break;
            }
        }
        static void light(int val)
        {
            Console.WriteLine("\nSpeech Recognized:light ");
        }

        static void fan(int val)
        {
            Console.WriteLine("\nSpeech Recognized: fan");
        }


    }
}

这完全适用于我的朋友计算机,但在我的计算机中它无法识别我说的话,也许它没有得到输入。我们都有几乎相同的配置。麦克风也运行良好,我不知道什么是错的。

我已安装    Microsoft语音平台 - 软件开发工具包(SDK),版本10.2(x86版)    Microsoft语音平台 - 服务器运行时版本10.2(x86版本)

请帮帮我。

4 个答案:

答案 0 :(得分:2)

我刚刚将Microsoft.Speech.Recognition替换为System.Speech.Recognition,但它确实有效。

不明白出了什么问题。

答案 1 :(得分:1)

您正在调用sre.RecognizeAsyncStop();在它有机会认出任何演讲之前。请记住,异步是非阻塞的,所以它不会等到语音被识别出来。删除该行,它应该工作。

答案 2 :(得分:0)

尝试将较低的值放入信心中。也许你的麦克风有太多的噪音甚至是静音? :)

答案 3 :(得分:0)

我怀疑你的朋友正在运行windows xp并且你正在运行vista或7.我认为Microsoft实际上将语音识别作为os包的一部分包含在xp中,而不是。这可能是您必须将包含从Microsoft更改为System的可能答案。