admin管理员组文章数量:1655036
Unity3d软件中的语音识别,包含了关键字识别、语法识别和听写识别,本篇先介绍Win10平台下Unity3d关键字识别
本文介绍了如何使用Windows语音API在unity3d中实现语音识别。本例给出的实现也可用于虚拟现实(VR)应用。
从Unity 5.4.0开始,Unity引擎添加了windows语音识别API (UnityEngine.Windows.Speech)来识别语音输入。这些api支持所有类型的windows平台(windows editor、windows independent和windows Store),但只支持windows 10操作系统,支持中文语音识别。
关键点:
- 名称空间:UnityEngine.Windows.Speech
- Unity3d支持:Unity 5.4.0或更高版本
- 操作系统支持:Windows 10
Unity3d软件中的语音识别
语音识别API可分为两大类:
- 短语识别
Keyword Recognizer(关键字识别)
Grammar Recognizer(语法识别) - 听写识别
Dictation Recognizer(听写识别)
注意:不能同时使用短语识别和语音听写识别。在同一时间内间个实例中,应用程序中只有一个识别是激活的。
短语识别
短语识别用于识别用户所说的特定短语。
Windows Store构建的发布设置:
如果您想为windows store构建应用程序,那么可以启用麦克风功能来利用应用程序的语音输入。
- 在Unity编辑器中设置 (Edit -> Project Settings -> Player)
- 选择 “Windows Store”标签
- 检查麦克风(Publishing Settings -> Capabilities)
短语识别可以分为两种类型:关键词识别和语法识别
关键词识别
关键字识别用于监听给定字符串数组。让我们看看如何实现关键字识别器。
Step 1: 定义字符串数组
Step 2: 订阅和处理OnPhraseRecognized事件。当给定短语被识别时,将调用此事件。.
Step 3: 开始关键字识别
下面给出了实现关键字识别的完整脚本。将此脚本复制到项目中,可以挂在任何游戏物体上。
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Windows.Speech;
public class KeyWordRecognizerBehaviour : MonoBehaviour {
KeywordRecognizer keywordRecognizer;
// keyword array
public string[] Keywords_array;
// Use this for initialization
void Start () {
// Change size of array for your requirement
Keywords_array = new string[2];
Keywords_array [0] = "hello";
Keywords_array [1] = "how are you";
// instantiate keyword recognizer, pass keyword array in the constructor
keywordRecognizer = new KeywordRecognizer(Keywords_array);
keywordRecognizer.OnPhraseRecognized += OnKeywordsRecognized;
// start keyword recognizer
keywordRecognizer.Start ();
}
void OnKeywordsRecognized(PhraseRecognizedEventArgs args)
{
Debug.Log ("Keyword: " + args.text + "; Confidence: " + args.confidence + "; Start Time: " + args.phraseStartTime + "; Duration: " + args.phraseDuration);
// write your own logic
}
}
运行结果:
可以同时有多个关键字识别器,但它们不监听普通关键字
如果您在项目中使用多个关键字识别器,那么您可以使用PhraseRecognitionSystem类用一行代码停止并重新启动所有关键字识别。
// shut down phrase recognition system. All keyword recognizer will be stopped
PhraseRecognitionSystem.Shutdown();
// restore all recognizer in the previous state
PhraseRecognitionSystem.Restart();
二、Windows10平台下Unity3d的语音识别——语法识别
三、Windows10平台下Unity3d的语音识别——听写识别
版权声明:本文标题:一、Windows10平台下Unity3d的语音识别——关键字识别 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dianzi/1729690299a1210103.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论