news 2026/8/7 6:12:55

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

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
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

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/28 15:18:44

Miniconda环境文档化:Sphinx生成API参考手册

Miniconda环境文档化&#xff1a;Sphinx生成API参考手册 在人工智能和数据科学项目日益复杂的今天&#xff0c;一个常见的困扰是&#xff1a;代码能在本地运行&#xff0c;却在同事或生产环境中报错。更糟的是&#xff0c;当需要发布某个算法库时&#xff0c;发现API文档早已与…

作者头像 李华
网站建设 2026/8/2 18:51:59

GitHub Pull Request流程:贡献Miniconda相关开源项目

GitHub Pull Request流程&#xff1a;贡献Miniconda相关开源项目 在数据科学和人工智能项目日益复杂的今天&#xff0c;一个常见的工程痛点是&#xff1a;为什么代码在你的机器上运行正常&#xff0c;却在CI流水线或同事环境中报错&#xff1f; 答案往往指向依赖管理的不一致。…

作者头像 李华
网站建设 2026/8/5 19:43:31

Docker save/load导出导入Miniconda镜像便于迁移

Docker save/load 导出导入 Miniconda 镜像实现高效环境迁移 在人工智能和数据科学项目中&#xff0c;一个常见的痛点是&#xff1a;“代码没问题&#xff0c;但跑不出来。” 这往往不是因为算法有缺陷&#xff0c;而是环境不一致导致的依赖冲突——有人用的是 Python 3.9&…

作者头像 李华
网站建设 2026/7/28 10:01:44

使用tox测试框架验证Miniconda环境兼容性

使用tox测试框架验证Miniconda环境兼容性 在AI与数据科学项目日益复杂的今天&#xff0c;一个令人头疼的问题始终存在&#xff1a;为什么代码在本地运行正常&#xff0c;到了服务器或同事的机器上却频频报错&#xff1f;更典型的情况是&#xff0c;模型训练脚本依赖特定版本的P…

作者头像 李华
网站建设 2026/8/6 3:05:56

AdisInsight数据库的3个应用场景与5个内容模块

AdisInsight是由Springer Nature集团旗下Adis出版社开发一款研发信息数据库&#xff0c;AdisInsight药物研发信息数据库包含关于药物开发、临床试验、药物不良反应事件、交易和专利的信息&#xff0c;其搜索范围涵盖数千种期刊、会议论文集、公司网站和其他已发表的资料&#x…

作者头像 李华
网站建设 2026/7/30 2:09:48

四轴桥板卧加编程:AB轴坐标转换宏程序与VT送出

四轴桥板-卧加-AB轴坐标转换宏程序送VT 四轴桥板卧加编程带刀尖跟随G65p9012配套UG-MC后处理&#xff0c;适用于四轴不带rtcp功能的机床工件任意摆放&#xff0c;一次装夹&#xff0c;任意点位建立坐标&#xff0c;后处理自动计算与回转中心的差值三菱-发那科-新代系统可通用A轴…

作者头像 李华