.NET中的语音识别不起作用

本文关键字:不起作用 语音识别 NET | 更新日期: 2023-09-27 18:20:27

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

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

请帮帮我。

.NET中的语音识别不起作用

我刚刚将Microsoft.Speech.Recognition替换为System.Speech.Recognition,它就工作了。

不明白哪里出了问题。

您正在调用sre。RecognizeAsyncStop();在它有机会识别任何讲话之前。请记住,异步是非阻塞的,所以它不会等到语音被识别。拆下那根线,它就会工作了。

尝试将较低的值置信度。也许你的麦克风噪音太大,甚至静音了?:)

我怀疑你的朋友正在运行windowsxp,而你正在运行vista或7。我认为微软实际上把语音识别作为操作系统包的一部分,而在xp中没有。这可能是为什么您必须将include从Microsoft更改为System的一个可能答案。