栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

微软.Net离线语音识别

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

微软.Net离线语音识别

微软.net framework自带离线语音识别引擎,可以支持多种语言的离线识别。该引擎具有免费、无需网络、针对性强、运算量小、兼容windows的特点,对于固定语境的语音识别具有不可比拟的优势。

在使用时注意添加对system.speech的引用,默认是不引用的,如果没有请注意.net版本,最好在4.5以上。另外添加using命名空间。以下为简单的控制台例子。

using System;
using System.Speech.Recognition;

namespace 语音识别
{
    class Program
    {
        static void Main(string[] args)
        {

            // Create an in-process speech recognizer for the en-US locale.  
            using (
            SpeechRecognitionEngine recognizer =
              new SpeechRecognitionEngine(
                new System.Globalization.CultureInfo("zh-CN")))
            {
                Choices choices = new Choices(new string[] { "你好", "123","木头人","我是"});
                GrammarBuilder gb = new GrammarBuilder(choices);

                // Create and load a dictation grammar.  
                recognizer.LoadGrammar(new Grammar(gb));

                // Add a handler for the speech recognized event.  
                recognizer.SpeechRecognized +=
                  new EventHandler(recognizer_SpeechRecognized);

                // Configure input to the speech recognizer.  
                recognizer.SetInputToDefaultAudioDevice();

                // Start asynchronous, continuous speech recognition.  
                recognizer.RecognizeAsync(RecognizeMode.Multiple);

                // Keep the console window open.  
                while (true)
                {
                    Console.ReadLine();
                }
            }
        }

        // Handle the SpeechRecognized event.  
        static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            Console.WriteLine("Recognized text: " + e.Result.Text);
        }
    }
}

可以直接下载我的例程https://download.csdn.net/download/u010622265/12649885

需要注意的是CultureInfo,默认情况下中文的操作系统只有"Zh-CN",使用其他的会报错,除非系统安装了其他的语音识别包,win10可在设置里安装:

语法可以有多种形式,具体参考.net文档。

下面列举一种以SRGS(Speech Recognition Grammar Specification)规范定义的语法文件(格式遵循xml)。

该文件中root是main规则,而main规则中又可以包含其他规则(part1-5)定义的语法,最终包含的句式有:

我/你/他+是+学生/老师/工人
我/你/他+喜欢/讨厌+打球/读书/音乐
我/你/他+今年+12/24/36/50+岁




  
    
      
         是 
      
      
          
      
      
        
        今年
        
        岁
      
    
  

  
    
       我 
       你 
       他 
    
  

  
    
       学生 
       老师 
       工人 
    
  
  
  
    
       喜欢
       讨厌
    
  

  
    
       打球
       读书
       音乐
    
  

  
    
       12
       24
       36
       50
    
  

 调用这种SRGS文件定义的语法:

recognizer.LoadGrammar(new Grammar("Grammar.xml"));

SRGS格式详细参考https://www.w3.org/TR/speech-grammar/

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/909205.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号