PlatformNotSupportedException Using .NET Speech Re

2019-02-25 00:20发布

So I'm trying voice recognition for C#, I'm using System.Speech.Recognition, and, I was searching around on the internet, trying out several pieces of code for some basic speech recognition, the best one I could find was this:

using System;
using System.Text;
using System.Windows.Forms;
using System.Speech.Recognition;

namespace SpeechRecognition
{
    public partial class MainForm : Form
    {

        SpeechRecognitionEngine recognitionEngine; 


        public MainForm()
        {
            InitializeComponent();

            Initialize();
        }

        private void Initialize()
        {
            recognitionEngine = new SpeechRecognitionEngine();
            recognitionEngine.SetInputToDefaultAudioDevice();
            recognitionEngine.SpeechRecognized += (s, args) =>
            {
                foreach (RecognizedWordUnit word in args.Result.Words)
                {
                    // You can change the minimun confidence level here
                    if (word.Confidence > 0.8f)
                        freeTextBox.Text += word.Text + " ";
                }
                freeTextBox.Text += Environment.NewLine;
            };
        }

        private void startButton_Click(object sender, EventArgs e)
        {
            try
            {
                recognitionEngine.UnloadAllGrammars();
                recognitionEngine.LoadGrammar(new DictationGrammar());
                RecognitionResult result = recognitionEngine.Recognize(new TimeSpan(0, 0, 20));

                if (result != null)
                {
                    foreach (RecognizedWordUnit word in result.Words)
                    {

                        freeTextBox.Text += word.Text + " ";
                    }
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void startAsyncButton_Click(object sender, EventArgs e)
        {
            recognitionEngine.UnloadAllGrammars();
            recognitionEngine.LoadGrammar(new DictationGrammar());
            recognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
        }



        private void stopButton_Click(object sender, EventArgs e)
        {
            recognitionEngine.RecognizeAsyncStop();
        }


        private void startAsyncGrammarButton_Click(object sender, EventArgs e)
        {         
            try
            {
                recognitionEngine.UnloadAllGrammars();

                Grammar cg = CreateSampleGrammar();
                recognitionEngine.LoadGrammar(cg);
                recognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        private Grammar CreateSampleGrammar()
        {
            Choices commandChoices = new Choices("Calculator", "Notepad", "Internet Explorer", "Paint");
            GrammarBuilder grammarBuilder = new GrammarBuilder("Start");
            grammarBuilder.Append(commandChoices);
            Grammar g = new Grammar(grammarBuilder);
            g.Name = "Available programs";
            return g;
        }

    }
}

Now, I tried this, and some others, and they all resulted in the same error, a PlatformNotSupportedException, in the error it says: "There is no recogniser installed".

Is there any way around this? I'm running Windows 7 64 bits.

3条回答
一夜七次
2楼-- · 2019-02-25 00:34

I had same problem. I just started VisualStudio in x86 Debug mode and the System.Speech.dll was used for x64. In Release mode (x64) it worked. Perhaps you have same issue with CPU Architecture and System.Speech.dll setup.

查看更多
萌系小妹纸
3楼-- · 2019-02-25 00:37

The Speech Platform Runtime 11 and the Speech Platform SDK 11 do not include Runtime Languages for speech recognition or for speech synthesis (TTS or text-to-speech). You must install them separately. A Runtime Language includes the language model, acoustic model, and other data necessary to provision a speech engine to perform speech recognition or TTS in a particular language. There are separate Runtime Languages for speech recognition or speech synthesis. The version of Runtime Languages that you download (for example, version 11.0) must match the version of the Speech Platform Runtime that you have installed. You can download Runtime Languages using this link.

From http://msdn.microsoft.com/en-us/library/hh362873.aspx.

I think you're using the version that shipped with .NET, but there have been several revisions released out of band since then. Microsoft Speech Services v11 is the current release as of today. If you install the SDK, add a reference, and change your namespace to Microsoft.Speech (instead of System.Speech) you should be updated.

查看更多
淡お忘
4楼-- · 2019-02-25 00:47

Which version of Windows 7 are you running? Which language?

Can you use the built in Windows 7 dictation features? Is the speech recognition control panel app working for you? See http://windows.microsoft.com/en-US/windows7/Setting-speech-options

I thought all Windows 7 versions should come with recognizer preinstalled. However, if you are using an unsupported language, it may not.

From https://stackoverflow.com/a/2998963/90236:

You can use use the APIs to query and determine your installed recongizers Desktop: System.Speech.Recognition.SpeechRecognitionEngine.InstalledRecognizers()

I found that I can also see what recognizers are installed by looking at the registry keys: Desktop recognizers: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Recognizers\Tokens

If you want to try a very simple program that might help, see https://stackoverflow.com/a/4737003/90236

查看更多
登录 后发表回答