Unity-使用Windows系统下的语音识别(关键字)

文章目录[x]
  1. 1:简介
  2. 2:关键字识别
  3. 3:关键字识别代码

简介

Unity3D为Windows平台提供了语音识别功能,语音识别种类包括:关键字识别、语法识别和听写识别。关键字识别、语法识别可以离线使用,听写识别需要联网。如有离线听写识别的需求可以使用OpenAI开源的whisper项目,在本地部署whisper然后自行添加Web接口。
从Unity 5.4.0开始,Unity引擎添加了windows语音识别API (UnityEngine.Windows.Speech)来识别语音输入。这些api支持所有类型的windows平台(windows editor、windows independent和windows Store),但只支持windows 10及以上操作系统,支持中文语音识别。
  • 操作系统要求: Window10、Windows11 (需要在设置中开启语音识别功能)
  • Unity版本:5.4以上
  • 引用命名空间: UnityEngine.Windows.Speech

关键字识别

  1. 1定义要识别的关键字 字符串数组
  2. 2订阅和处理OnPhraseRecognized事件。当给定短语被识别时,将调用此事件
  3. 3开始关键字识别

关键字识别代码

为了方便使用,为关键字识别做一个基类WOV(Wake of voice的缩写,我主要用关键字识别后做唤醒操作)
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Wake-on-voice 语音唤醒 base类
/// </summary>
public class WOV : MonoBehaviour
{

    /// <summary>
    /// 关键词回调
    /// </summary>
    protected Action<string> OnKeywordRecognizer;
    /// <summary>
    /// 绑定唤醒回调
    /// </summary>
    /// <param name=""></param>
    /// <param name="_callback"></param>
    public virtual void OnBindAwakeCallBack(Action<string> _callback)
    {
        OnKeywordRecognizer += _callback;
    }
    /// <summary>
    /// 开始识别
    /// </summary>
    public virtual void StartRecognizer()
    {

    }
    /// <summary>
    /// 结束识别
    /// </summary>
    public virtual void StopRecognizer()
    {

    }
    /// <summary>
    /// 唤醒词回调
    /// </summary>
    /// <param name="_msg"></param>
    protected virtual void OnAwakeOnVoice(string _msg)
    {
        if(OnKeywordRecognizer==null)
            return;

        OnKeywordRecognizer(_msg);
    }
}
unity关键字识别类 UnityWakeOnVoice(我主要用关键字识别后做唤醒操作)
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
#if UNITY_STANDALONE_WIN
using UnityEngine.Windows.Speech;
using Util;
#endif
/// <summary>
/// unity内置语音唤醒 windows有效
/// </summary>
public class UnityWakeOnVoice : WOV
{
    /// <summary>
    /// 关键词
    /// </summary>
    [SerializeField]
    private string[] m_Keywords;//要进行关键字识别的数字 如["开灯","关灯","关机"]
    /// <summary>
    /// 关键字识别器
    /// </summary>
#if UNITY_STANDALONE_WIN
    private KeywordRecognizer m_Recognizer;
    void Start()
    {
        //创建一个关键字识别器
        m_Recognizer = new KeywordRecognizer(m_Keywords,ConfidenceLevel.Medium);
        Debug.Log("创建识别器成功");
        m_Recognizer.OnPhraseRecognized += OnPhraseRecognized;

        StartRecognizer();
    }

    private void OnDestroy()
    {
        StopRecognizer();
    }

    /// <summary>
    /// 开始识别
    /// </summary>
    public override void StartRecognizer()
    {
        if (m_Recognizer == null)
            return;

        m_Recognizer.Start();
    }
    /// <summary>
    /// 结束识别
    /// </summary>
    public override void StopRecognizer()
    {
        if (m_Recognizer == null)
            return;

        m_Recognizer.Stop();
    }

    /// <summary>
    /// 识别关键词回调
    /// </summary>
    /// <param name="args"></param>
    private void OnPhraseRecognized(PhraseRecognizedEventArgs args)
    {
        StringBuilder builder = new StringBuilder();
        builder.AppendFormat("{0}", args.text);
        string _keyWord = builder.ToString();
        Debug.Log("识别器捕捉到关键词:"+_keyWord);
        OnAwakeOnVoice(_keyWord); //处理回调
        KeyWordHandle(_keyWord); //在本类里处理
    }
#endif
    private void KeyWordHandle(string keyWord)
    {
        DateTime dateTime = DateTime.Now;
        string strNowTime = string.Format("{0:D}-{1:D}-{2:D}-{3:D}-{4:D}-{5:D}-{6:D}", dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dateTime.Second, dateTime.Millisecond);

        Debug.LogWarning($"识别到语音指令:{keyWord}"+"  "+strNowTime );
        switch (keyWord)
        {
            //识别不同的关键字后的逻辑
        }
    }

    private void RecordCommandTime(string msg)
    {
        Debug.Log($"识别到语音指令:{msg}");
    }
}

 

点赞

发表回复

昵称和uid可以选填一个,填邮箱必填(留言回复后将会发邮件给你)
tips:输入uid可以快速获得你的昵称和头像(已失效)