news 2026/2/26 13:32:45

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/2/23 16:34:23

GitHub Pages发布技术博客:分享Miniconda使用心得

GitHub Pages发布技术博客&#xff1a;分享Miniconda使用心得 在数据科学和人工智能项目开发中&#xff0c;你是否曾遇到过这样的场景&#xff1f;刚克隆下同事的代码仓库&#xff0c;满怀期待地运行 python train.py&#xff0c;结果却因“模块未找到”或“版本不兼容”而报错…

作者头像 李华
网站建设 2026/2/26 3:49:14

Markdown的使用及数学公式

本文介绍如何使用Markdown,以及在markdow中如何编写公式 使用 # 号标记 Markdown 使用 # 号来创建标题&#xff0c;这是从 HTML 的 <h1> 到 <h6> 标签概念演化而来的。 使用 # 号可表示 1-6 级标题&#xff0c;一级标题对应一个 # 号&#xff0c;二级标题对应两…

作者头像 李华
网站建设 2026/2/23 9:53:37

2025继续教育降AI率工具TOP10测评榜单

2025继续教育降AI率工具TOP10测评榜单 2025继续教育降AI率工具测评&#xff1a;为何需要一份专业榜单&#xff1f; 在继续教育领域&#xff0c;论文写作已成为许多学员必须面对的挑战。随着AI检测技术不断升级&#xff0c;传统降重手段已难以满足当前的检测标准。很多学员在提交…

作者头像 李华
网站建设 2026/2/23 11:10:05

大模型学习宝典:零基础入门到进阶完整路线_写给小白的大模型入门教程!大模型核心技术都在这了

本文全面介绍了大语言模型的基本概念、工作原理及类型&#xff0c;详细分析了开源与闭源模型的优缺点&#xff0c;阐述了从数据准备到应用开发的全流程&#xff0c;并探讨了参与大模型领域的方式及未来发展趋势。文章强调无论是技术相关还是非技术背景人士&#xff0c;都应积极…

作者头像 李华
网站建设 2026/2/25 15:16:47

Jupyter nbextension管理前端插件增强体验

Jupyter nbextension管理前端插件增强体验 在数据科学和机器学习项目中&#xff0c;我们常常面临一个看似微小却影响深远的问题&#xff1a;如何在一个不断膨胀的 Notebook 中保持清晰的结构与高效的开发节奏&#xff1f;当一个 .ipynb 文件超过 50 个代码块、包含多个模型训练…

作者头像 李华
网站建设 2026/2/26 3:18:45

JupyterLab整合Miniconda-Python3.10提升交互式编程效率

JupyterLab整合Miniconda-Python3.10提升交互式编程效率 在数据科学和人工智能项目中&#xff0c;一个常见的痛点是&#xff1a;代码在一个环境中运行正常&#xff0c;换到另一台机器却频频报错。问题往往不在于算法本身&#xff0c;而在于“环境不一致”——包版本冲突、依赖缺…

作者头像 李华