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

相关推荐
游乐码17 小时前
Unity基础(十三)资源卸载
unity·游戏引擎
winlife_17 小时前
全程用 AI 做一款商业级手游 · EP7 表现层与手感:从“能跑“到“摸起来爽“
java·开发语言·人工智能·unity·ai编程·游戏开发·mcp
冰糖橘子ABC17 小时前
Unity 动作重定向
unity·游戏引擎
程序员正茂18 小时前
Unity3d程序发布后自动显示发布日期
unity
nnsix1 天前
Unity 贴图压缩格式 笔记
笔记·unity·贴图
ysn111112 天前
搭建状态同步框架的实践心得
unity·架构
weixin_441940012 天前
【Unity教程】使用vuforia创建简单的AR实例
unity·游戏引擎·ar
郝学胜-神的一滴2 天前
[简化版 GAMES 101] 计算机图形学 12:可见性与 Z‑Buffer 深度缓存
unity·godot·图形渲染·three.js·opengl·unreal
游乐码3 天前
Unity基础(十一 )资源同步加载
unity·游戏引擎
LONGZETECH3 天前
汽车仿真教学软件技术实现深度解析:从三维建模到学情数据闭环
c语言·3d·unity·架构·汽车