news 2026/10/15 17:44:01

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/10/14 15:53:14

SSH免密登录配置:提升频繁连接Miniconda容器效率

SSH免密登录配置&#xff1a;提升频繁连接Miniconda容器效率 在如今的AI研发和数据科学工作中&#xff0c;开发者早已习惯于在本地主机与远程计算环境之间来回切换。无论是调试模型训练脚本、同步实验代码&#xff0c;还是批量管理多个容器节点&#xff0c;SSH都是最常用的“桥…

作者头像 李华
网站建设 2026/10/10 22:06:36

国人打造!效果直逼 Manus!专为 Agent 设计的 Memory 项目

项目简介 Acontext 是一个上下文数据平台&#xff0c;它&#xff1a; 存储 上下文和工件观察 代理任务和用户反馈。通过将经验&#xff08;SOP&#xff09;收集到长期记忆中&#xff0c;实现代理自我学习。提供本地仪表板来查看消息、任务、工件和经验。存储、观察和学习 我们正…

作者头像 李华
网站建设 2026/10/10 22:41:00

AI大模型时代程序员生存指南:从职业转型到高薪岗位的完整路径_大龄程序员想转行大模型,应该往哪个方向转?

AI正颠覆全行业&#xff0c;要求每个人提升AI领导力。未来职业结构将呈现"两头重&#xff0c;中间轻"形态&#xff0c;程序员需向专业能力更强的方向发展。文章详细介绍了程序员转型大模型开发的四大方向(NLP、CV、算法、部署)&#xff0c;分析了岗位需求和薪资前景&…

作者头像 李华
网站建设 2026/10/10 22:39:59

基于SpringBoot的智慧医疗问诊系统毕设源码+文档+讲解视频

前言 随着医疗数字化转型加速&#xff0c;传统就医模式存在挂号难、问诊排队久、医疗资源分配不均等问题&#xff0c;难以满足群众便捷化、高效化的就医需求。本课题旨在设计并实现一款基于SpringBoot框架的智慧医疗问诊系统&#xff0c;构建“线上问诊健康管理”一体化医疗服务…

作者头像 李华
网站建设 2026/10/15 1:36:38

使用pyproject.toml替代setup.py管理Miniconda项目依赖

使用 pyproject.toml 管理 Miniconda 项目的现代依赖实践 在数据科学和人工智能项目中&#xff0c;你是否曾遇到这样的场景&#xff1a;同事拉下代码后运行报错&#xff0c;提示“numpy 版本不兼容”&#xff1f;或者论文实验在本地完美复现&#xff0c;换到服务器却因环境差异…

作者头像 李华
网站建设 2026/10/14 14:31:55

SSH密钥认证连接Miniconda容器实现免密登录

SSH密钥认证连接Miniconda容器实现免密登录 在数据科学和人工智能开发中&#xff0c;一个常见的痛点是&#xff1a;如何在保证环境隔离与依赖一致性的前提下&#xff0c;安全高效地访问远程计算资源&#xff1f;尤其是在使用GPU服务器或Docker容器进行模型训练时&#xff0c;频…

作者头像 李华