**学习源于官方文档 Voice input in Unity **

笔记一部分是直接翻译官方文档,部分各人理解不一致的和一些比较浅显的保留英文原文

(三)Hololens Unity 开发之 语音识别

HoloLens 有三大输入系统,凝视点、手势和声音 ~ 本文主要讲解 语音输入 ~ (测试不支持中文语音输入~)

一、概述

HoloToolKit Unity 包提供了三种 语音输入的方式 :

  • Phrase Recognition 短语识别

    * KeywordRecognizer 单一关键词识别

    * GrammarRecognizer 语法识别

  • Dictation Recognition 听写识别

    * DictationRecognizer 将声音识别转化为文字

Note: KeywordRecognizer 和 GrammarRecognizer 是主动活跃识别的方式~ 也就是说调用开始识别的方法,那么久处于活跃状态开始识别,而DictationRecognizer只要注册了就就在默默的监听语音输入,一旦监听到关键词那么久触发回调

二、Unity开发打开Microphone权限

下面是官方文档 讲解 如何打开microphone权限,直接上配图~

The Microphone capability must be declared for an app to leverage Voice input.

  1. In the Unity Editor, go to the player settings by navigating to "Edit > Project Settings > Player"
  2. Click on the "Windows Store" tab
  3. In the "Publishing Settings > Capabilities" section, check the Microphone capability

三、Phrase Recognition 短语识别

To enable your app to listen for specific phrases spoken by the user then take some action, you need to:

  1. Specify which phrases to listen for using a KeywordRecognizer or GrammarRecognizer
  2. Handle the OnPhraseRecognized event and take action corresponding to the phrase recognized

使用短语识别嘞~需要做两个步骤:

  1. 指定需要监听的 短语 或者 关键词
  2. 处理识别到 短语 或者 关键词 之后的事件回调 ~ OnPhraseRecognized

1、 关键词识别 (直接Demo代码~)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Windows.Speech;
using System.Linq; public class VoiceInputDemo : MonoBehaviour { public Material yellow;
public Material red;
public Material blue;
public Material green; /// <summary>
/// 关键词识别对象
/// </summary>
private KeywordRecognizer keywordRecognizer; /// <summary>
/// 存放关键词的字典
/// </summary>
private Dictionary<string, System.Action> keywords = new Dictionary<string, System.Action>();
// Use this for initialization
void Start () { // 向字典中添加关键词,key为关键词, vallue为一个匿名action
keywords.Add("yellow", () =>
{
Debug.Log("听到了 yellow");
transform.GetComponent<MeshRenderer>().material = yellow;
}); keywords.Add("red", () =>
{
Debug.Log("听到了 red");
transform.GetComponent<MeshRenderer>().material = red;
}); keywords.Add("green", () =>
{
Debug.Log("听到了 green");
transform.GetComponent<MeshRenderer>().material = green;
}); keywords.Add("blue", () =>
{
Debug.Log("听到了 blue");
transform.GetComponent<MeshRenderer>().material = blue;
}); // 初始化关键词识别对象
keywordRecognizer = new KeywordRecognizer(keywords.Keys.ToArray()); // 添加关键词代理事件
keywordRecognizer.OnPhraseRecognized += KeywordRecognizer_OnPhraseRecognized; // 注意: 这方法一定要写,开始执行监听
keywordRecognizer.Start();
} private void KeywordRecognizer_OnPhraseRecognized(PhraseRecognizedEventArgs args)
{ System.Action keywordAction;
// if the keyword recognized is in our dictionary, call that Action.
// 如果关键字在我们的字典中被识别,调用该action。
if (keywords.TryGetValue(args.text, out keywordAction))
{
Debug.Log("听到了,进入了事件方法 关键词语 : " + args.text.ToString()); // 执行该action
keywordAction.Invoke();
}
} // Update is called once per frame
void Update () { }
}

2、 语法识别 GrammarRecognizer

按照官方文档上来说的 我得 创建一个 SRGS 的XML文件放在 StreamingAssets 文件夹下~不过我没有做到英文语法输入的需求 ~ 感兴趣的点击 https://msdn.microsoft.com/en-us/library/hh378349(v=office.14).aspx 自己查看官方文段对SRGS的讲解~

下面贴的一段官方文档的代码

Once you have your SRGS grammar, and it is in your project in a StreamingAssets folder:

<PROJECT_ROOT>/Assets/StreamingAssets/SRGS/myGrammar.xml

Create a GrammarRecognizer and pass it the path to your SRGS file:

private GrammarRecognizer grammarRecognizer;
grammarRecognizer = new GrammarRecognizer(Application.streamingDataPath + "/SRGS/myGrammar.xml");

Now register for the OnPhraseRecognized event

