使用SAPI转换语音转文本

时间:2013-10-13 12:51:56

标签: c# speech-recognition speech-to-text sapi

无论如何还是有人知道如何使用微软的Windows表格应用语音识别API进行STT?

1 个答案:

答案 0 :(得分:5)

.NET包含用于语音识别的程序集。 您需要添加对

的引用
  

System.Speech

使用

添加命名空间
using System.Speech.Recognition;

以下代码将分析您的语音并将文本添加到文本框中:

private void startRecognition()
    {
        SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine(); //default culture
        //SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine(new CultureInfo("de-DE"));
        //With specified culture | Could cause an CultureNotFoundException
        Grammar dictationGrammar = new DictationGrammar();
        recognizer.LoadGrammar(dictationGrammar);
        try
        {
            recognizer.SetInputToDefaultAudioDevice();
            RecognitionResult result = recognizer.Recognize();
            if(result != null)
                result_textBox.Text += result.Text + "\r\n"; 
        }
        catch (InvalidOperationException exception)
        {
            MessageBox.Show(exception.Message,exception.Source);
        }
        finally
        {
            recognizer.UnloadAllGrammars();
        }                
    }

要更改识别超时的时间,请更改以下属性:

    recognizer.InitialSilenceTimeout = TimeSpan.FromSeconds(3);
    recognizer.BabbleTimeout = TimeSpan.FromSeconds(2);
    recognizer.EndSilenceTimeout = TimeSpan.FromSeconds(1);
    recognizer.EndSilenceTimeoutAmbiguous = TimeSpan.FromSeconds(1.5);

来源:

http://msdn.microsoft.com/en-us/magazine/cc163663.aspx | http://msdn.microsoft.com/en-us/library/system.speech.recognition.speechrecognitionengine.aspx