如何在使用Microsoft SpeechRecognitionEngine时提高结果的准确性?

时间:2015-07-30 18:02:00

标签: c# speech-recognition

我目前正在开发一个DialerService项目。其中一个功能是将录制的.wav媒体文件解释为plaint文本。我使用SpeechRecognitionEngine试图解释内容,我得到了一些不准确的结果,或者有时候没有任何意义的破坏句子。

.wav文件是来自两个或多个客户端之间的电话交谈的录制文件,我测试的文件是我与同事进行的非常简单的简短对话。

所以我的问题是如何提高解释的准确性以及如何为此目的改进我的代码?我知道添加语法将有助于识别一些关键字,但我需要的是通常解释我从用户录制的内容。

这是打击我的工作代码:

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

namespace VoiceRecognition
{
    class Program
    {

        static bool completed;

        static void Main(string[] args)
        {
            using (
             SpeechRecognitionEngine recognizer =
                    new SpeechRecognitionEngine(
                        new System.Globalization.CultureInfo("en-US")))
            {

                // Create and load a grammar.
                Grammar dictation = new DictationGrammar();
                dictation.Name = "Dictation Grammar";

                recognizer.LoadGrammar(new DictationGrammar());

                recognizer.SetInputToWaveFile(@"C:\Projects2\VoiceRecognition2\conf_with_vincent_1.wav");
                // Attach event handlers for the results of recognition.
                //recognizer.AudioLevelUpdated += new EventHandler<AudioLevelUpdatedEventArgs>(recognizer_AudioLevelUpdated);
                //recognizer.AudioStateChanged += new EventHandler<AudioStateChangedEventArgs>(recognizer_AudioStateChanged);

                recognizer.SpeechRecognized  +=  new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
                recognizer.RecognizeCompleted += new EventHandler<RecognizeCompletedEventArgs>(recognizer_RecognizeCompleted);

                // Perform recognition on the entire file.
                Console.WriteLine("Starting asynchronous recognition...");
                completed = false;
                //recognizer.RecognizeAsync();
                recognizer.RecognizeAsync(RecognizeMode.Multiple);

                // Keep the console window open.
                while (!completed)
                {
                    Console.ReadLine();
                }
                Console.WriteLine("Done.");
            }

            Console.WriteLine();
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();


        }

        // Handle the Audio state event.
        static void recognizer_AudioStateChanged(object sender, AudioStateChangedEventArgs e)
        {
            Console.WriteLine("The new audio state is: " + e.AudioState);
        }

        static void recognizer_AudioLevelUpdated(object sender, AudioLevelUpdatedEventArgs e)
        {
            Console.WriteLine("The audio level is now: {0}.", e.AudioLevel);
        }


        // Handle the SpeechRecognized event.
        static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            if (e.Result != null && e.Result.Text != null)
            {
                Console.WriteLine("  Recognized text =  {0}", e.Result.Text);
            }
            else
            {
                Console.WriteLine("  Recognized text not available.");
            }
        }

        // Handle the RecognizeCompleted event.
        static void recognizer_RecognizeCompleted(object sender, RecognizeCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                Console.WriteLine("  Error encountered, {0}: {1}",
                e.Error.GetType().Name, e.Error.Message);
            }
            if (e.Cancelled)
            {
                Console.WriteLine("  Operation cancelled.");
            }
            if (e.InputStreamEnded)
            {
                Console.WriteLine("  End of stream encountered.");
            }
            completed = true;
        }



    }
}

另一个课程是:

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

public class SpeechReconizer
{

    SpeechRecognitionEngine _speechRecognitionEngine;
    public SpeechReconitionResult ReadResult { get; set; }

    public SpeechReconizer()
    {
        Grammar dictation = new DictationGrammar();
        dictation.Name = "Dictation Grammar";



        _speechRecognitionEngine = new SpeechRecognitionEngine();
        _speechRecognitionEngine.SetInputToDefaultAudioDevice();
        _speechRecognitionEngine.LoadGrammar(dictation);
        _speechRecognitionEngine.InitialSilenceTimeout = TimeSpan.FromSeconds(3);
        _speechRecognitionEngine.BabbleTimeout = TimeSpan.FromSeconds(2);
        _speechRecognitionEngine.EndSilenceTimeout = TimeSpan.FromSeconds(1);
        _speechRecognitionEngine.EndSilenceTimeoutAmbiguous = TimeSpan.FromSeconds(1.5);
        _speechRecognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
        _speechRecognitionEngine.SpeechRecognized += RecognizerSpeechRecognized;
        _speechRecognitionEngine.RecognizeCompleted += RecognizerRecognizeCompleted;
    }