grammarRecognizer.OnPhraseRecognized += grammarRecognizer_OnPhraseRecognized;

You will get a callback containing information specified in your SRGS grammar which you can handle appropriately. Most of the important information will be provided in the semanticMeanings array.

private void Grammar_OnPhraseRecognized(PhraseRecognizedEventArgs args)
{
SemanticMeaning[] meanings = args.semanticMeanings;
// do something
}

Finally, start recognizing!

grammarRecognizer.Start();

四、听写

1、概述

DictationRecognizer 使用这个对象可以识别语音输入转化为文本,使用这个对象有三个步骤~

  1. 创建一个DictationRecognizer对象
  2. 注册Dictation 事件
  3. 开始识别听写

2、开启网络客户端权限

The "Internet Client" capability, in addition to the "Microphone" capability mentioned above, must be declared for an app to leverage dictation.

  1. In the Unity Editor, go to the player settings by navigating to "Edit > Project Settings > Player" page
  2. Click on the "Windows Store" tab
  3. In the "Publishing Settings > Capabilities" section, check the InternetClient capability

直接上Unity的图吧~

3、Demo代码示例~

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Windows.Speech; public class VoiceDictationDemo : MonoBehaviour
{ private DictationRecognizer dictationRecognizer; // Use this for initialization
void Start()
{ // 定义一个听写对象
dictationRecognizer = new DictationRecognizer(); // 注册一个 结果回调 事件
dictationRecognizer.DictationResult += DictationRecognizer_DictationResult;
// 注册一个 完成 事件
dictationRecognizer.DictationComplete += DictationRecognizer_DictationComplete;
// 注册一个 错误 事件
dictationRecognizer.DictationError += DictationRecognizer_DictationError;
// 注册一个 识别语句 的 事件
dictationRecognizer.DictationHypothesis += DictationRecognizer_DictationHypothesis; dictationRecognizer.Start();
} private void DictationRecognizer_DictationHypothesis(string text)
{
Debug.Log("进入了Hypothesis 的 事件 回调 ~ " + text);
dictationRecognizer.Start();
} private void DictationRecognizer_DictationError(string error, int hresult)
{
Debug.Log("进入了Error 的 事件 回调 ~ " + error + " 状态码 " + hresult);
dictationRecognizer.Start();
} private void DictationRecognizer_DictationComplete(DictationCompletionCause cause)
{ Debug.Log("进入了Complete 的 事件 回调 ~ " + cause);
dictationRecognizer.Start();
} private void DictationRecognizer_DictationResult(string text, ConfidenceLevel confidence)
{
Debug.Log("进入了Result 的 事件 回调 ~ " + text + " 枚举 " + confidence);
dictationRecognizer.Start();
} void OnDestroy()
{
// 销毁事件
dictationRecognizer.DictationResult -= DictationRecognizer_DictationResult;
dictationRecognizer.DictationComplete -= DictationRecognizer_DictationComplete;
dictationRecognizer.DictationHypothesis -= DictationRecognizer_DictationHypothesis;
dictationRecognizer.DictationError -= DictationRecognizer_DictationError;
dictationRecognizer.Dispose();
} }

用有道 里面 的英语短视频 做了下测试~ 几乎能达到百分之九十八 以上的 识别率。。感叹微软做的挺不错的~

五、同时使用 语音识别 和 听写 (文档翻译)

If you want to use both phrase recognition and dictation in your app, you'll need to fully shut one down before you can start the other. If you have multiple KeywordRecognizers running, you can shut them all down at once with:

如果你想同时使用 语音识别 和 听写识别,那么你必须关闭一个再启动另外一个~ 如果你有多个语音识别的对象KeywordRecognizers,那么你可以通过下面的方法把他们全部关闭~

PhraseRecognitionSystem.Shutdown();

In order to restore all recognizers to their previous state, after the DictationRecognizer has stopped, you can call:

当然,你也可以恢复关闭前的所有状态,当在你的听写识别结束的时候,你可以调用下面的方法恢复之前的语音识别~

PhraseRecognitionSystem.Restart();

You could also just start a KeywordRecognizer, which will restart the PhraseRecognitionSystem as well.

当然,你也可以只启动一个KeywordRecognizer语音识别对象同样的也是用PhraseRecognitionSystem来控制其暂停或者恢复

