Unity使用sherpa-onnx实现关键词检测

使用模型 sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01

效果图

具体代码

复制代码
using uMicrophoneWebGL;
using UnityEngine;

[RequireComponent(typeof(MicrophoneWebGL))]
public class KeywordSpottingSample : MonoBehaviour
{
    MicrophoneWebGL microphone;
    public KeywordSpotting keywordSpotting;

    // Start is called before the first frame update
    void Start()
    {
        keywordSpotting.Init();
        microphone = GetComponent<MicrophoneWebGL>();
        microphone.dataEvent.AddListener(OnAudioData);
    }

    public void OnAudioData(float[] data)
    {
        if (keywordSpotting != null)
        {
            keywordSpotting.AcceptData(data);
        }
    }

    float timer = 0f;
    float interval = 0.2f;
    string keyword;
    private void Update()
    {
        if (keywordSpotting != null && keywordSpotting.initDone)
        {
            timer += Time.deltaTime;
            if (timer >= interval)
            {
                keyword = keywordSpotting.Recognize();
                if (!string.IsNullOrEmpty(keyword))
                {
                    Debug.Log("keyword:" + keyword);
                } 
                timer = 0f;
            } 
        }
    }
}

using System.IO;
using SherpaOnnx;
using UnityEngine;

/// <summary>
/// 关键字识别
/// </summary>
public class KeywordSpotting : MonoBehaviour
{
    KeywordSpotter keywordSpotter;
    string pathRoot;
    string modelPath;
    OnlineStream onlineStream;
    int sampleRate = 16000;
    public bool initDone = false;

    public void Init()
    {
        pathRoot = Util.GetPath() + "/models";

        //需要将此文件夹拷贝到models
        modelPath = pathRoot + "/sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01";
        KeywordSpotterConfig config = new KeywordSpotterConfig();
        config.FeatConfig.SampleRate = 16000;
        config.FeatConfig.FeatureDim = 80;

        config.ModelConfig.Transducer.Encoder = Path.Combine(modelPath, "encoder-epoch-12-avg-2-chunk-16-left-64.onnx");
        config.ModelConfig.Transducer.Decoder = Path.Combine(modelPath, "decoder-epoch-12-avg-2-chunk-16-left-64.onnx");
        config.ModelConfig.Transducer.Joiner = Path.Combine(modelPath, "joiner-epoch-12-avg-2-chunk-16-left-64.onnx");

        config.ModelConfig.Tokens = Path.Combine(modelPath, "tokens.txt");
        config.ModelConfig.Provider = "cpu";
        config.ModelConfig.NumThreads = 1;
        config.ModelConfig.Debug = 0;
        config.KeywordsFile = Path.Combine(modelPath, "keywords.txt");
        keywordSpotter = new KeywordSpotter(config);
        onlineStream = keywordSpotter.CreateStream();
        initDone = true;
    }

    public void AcceptData(float[] data)
    {
        onlineStream.AcceptWaveform(sampleRate, data);
    }

    KeywordResult result;
    public string Recognize()
    { 
        while (keywordSpotter.IsReady(onlineStream))
        {
            keywordSpotter.Decode(onlineStream);
            result = keywordSpotter.GetResult(onlineStream);
            if (result.Keyword != string.Empty)
            {
                Debug.Log("关键字: " + result.Keyword);
                // Remember to call Reset() right after detecting a keyword
                keywordSpotter.Reset(onlineStream);
                return result.Keyword;
            }
        }
        return string.Empty;
    }
}

最后是工程地址

https://github.com/xue-fei/sherpa-onnx-unity

相关推荐
在路上看风景11 小时前
31. Unity 异步加载的底层细节
unity
天人合一peng13 小时前
Unity中做表头时像work中整个调整宽窄
unity
小李也疯狂1 天前
Unity 中的立方体贴图(Cubemaps)
unity·游戏引擎·贴图·cubemap
牛掰是怎么形成的1 天前
Unity材质贴图引用陷阱:包体暴涨真相
unity·材质·贴图
呆呆敲代码的小Y1 天前
【Unity工具篇】| 超实用工具LuBan,快速上手使用
游戏·unity·游戏引擎·unity插件·luban·免费游戏·游戏配置表
EQ-雪梨蛋花汤1 天前
【Unity优化】Unity多场景加载优化与资源释放完整指南:解决Additive加载卡顿、预热、卸载与内存释放问题
unity·游戏引擎
我的offer在哪里1 天前
用 Unity 从 0 做一个「可以玩的」游戏,需要哪些步骤和流程
游戏·unity·游戏引擎
泡泡茶壶ᐇ1 天前
Unity游戏开发入门指南:从零开始理解游戏引擎核心概念
unity·游戏引擎
YigAin1 天前
Unity中的Lock,到底在锁什么,什么时候该用?
unity
Var_al1 天前
抖小Unity WebGL分包命令行工具实践指南
unity·游戏引擎·webgl