    public SpeechReconitionResult ReadSpeech(string sourceAudio)
    {
        ReadResult = new SpeechReconitionResult();

        _speechRecognitionEngine.SetInputToWaveFile(sourceAudio);


        _speechRecognitionEngine.Recognize();
        return ReadResult;

    }

    private void RecognizerSpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
        if (e.Result != null && e.Result.Text != null)
        {
            ReadResult.Success = true;
            ReadResult.Text = e.Result.Text;
        }
        else
        {
            ReadResult.Text = "Recognized text not available.";
        }
    }

    private void RecognizerRecognizeCompleted(object sender, RecognizeCompletedEventArgs e)
    {
        if (e.Error != null)
        {
            ReadResult.Success = false;
            ReadResult.ErrorMessage = string.Format("{0}: {1}",
                          e.Error.GetType().Name, e.Error.Message);
        }
        if (e.Cancelled)
        {
            ReadResult.Success = false;
            ReadResult.ErrorMessage = "Operation cancelled.";
        }
    }

}
public class SpeechReconitionResult
{
    public string Text { get; set; }
    public bool Success { get; set; }
    public string ErrorMessage { get; set; }
    public bool Complete { get; set; }
}

测试结果是(在控制台中):

Starting asynchronous recognition...
  Recognized text = Helence and the globe or east
  Recognized text = alarmed
  Recognized text = and client thanks
  Recognized text = what aren't going to do and that they
  Recognized text = aren't goint to rule
  Recognized text = working to dear E
  Recognized text = N
  Recognized text = at dinner
  Recognized text = and
  Recognized text = that going there
  Recognized text = and you have a 98 no problem bars
  End of stream encountered.

实际内容是: - 你好文森特。 - 你好鲍里斯。 -你好吗? -我很好。 -你今天打算做什么? - 我要看电视,吃饭,回家。 -感谢您有一个愉快的一天。 没问题。

2 个答案:

答案 0 :(得分:4)

System.Speech.Recognition为默认的窗口语音识别提供支持。它专为单个用户设计,可由用户通过Windows语音识别培训进行培训。

您可能需要的是Microsoft.Speech.Recognition库,它专为低质量音频而设计。它的工作方式几乎相同,但它不是为听写而设计的。它更多地用于检测来自电话质量音频的命令。如果您想试一试我发现的最新版本:http://www.microsoft.com/en-us/download/details.aspx?id=27226

答案 1 :(得分:0)

实际上,我已经使用 C# 测试了几种方法。其中之一是 SrgsToken.Pronunciation.Property 这实质上允许您从 SrgsOneOf 对象创建“俚语规则”。假设您有一组命令,例如“放弃”,但此人说的是“放弃”。你实际上可以创建一个 SrgsOneOf 放弃 = new SrgsOneOf(new string[] { "abandan", "abandin", "ah'bandon", abanon });

从 SrgsOneOf 对象创建“俚语规则”。SrgsRule slangRule = new SrgsRule("slang", 放弃);

此外,在 C# 中,枚举(或枚举类型)用于将常量名称分配给一组数字整数值。它使常量值更具可读性。我将此应用到我的 switch case 语句中,从那时起,响应变得更加准确和清晰。

最后:对于练习良好的语法构建至关重要,投资购买昂贵的麦克风/优质的麦克风。许多人忽视的另一个因素是字符串的使用和它可以消耗的内存。使用“见下文”的常量字符串的主要观点是常量字符串会被自动插入。如果您有 1000 个具有常规字符串字段的类型实例,并且所有实例都存储永远不会更改的相同字符串,那么将存储 1000 个相等的字符串实例,从而不必要地炸毁应用程序的内存配置文件。如果你声明字符串常量,“它只会消耗一次内存”。这与直接使用字符串文字的行为相同。与静态只读字符串相反,常量字符串的值直接存储在引用类中。

假设我有选择:
选择命令 = new Choices(); commands.Add(new string[] { "scarlett find biochemistry"});

我该如何采取更明智的方法? 回答: private const string V = "scarlett find biochemistry"; 现在: 选择命令 = new Choices(); commands.Add(new string[] { V });

这极大地改进了我在 YouTube.com 上看到的系统。 Scarlett Extreme