(三)Hololens Unity 开发之 语音识别的更多相关文章

  1. (二)Hololens Unity 开发之 语音识别

    学习源于官方文档 Voice input in Unity 笔记一部分是直接翻译官方文档,部分各人理解不一致的和一些比较浅显的保留英文原文 (二)Hololens Unity 开发之 语音识别 Hol ...

  2. (二)Hololens Unity 开发入门 之 Hello HoloLens~

    学习源于官方文档 微软官文~ 笔记一部分是直接翻译官方文档,部分各人理解不一致的和一些比较浅显的保留英文原文 (二)Hololens Unity 开发入门 之 Hello HoloLens~ 本文主要 ...

  3. (四)Hololens Unity 开发之 凝视系统

    学习源于官方文档 Gaze in Unity 笔记一部分是直接翻译官方文档,部分各人理解不一致的和一些比较浅显的保留英文原文 HoloLens 有三大输入系统,凝视点.手势和声音 ~ 本文主要记录凝视 ...

  4. (五)Hololens Unity 开发之 手势识别

    学习源于官方文档 Gestures in Unity 笔记一部分是直接翻译官方文档,部分各人理解不一致的和一些比较浅显的保留英文原文 (五)Hololens Unity 开发之 手势识别 HoloLe ...

  5. (一)Hololens Unity 开发环境搭建(Mac BOOTCAMP WIN10)

    (一)Hololens Unity 开发环境搭建(Mac BOOTCAMP WIN10) 系统要求 64位 Windows 10 除了家庭版的 都支持 ~ 64位CPU CPU至少是四核心以上~ 至少 ...

  6. HoloLens开发手记 - Unity development overview 使用Unity开发概述

    Unity Technical Preview for HoloLens最新发行版为:Beta 24,发布于 09/07/2016 开始使用Unity开发HoloLens应用之前,确保你已经安装好了必 ...

  7. 使用Unity开发HoloLens应用

    https://developer.microsoft.com/en-us/windows/holographic/install_the_tools 导读:开发者们在陆续收到HoloLens开发者版 ...

  8. Unity开发概览(HoloLens开发系列)

    本文翻译自:Unity development overview 要开始使用Unity创建全息应用,点此安装包含Unity HoloLens技术预览的开发工具.Unity HoloLens技术预览基于 ...

  9. 【Holograms 101D】一步步用Unity 开发 Hologram

    转载请注明出处: copperface:[Holograms 101D]一步步用Unity 开发 Hologram Holograms 101 该教程将带领你走完 Hologram 创建 的全过程.整 ...

随机推荐

  1. hdu 1874 Dijkstra算法

    先贴个网上找的比较通俗易懂的教程: 2.1Dijkstra算法(非负权,使用于有向图和无向图) Dijkstra算法是典型最短路算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心 ...

  2. Spring(十五)之声明式事务

    声明式事务管理方法允许你在配置的帮助下而不是源代码硬编程来管理事务.这意味着你可以将事务管理从事务代码中隔离出来.你可以只使用注释或基于配置的 XML 来管理事务. bean 配置会指定事务型方法.下 ...

  3. Tomcat中的Filter

    Filter 节选部分源码.源码版本 Tomcat8.5 说明 filter 是 Servlet 规范 filter 是在 ,执行 Servlet.service方法之前执行 Filter相关接口 p ...

  4. vue每次请求加头部(shiro+vue)

    前后台分离,全局请求加头部 设置全局请求为ajax请求 _axios.interceptors.request.use( function(config) { var accessToken = lo ...

  5. python sorted() count() set(list)-去重 -- search + match

    2.用python实现统计一篇英文文章内每个单词的出现频率,并返回出现频率最高的前10个单词及其出现次数,并解答以下问题?(标点符号可忽略) (1) 创建文件对象f后,解释f的readlines和xr ...

  6. C# 缓存工厂类

    描 述:缓存工厂类 /// <summary> /// 描 述:缓存工厂类 /// </summary> public class CacheFactory { /// < ...

  7. 升级MAC OS到10.13, 10.14系统后UNITY工程无法加载资源的解决办法

    升级MAC OS到10.13, 10.14系统后,出现UNITY工程无法加载资源的情况: Unity项目中Asset目录显示为空! 解决办法一: 打开Launchpad中的磁盘工具 (也就是实用工具下 ...

  8. 《关于安卓和IOS开发》

    28年前有人发明www microsoft技术开发人员lot 看论文可以看中国知网 微软亚洲研究院 WWDC苹果开发者大会上,苹果都会发布一些新的公司发展出的新的产品的新技术.iOS开发,用到的语言有 ...

  9. Angular7教程-02-Angular项目目录及基本文件说明

    本教程基于Angular7,更新时间2018-11-05. 1. 项目根目录如下: e2e文件夹:end to end,测试目录,主要用于集成测试. node_modules:项目的模块依赖目录. s ...

  10. 蓝桥杯第七届决赛(国赛)C++B组 第四题 机器人塔

    机器人塔 X星球的机器人表演拉拉队有两种服装,A和B.他们这次表演的是搭机器人塔. 类似: A    B B   A B A  A A B B B B B A BA B A B B A 队内的组塔规则 ...