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

发布于 2024-12-27 08:23:52 字数 3363 浏览 1 评论 0原文

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

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

请帮助我。

i was working with a simple speech recognition application for controlling relays through a parallel port and this is the basic program how it is supposed to work.

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


    }
}

This works perfectly on my friends computer but in my computer it doesn't recognize what i speak, maybe it is not getting the input. we both have the almost the same config. the microphone is also working well and i dont know whats wrong.

i have installed
Microsoft Speech Platform - Software Development Kit (SDK), version 10.2 (x86 edition)
Microsoft Speech Platform – Server Runtime, version 10.2 (x86 edition)

Please Help me out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

原来分手还会想你 2025-01-03 08:23:52

我刚刚将 Microsoft.Speech.Recognition 替换为 System.Speech.Recognition 并且它起作用了。

不明白出了什么问题。

i just replaced the Microsoft.Speech.Recognition to System.Speech.Recognition and it worked.

Don't understand what is wrong.

亣腦蒛氧 2025-01-03 08:23:52

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

You are calling sre.RecognizeAsyncStop(); before it has a chance to recognise any speech. Remember async is non blocking, so it doesnt wait until speech is recognised. Remove that line and it should work.

意中人 2025-01-03 08:23:52

尝试设置较低的置信值。也许您的麦克风噪音太大甚至静音? :)

Try putting a lower value into confidence. Maybe your microphone has too much noise or even muted? :)

抚笙 2025-01-03 08:23:52

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

I suspect your friend is running windows xp and you are running vista or 7. I think Microsoft actually included the speech recognition as part of the os package where in xp it wasn't. That could be a possible answer to why you had to change the include from Microsoft to System.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文