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

相关推荐
weixin_424294673 天前
Unity 调用Steamworks API 的 SteamUserStats.RequestCurrentStats()报错
unity·游戏引擎·steamwork
HoFunGames3 天前
Unity小地图,Easy Minimap System MT-GPS插件
unity·游戏引擎
wy3258643643 天前
Unity 新输入系统InputSystem(基本操作)
unity·c#·游戏引擎
WarPigs3 天前
着色器multi_compile笔记
unity·着色器
ECHO飞跃 0123 天前
Unity2019 本地推理 通义千问0.5-1.5B微调导入
人工智能·深度学习·unity·llama
Unity游戏资源学习屋3 天前
【Unity UI资源包】GUI Pro - Casual Game 专为休闲手游打造的专业级UI资源包
ui·unity
冰凌糕3 天前
Unity3D Shader 顶点法线外扩实现描边效果
unity
小菱形_3 天前
【Unity】TimeLine
unity·游戏引擎
小贺儿开发4 天前
Unity3D 自动化物流分拣模拟
运维·科技·unity·自动化·人机交互·传送带·物流分拣
EQ-雪梨蛋花汤4 天前
【3D可视化】基于 Unity 的智慧体育馆三维信息可视化大屏实践
3d·unity·信息可视化