简体   繁体   中英

Speech Recognition Program does not capture voice / command

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Speech.Recognition;

namespace ConsoleApp2
{
  class Program
  {
     static SpeechRecognitionEngine recEngine = new 
     SpeechRecognitionEngine();
     bool keyHold = false;

     static void Main(string[] args)
     {
        Choices commands = new Choices();
        commands.Add(new string[] { "Current dollar value", "Current euro value" });
        GrammarBuilder gBuilder = new GrammarBuilder();
        gBuilder.Append(commands);
        Grammar grammar = new Grammar(gBuilder);

        recEngine.LoadGrammarAsync(grammar);
        recEngine.SetInputToDefaultAudioDevice();
        recEngine.RecognizeAsync(RecognizeMode.Multiple);
     }

     void recEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
     {
         switch (e.Result.Text)
         {
            case "Current dollar value":
                Console.WriteLine("10kr");
                break;

            case "Current euro value":
                Console.WriteLine();
                break;
        }
     }
  }
}

The program is just closing on startup, and does not record my voice, I tried with putting a console.readkey(); under this line in the code recEngine.RecognizeAsync(RecognizeMode.Multiple); . It holds the program up and did not close automatically ofc but still the program does not record my incoming voice command.
Why is this?

Speech Recognition is event-driven.
You need to define the necessary event handlers.
SpeechRecognitionEngine.SpeechRecognized Event must be subscribed to receive the results of the recognition.
See also SpeechRecognitionEngine.AudioStateChanged Event

Also, the Engine must be updated with the new settings before starting the Recognition:
See SpeechRecognitionEngine.RequestRecognizerUpdate Method

Note:
I used only " dollar " and " euro " as Grammar items as a hint: try to use the less key words possible in you grammars, otherwise they become very difficult to handle. You can pronounce the whole "Current dollar value" phrase, the recognized key word, however, is "dollar".


using System.Speech.Recognition;

  static void Main(string[] args)
  {
    using (SpeechRecognitionEngine recEngine = new SpeechRecognitionEngine())
    {
       recEngine.SpeechRecognized += 
             new EventHandler<SpeechRecognizedEventArgs>(recEngine_SpeechRecognized);
       recEngine.AudioStateChanged += 
             new EventHandler<AudioStateChangedEventArgs>(recEngine_AudioStateChange);

       Choices commands = new Choices();
       commands.Add(new string[] { "dollar", "euro" });
       GrammarBuilder gBuilder = new GrammarBuilder();
       gBuilder.Append(commands);
       Grammar grammar = new Grammar(gBuilder);

       recEngine.SetInputToDefaultAudioDevice();
       recEngine.LoadGrammarAsync(grammar);
       recEngine.RequestRecognizerUpdate();

       recEngine.RecognizeAsync(RecognizeMode.Multiple);

       Console.ReadKey();
       recEngine.SpeechRecognized -= recEngine_SpeechRecognized;
       recEngine.AudioStateChanged -= recEngine_AudioStateChange;
    }
  }

  internal static void recEngine_AudioStateChange(object sender, AudioStateChangedEventArgs e)
  {
     Console.WriteLine("Current AudioLevel: {0}", e.AudioState);
  }

  internal static void recEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
  {
     switch (e.Result.Text)
     {
        case "dollar":
           Console.WriteLine("10kr");
           break;
        case "euro":
           Console.WriteLine("A lot more");
           break;
     }
  }

Note2:
On Windows 7, System.Speech.Recognition is not working.
Speech Recognition and TTS (Text To Speech) can be enabled using the free development tools of Microsoft Speech Platform . Download and install the SDK, Runtime and desired languages. This is the server speech recognition implementation, developed to support speech recognition through telephone lines. So it features a very good native filter for noises and a good tolerance in regard to pronunciation quirks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM